Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ final class Encoder
/** @deprecated use DEFAULT_BYTE_MODE_ENCODING */
public const DEFAULT_BYTE_MODE_ECODING = self::DEFAULT_BYTE_MODE_ENCODING;

/**
* Allowed characters for the Alphanumeric Mode.
*/
private const ALPHANUMERIC_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';

/**
* The original table is defined in the table 5 of JISX0510:2004 (p.19).
*/
Expand Down Expand Up @@ -148,44 +153,30 @@ public static function encode(
/**
* Gets the alphanumeric code for a byte.
*/
private static function getAlphanumericCode(int $code) : int
private static function getAlphanumericCode(int $byte) : int
{
if (isset(self::ALPHANUMERIC_TABLE[$code])) {
return self::ALPHANUMERIC_TABLE[$code];
}

return -1;
return self::ALPHANUMERIC_TABLE[$byte] ?? -1;
}

/**
* Chooses the best mode for a given content.
*/
private static function chooseMode(string $content, ?string $encoding = null) : Mode
{
if ('' === $content) {
return Mode::BYTE();
}

if (null !== $encoding && 0 === strcasecmp($encoding, 'SHIFT-JIS')) {
return self::isOnlyDoubleByteKanji($content) ? Mode::KANJI() : Mode::BYTE();
}

$hasNumeric = false;
$hasAlphanumeric = false;
$contentLength = strlen($content);

for ($i = 0; $i < $contentLength; ++$i) {
$char = $content[$i];

if (ctype_digit($char)) {
$hasNumeric = true;
} elseif (-1 !== self::getAlphanumericCode(ord($char))) {
$hasAlphanumeric = true;
} else {
return Mode::BYTE();
}
if (ctype_digit($content)) {
return Mode::NUMERIC();
}

if ($hasAlphanumeric) {
if (self::isOnlyAlphanumeric($content)) {
return Mode::ALPHANUMERIC();
} elseif ($hasNumeric) {
return Mode::NUMERIC();
}

return Mode::BYTE();
Expand All @@ -205,7 +196,7 @@ private static function calculateMaskPenalty(ByteMatrix $matrix) : int
}

/**
* Checks if content only consists of double-byte kanji characters.
* Checks if content only consists of double-byte kanji characters (or is empty).
*/
private static function isOnlyDoubleByteKanji(string $content) : bool
{
Expand All @@ -232,6 +223,14 @@ private static function isOnlyDoubleByteKanji(string $content) : bool
return true;
}

/**
* Checks if content only consists of alphanumeric characters (or is empty).
*/
private static function isOnlyAlphanumeric(string $content) : bool
{
return strlen($content) === strspn($content, self::ALPHANUMERIC_CHARS);
}

/**
* Chooses the best mask pattern for a matrix.
*/
Expand Down
5 changes: 4 additions & 1 deletion test/Encoder/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function testGetAlphanumericCode() : void

public function testChooseMode() : void
{
// Empty string
$this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, ''));
$this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, '', 'SHIFT-JIS'));

// Numeric mode
$this->assertSame(Mode::NUMERIC(), $this->methods['chooseMode']->invoke(null, '0'));
$this->assertSame(Mode::NUMERIC(), $this->methods['chooseMode']->invoke(null, '0123456789'));
Expand All @@ -77,7 +81,6 @@ public function testChooseMode() : void
// 8-bit byte mode
$this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, 'a'));
$this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, '#'));
$this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, ''));

// AIUE in Hiragana in SHIFT-JIS
$this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, "\x8\xa\x8\xa\x8\xa\x8\xa6"));
Expand Down