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