Debian Codegraph @

src/Common/CharacterSetEci.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace BaconQrCode\Common;
5
6 use BaconQrCode\Exception\InvalidArgumentException;
7 use DASPRiD\Enum\AbstractEnum;
8
9 /**
10 * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1 of ISO 18004.
11 *
12 * @method static self CP437()
13 * @method static self ISO8859_1()
14 * @method static self ISO8859_2()
15 * @method static self ISO8859_3()
16 * @method static self ISO8859_4()
17 * @method static self ISO8859_5()
18 * @method static self ISO8859_6()
19 * @method static self ISO8859_7()
20 * @method static self ISO8859_8()
21 * @method static self ISO8859_9()
22 * @method static self ISO8859_10()
23 * @method static self ISO8859_11()
24 * @method static self ISO8859_12()
25 * @method static self ISO8859_13()
26 * @method static self ISO8859_14()
27 * @method static self ISO8859_15()
28 * @method static self ISO8859_16()
29 * @method static self SJIS()
30 * @method static self CP1250()
31 * @method static self CP1251()
32 * @method static self CP1252()
33 * @method static self CP1256()
34 * @method static self UNICODE_BIG_UNMARKED()
35 * @method static self UTF8()
36 * @method static self ASCII()
37 * @method static self BIG5()
38 * @method static self GB18030()
39 * @method static self EUC_KR()
40 */
41 final class CharacterSetEci extends AbstractEnum
42 {
43 protected const CP437 = [[0, 2]];
44 protected const ISO8859_1 = [[1, 3], 'ISO-8859-1'];
45 protected const ISO8859_2 = [[4], 'ISO-8859-2'];
46 protected const ISO8859_3 = [[5], 'ISO-8859-3'];
47 protected const ISO8859_4 = [[6], 'ISO-8859-4'];
48 protected const ISO8859_5 = [[7], 'ISO-8859-5'];
49 protected const ISO8859_6 = [[8], 'ISO-8859-6'];
50 protected const ISO8859_7 = [[9], 'ISO-8859-7'];
51 protected const ISO8859_8 = [[10], 'ISO-8859-8'];
52 protected const ISO8859_9 = [[11], 'ISO-8859-9'];
53 protected const ISO8859_10 = [[12], 'ISO-8859-10'];
54 protected const ISO8859_11 = [[13], 'ISO-8859-11'];
55 protected const ISO8859_12 = [[14], 'ISO-8859-12'];
56 protected const ISO8859_13 = [[15], 'ISO-8859-13'];
57 protected const ISO8859_14 = [[16], 'ISO-8859-14'];
58 protected const ISO8859_15 = [[17], 'ISO-8859-15'];
59 protected const ISO8859_16 = [[18], 'ISO-8859-16'];
60 protected const SJIS = [[20], 'Shift_JIS'];
61 protected const CP1250 = [[21], 'windows-1250'];
62 protected const CP1251 = [[22], 'windows-1251'];
63 protected const CP1252 = [[23], 'windows-1252'];
64 protected const CP1256 = [[24], 'windows-1256'];
65 protected const UNICODE_BIG_UNMARKED = [[25], 'UTF-16BE', 'UnicodeBig'];
66 protected const UTF8 = [[26], 'UTF-8'];
67 protected const ASCII = [[27, 170], 'US-ASCII'];
68 protected const BIG5 = [[28]];
69 protected const GB18030 = [[29], 'GB2312', 'EUC_CN', 'GBK'];
70 protected const EUC_KR = [[30], 'EUC-KR'];
71
72 /**
73 * @var string[]
74 */
75 private array $otherEncodingNames;
76
77 /**
78 * @var array<int, self>|null
79 */
80 private static ?array $valueToEci;
81
82 /**
83 * @var array<string, self>|null
84 */
85 private static ?array $nameToEci = null;
86
87 /**
88 * @param int[] $values
89 */
90 public function __construct(private readonly array $values, string ...$otherEncodingNames)
91 {
92 $this->otherEncodingNames = $otherEncodingNames;
93 }
94
95 /**
96 * Returns the primary value.
97 */
98 public function getValue() : int
99 {
100 return $this->values[0];
101 }
102
103 /**
104 * Gets character set ECI by value.
105 *
106 * Returns the representing ECI of a given value, or null if it is legal but unsupported.
107 *
108 * @throws InvalidArgumentException if value is not between 0 and 900
109 */
110 public static function getCharacterSetEciByValue(int $value) : ?self
111 {
112 if ($value < 0 || $value >= 900) {
113 throw new InvalidArgumentException('Value must be between 0 and 900');
114 }
115
116 $valueToEci = self::valueToEci();
117
118 if (! array_key_exists($value, $valueToEci)) {
119 return null;
120 }
121
122 return $valueToEci[$value];
123 }
124
125 /**
126 * Returns character set ECI by name.
127 *
128 * Returns the representing ECI of a given name, or null if it is legal but unsupported
129 */
130 public static function getCharacterSetEciByName(string $name) : ?self
131 {
132 $nameToEci = self::nameToEci();
133 $name = strtolower($name);
134
135 if (! array_key_exists($name, $nameToEci)) {
136 return null;
137 }
138
139 return $nameToEci[$name];
140 }
141
142 private static function valueToEci() : array
143 {
144 if (null !== self::$valueToEci) {
145 return self::$valueToEci;
146 }
147
148 self::$valueToEci = [];
149
150 foreach (self::values() as $eci) {
151 foreach ($eci->values as $value) {
152 self::$valueToEci[$value] = $eci;
153 }
154 }
155
156 return self::$valueToEci;
157 }
158
159 private static function nameToEci() : array
160 {
161 if (null !== self::$nameToEci) {
162 return self::$nameToEci;
163 }
164
165 self::$nameToEci = [];
166
167 foreach (self::values() as $eci) {
168 self::$nameToEci[strtolower($eci->name())] = $eci;
169
170 foreach ($eci->otherEncodingNames as $name) {
171 self::$nameToEci[strtolower($name)] = $eci;
172 }
173 }
174
175 return self::$nameToEci;
176 }
177 }