AbstractString.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the PHPASN1 library.
  4. *
  5. * Copyright © Friedrich Große <friedrich.grosse@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FG\ASN1;
  11. use Exception;
  12. abstract class AbstractString extends ASNObject implements Parsable
  13. {
  14. /** @var string */
  15. protected $value;
  16. private $checkStringForIllegalChars = true;
  17. private $allowedCharacters = [];
  18. /**
  19. * The abstract base class for ASN.1 classes which represent some string of character.
  20. *
  21. * @param string $string
  22. */
  23. public function __construct($string)
  24. {
  25. $this->value = $string;
  26. }
  27. public function getContent()
  28. {
  29. return $this->value;
  30. }
  31. protected function allowCharacter($character)
  32. {
  33. $this->allowedCharacters[] = $character;
  34. }
  35. protected function allowCharacters(...$characters)
  36. {
  37. foreach ($characters as $character) {
  38. $this->allowedCharacters[] = $character;
  39. }
  40. }
  41. protected function allowNumbers()
  42. {
  43. foreach (range('0', '9') as $char) {
  44. $this->allowedCharacters[] = (string) $char;
  45. }
  46. }
  47. protected function allowAllLetters()
  48. {
  49. $this->allowSmallLetters();
  50. $this->allowCapitalLetters();
  51. }
  52. protected function allowSmallLetters()
  53. {
  54. foreach (range('a', 'z') as $char) {
  55. $this->allowedCharacters[] = $char;
  56. }
  57. }
  58. protected function allowCapitalLetters()
  59. {
  60. foreach (range('A', 'Z') as $char) {
  61. $this->allowedCharacters[] = $char;
  62. }
  63. }
  64. protected function allowSpaces()
  65. {
  66. $this->allowedCharacters[] = ' ';
  67. }
  68. protected function allowAll()
  69. {
  70. $this->checkStringForIllegalChars = false;
  71. }
  72. protected function calculateContentLength()
  73. {
  74. return strlen($this->value);
  75. }
  76. protected function getEncodedValue()
  77. {
  78. if ($this->checkStringForIllegalChars) {
  79. $this->checkString();
  80. }
  81. return $this->value;
  82. }
  83. protected function checkString()
  84. {
  85. $stringLength = $this->getContentLength();
  86. for ($i = 0; $i < $stringLength; $i++) {
  87. if (in_array($this->value[$i], $this->allowedCharacters) == false) {
  88. $typeName = Identifier::getName($this->getType());
  89. throw new Exception("Could not create a {$typeName} from the character sequence '{$this->value}'.");
  90. }
  91. }
  92. }
  93. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  94. {
  95. $parsedObject = new static('');
  96. self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++);
  97. $contentLength = self::parseContentLength($binaryData, $offsetIndex);
  98. $string = substr($binaryData, $offsetIndex, $contentLength);
  99. $offsetIndex += $contentLength;
  100. $parsedObject->value = $string;
  101. $parsedObject->setContentLength($contentLength);
  102. return $parsedObject;
  103. }
  104. public static function isValid($string)
  105. {
  106. $testObject = new static($string);
  107. try {
  108. $testObject->checkString();
  109. return true;
  110. } catch (Exception $exception) {
  111. return false;
  112. }
  113. }
  114. }