CharacterSetEci.php 5.2 KB

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