TypeParser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace JMS\SerializerBundle\Serializer;
  3. /**
  4. * Parses a serializer type.
  5. *
  6. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  7. */
  8. final class TypeParser
  9. {
  10. const T_NAME = 1;
  11. const T_STRING = 2;
  12. const T_OPEN_BRACKET = 3;
  13. const T_CLOSE_BRACKET = 4;
  14. const T_COMMA = 5;
  15. const T_NONE = 6;
  16. private $tokens;
  17. private $token;
  18. private $next;
  19. private $pointer = 0;
  20. /**
  21. * @param string $type
  22. *
  23. * @return array of the format ["name" => string, "params" => array]
  24. */
  25. public function parse($type)
  26. {
  27. if (empty($type)) {
  28. throw new \InvalidArgumentException('$type cannot be empty.');
  29. }
  30. $this->tokenize($type);
  31. $parsedType = $this->parseType();
  32. if (null !== $this->next) {
  33. throw new \InvalidArgumentException(sprintf('Expected end of type, but got %s (%s) at position %d.', $this->getTokenName($this->next[0]), json_encode($this->next[2]), $this->next[1]));
  34. }
  35. return $parsedType;
  36. }
  37. private function parseType()
  38. {
  39. $this->match(self::T_NAME);
  40. $typeName = $this->token[0];
  41. if ( ! $this->isNextToken(self::T_OPEN_BRACKET)) {
  42. return array('name' => $typeName, 'params' => array());
  43. }
  44. $this->match(self::T_OPEN_BRACKET);
  45. $params = array();
  46. do {
  47. if ($this->isNextToken(self::T_NAME)) {
  48. $params[] = $this->parseType();
  49. } else if ($this->isNextToken(self::T_STRING)) {
  50. $this->moveNext();
  51. $params[] = $this->token[0];
  52. } else {
  53. $this->matchAny(array(self::T_NAME, self::T_STRING)); // Will throw an exception.
  54. }
  55. } while ($this->isNextToken(self::T_COMMA) && $this->moveNext());
  56. $this->match(self::T_CLOSE_BRACKET);
  57. return array('name' => $typeName, 'params' => $params);
  58. }
  59. private function isNextToken($token)
  60. {
  61. return null !== $this->next && $this->next[2] === $token;
  62. }
  63. private function matchAny(array $tokens)
  64. {
  65. if (null === $this->next) {
  66. throw new \InvalidArgumentException(sprintf('Expected any of %s, but reached end of type.', implode(' or ', array_map(array($this, 'getTokenName'), $tokens))));
  67. }
  68. $found = false;
  69. foreach ($tokens as $token) {
  70. if ($this->next[2] === $token) {
  71. $found = true;
  72. break;
  73. }
  74. }
  75. if ( ! $found) {
  76. throw new \InvalidArgumentException(sprintf('Expected any of %s, but got %s at position %d.', implode(' or ', array_map(array($this, 'getTokenName'), $tokens)), $this->getTokenName($this->next[2]), $this->next[1]));
  77. }
  78. $this->moveNext();
  79. }
  80. private function match($token)
  81. {
  82. if (null === $this->next) {
  83. throw new \InvalidArgumentException(sprintf('Expected token %s, but reached end of type.', $this->getTokenName($token)));
  84. }
  85. if ($this->next[2] !== $token) {
  86. throw new \InvalidArgumentException(sprintf('Expected token %s, but got %s at position %d.', $this->getTokenName($token), $this->getTokenName($this->next[2]), $this->next[1]));
  87. }
  88. $this->moveNext();
  89. }
  90. private function moveNext()
  91. {
  92. $this->pointer += 1;
  93. $this->token = $this->next;
  94. $this->next = isset($this->tokens[$this->pointer]) ? $this->tokens[$this->pointer] : null;
  95. return null !== $this->next;
  96. }
  97. public static function getTokenName($token)
  98. {
  99. $ref = new \ReflectionClass(get_called_class());
  100. foreach ($ref->getConstants() as $name => $value) {
  101. if ($value === $token) {
  102. return $name;
  103. }
  104. }
  105. throw new \LogicException(sprintf('The token %s does not exist.', json_encode($token)));
  106. }
  107. private function tokenize($type)
  108. {
  109. $this->tokens = preg_split('/((?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*|"(?:[^"]|"")*"|\'(?:[^\']|\'\')*\'|<|>|,)|\s*/', $type, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
  110. $this->pointer = -1;
  111. foreach ($this->tokens as &$token) {
  112. $token[2] = $this->getType($token[0]);
  113. }
  114. $this->moveNext();
  115. }
  116. private function getType(&$value)
  117. {
  118. switch ($value[0]) {
  119. case '"':
  120. case "'":
  121. $value = substr($value, 1, -1);
  122. return self::T_STRING;
  123. case '<':
  124. return self::T_OPEN_BRACKET;
  125. case '>':
  126. return self::T_CLOSE_BRACKET;
  127. case ',':
  128. return self::T_COMMA;
  129. default:
  130. if (preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $value)) {
  131. return self::T_NAME;
  132. }
  133. return self::T_NONE;
  134. }
  135. }
  136. }