CharacterSetECI.php 3.5 KB

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