Skip to content

Commit 9c7b709

Browse files
committed
#5 Add missing files for CharacterSetECI bug . Thanks DarkL3mon
1 parent 82234b1 commit 9c7b709

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed

lib/QrReader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require_once ('common/BitMatrix.php');
77
require_once ('common/BitSource.php');
88
require_once ('common/BitArray.php');
9+
require_once ('common/CharacterSetEci.php');//
10+
require_once ('common/AbstractEnum.php');//
911
require_once ('BinaryBitmap.php');
1012
include_once ('LuminanceSource.php');
1113
include_once ('GDLuminanceSource.php');

lib/common/AbstractEnum.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Zxing\Common\CharacterSetEci\AbstractEnum;
4+
5+
use \Zxing\NotFoundException;
6+
use ReflectionClass;
7+
/**
8+
* A general enum implementation until we got SplEnum.
9+
*/
10+
final class AbstractEnum
11+
{
12+
/**
13+
* Default value.
14+
*/
15+
const __default = null;
16+
/**
17+
* Current value.
18+
*
19+
* @var mixed
20+
*/
21+
protected $value;
22+
/**
23+
* Cache of constants.
24+
*
25+
* @var array
26+
*/
27+
protected $constants;
28+
/**
29+
* Whether to handle values strict or not.
30+
*
31+
* @var boolean
32+
*/
33+
protected $strict;
34+
/**
35+
* Creates a new enum.
36+
*
37+
* @param mixed $initialValue
38+
* @param boolean $strict
39+
*/
40+
public function __construct($initialValue = null, $strict = false)
41+
{
42+
$this->strict = $strict;
43+
$this->change($initialValue);
44+
}
45+
/**
46+
* Changes the value of the enum.
47+
*
48+
* @param mixed $value
49+
* @return void
50+
*/
51+
public function change($value)
52+
{
53+
if (!in_array($value, $this->getConstList(), $this->strict)) {
54+
throw new Exception\UnexpectedValueException('Value not a const in enum ' . get_class($this));
55+
}
56+
$this->value = $value;
57+
}
58+
/**
59+
* Gets current value.
60+
*
61+
* @return mixed
62+
*/
63+
public function get()
64+
{
65+
return $this->value;
66+
}
67+
/**
68+
* Gets all constants (possible values) as an array.
69+
*
70+
* @param boolean $includeDefault
71+
* @return array
72+
*/
73+
public function getConstList($includeDefault = true)
74+
{
75+
if ($this->constants === null) {
76+
$reflection = new ReflectionClass($this);
77+
$this->constants = $reflection->getConstants();
78+
}
79+
if ($includeDefault) {
80+
return $this->constants;
81+
}
82+
$constants = $this->constants;
83+
unset($constants['__default']);
84+
return $constants;
85+
}
86+
/**
87+
* Gets the name of the enum.
88+
*
89+
* @return string
90+
*/
91+
public function __toString()
92+
{
93+
return array_search($this->value, $this->getConstList());
94+
}
95+
}

lib/common/CharacterSetEci.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
4+
namespace Zxing\Common;
5+
use Zxing\Common\AbstractEnum;
6+
/**
7+
* Encapsulates a Character Set ECI, according to "Extended Channel
8+
* Interpretations" 5.3.1.1 of ISO 18004.
9+
*/
10+
final class CharacterSetECI
11+
{
12+
private static $name = null;
13+
/**#@+
14+
* Character set constants.
15+
*/
16+
const CP437 = 0;
17+
const ISO8859_1 = 1;
18+
const ISO8859_2 = 4;
19+
const ISO8859_3 = 5;
20+
const ISO8859_4 = 6;
21+
const ISO8859_5 = 7;
22+
const ISO8859_6 = 8;
23+
const ISO8859_7 = 9;
24+
const ISO8859_8 = 10;
25+
const ISO8859_9 = 11;
26+
const ISO8859_10 = 12;
27+
const ISO8859_11 = 13;
28+
const ISO8859_12 = 14;
29+
const ISO8859_13 = 15;
30+
const ISO8859_14 = 16;
31+
const ISO8859_15 = 17;
32+
const ISO8859_16 = 18;
33+
const SJIS = 20;
34+
const CP1250 = 21;
35+
const CP1251 = 22;
36+
const CP1252 = 23;
37+
const CP1256 = 24;
38+
const UNICODE_BIG_UNMARKED = 25;
39+
const UTF8 = 26;
40+
const ASCII = 27;
41+
const BIG5 = 28;
42+
const GB18030 = 29;
43+
const EUC_KR = 30;
44+
/**#@-*/
45+
/**
46+
* Map between character names and their ECI values.
47+
*
48+
* @var array
49+
*/
50+
protected static $nameToEci = array(
51+
'ISO-8859-1' => self::ISO8859_1,
52+
'ISO-8859-2' => self::ISO8859_2,
53+
'ISO-8859-3' => self::ISO8859_3,
54+
'ISO-8859-4' => self::ISO8859_4,
55+
'ISO-8859-5' => self::ISO8859_5,
56+
'ISO-8859-6' => self::ISO8859_6,
57+
'ISO-8859-7' => self::ISO8859_7,
58+
'ISO-8859-8' => self::ISO8859_8,
59+
'ISO-8859-9' => self::ISO8859_9,
60+
'ISO-8859-10' => self::ISO8859_10,
61+
'ISO-8859-11' => self::ISO8859_11,
62+
'ISO-8859-12' => self::ISO8859_12,
63+
'ISO-8859-13' => self::ISO8859_13,
64+
'ISO-8859-14' => self::ISO8859_14,
65+
'ISO-8859-15' => self::ISO8859_15,
66+
'ISO-8859-16' => self::ISO8859_16,
67+
'SHIFT-JIS' => self::SJIS,
68+
'WINDOWS-1250' => self::CP1250,
69+
'WINDOWS-1251' => self::CP1251,
70+
'WINDOWS-1252' => self::CP1252,
71+
'WINDOWS-1256' => self::CP1256,
72+
'UTF-16BE' => self::UNICODE_BIG_UNMARKED,
73+
'UTF-8' => self::UTF8,
74+
'ASCII' => self::ASCII,
75+
'GBK' => self::GB18030,
76+
'EUC-KR' => self::EUC_KR,
77+
);
78+
/**
79+
* Additional possible values for character sets.
80+
*
81+
* @var array
82+
*/
83+
protected static $additionalValues = array(
84+
self::CP437 => 2,
85+
self::ASCII => 170,
86+
);
87+
/**
88+
* Gets character set ECI by value.
89+
*
90+
* @param string $name
91+
* @return CharacterSetEci|null
92+
*/
93+
public static function getCharacterSetECIByValue($value)
94+
{
95+
if ($value < 0 || $value >= 900) {
96+
throw new Exception\InvalidArgumentException('Value must be between 0 and 900');
97+
}
98+
if (false !== ($key = array_search($value, self::$additionalValues))) {
99+
$value = $key;
100+
}
101+
array_search($value, self::$nameToEci);
102+
try
103+
{
104+
self::setName($value);
105+
return new self($value);
106+
} catch (Exception\UnexpectedValueException $e) {
107+
return null;
108+
}
109+
}
110+
111+
private static function setName($value)
112+
{
113+
foreach (self::$nameToEci as $name => $key) {
114+
if($key == $value)
115+
{
116+
self::$name = $name;
117+
return true;
118+
}
119+
}
120+
if(self::$name == null)
121+
{
122+
foreach (self::$additionalValues as $name => $key) {
123+
if($key == $value)
124+
{
125+
self::$name = $name;
126+
return true;
127+
}
128+
}
129+
}
130+
}
131+
/**
132+
* Gets character set ECI name.
133+
*
134+
* @return character set ECI name|null
135+
*/
136+
public static function name()
137+
{
138+
return self::$name;
139+
}
140+
/**
141+
* Gets character set ECI by name.
142+
*
143+
* @param string $name
144+
* @return CharacterSetEci|null
145+
*/
146+
public static function getCharacterSetECIByName($name)
147+
{
148+
$name = strtoupper($name);
149+
if (isset(self::$nameToEci[$name])) {
150+
return new self(self::$nameToEci[$name]);
151+
}
152+
return null;
153+
}
154+
}

0 commit comments

Comments
 (0)