Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TokenCollection.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php declare(strict_types = 1);
  2. namespace TheSeer\Tokenizer;
  3. class TokenCollection implements \ArrayAccess, \Iterator, \Countable {
  4. /**
  5. * @var Token[]
  6. */
  7. private $tokens = [];
  8. /**
  9. * @var int
  10. */
  11. private $pos;
  12. /**
  13. * @param Token $token
  14. */
  15. public function addToken(Token $token) {
  16. $this->tokens[] = $token;
  17. }
  18. /**
  19. * @return Token
  20. */
  21. public function current(): Token {
  22. return current($this->tokens);
  23. }
  24. /**
  25. * @return int
  26. */
  27. public function key(): int {
  28. return key($this->tokens);
  29. }
  30. /**
  31. * @return void
  32. */
  33. public function next() {
  34. next($this->tokens);
  35. $this->pos++;
  36. }
  37. /**
  38. * @return bool
  39. */
  40. public function valid(): bool {
  41. return $this->count() > $this->pos;
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function rewind() {
  47. reset($this->tokens);
  48. $this->pos = 0;
  49. }
  50. /**
  51. * @return int
  52. */
  53. public function count(): int {
  54. return count($this->tokens);
  55. }
  56. /**
  57. * @param mixed $offset
  58. *
  59. * @return bool
  60. */
  61. public function offsetExists($offset): bool {
  62. return isset($this->tokens[$offset]);
  63. }
  64. /**
  65. * @param mixed $offset
  66. *
  67. * @return Token
  68. * @throws TokenCollectionException
  69. */
  70. public function offsetGet($offset): Token {
  71. if (!$this->offsetExists($offset)) {
  72. throw new TokenCollectionException(
  73. sprintf('No Token at offest %s', $offset)
  74. );
  75. }
  76. return $this->tokens[$offset];
  77. }
  78. /**
  79. * @param mixed $offset
  80. * @param Token $value
  81. *
  82. * @throws TokenCollectionException
  83. */
  84. public function offsetSet($offset, $value) {
  85. if (!is_int($offset)) {
  86. $type = gettype($offset);
  87. throw new TokenCollectionException(
  88. sprintf(
  89. 'Offset must be of type integer, %s given',
  90. $type === 'object' ? get_class($value) : $type
  91. )
  92. );
  93. }
  94. if (!$value instanceof Token) {
  95. $type = gettype($value);
  96. throw new TokenCollectionException(
  97. sprintf(
  98. 'Value must be of type %s, %s given',
  99. Token::class,
  100. $type === 'object' ? get_class($value) : $type
  101. )
  102. );
  103. }
  104. $this->tokens[$offset] = $value;
  105. }
  106. /**
  107. * @param mixed $offset
  108. */
  109. public function offsetUnset($offset) {
  110. unset($this->tokens[$offset]);
  111. }
  112. }