TypeParser.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. } elseif ($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. /**
  60. * @param integer $token
  61. */
  62. private function isNextToken($token)
  63. {
  64. return null !== $this->next && $this->next[2] === $token;
  65. }
  66. private function matchAny(array $tokens)
  67. {
  68. if (null === $this->next) {
  69. throw new \InvalidArgumentException(sprintf('Expected any of %s, but reached end of type.', implode(' or ', array_map(array($this, 'getTokenName'), $tokens))));
  70. }
  71. $found = false;
  72. foreach ($tokens as $token) {
  73. if ($this->next[2] === $token) {
  74. $found = true;
  75. break;
  76. }
  77. }
  78. if ( ! $found) {
  79. 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]));
  80. }
  81. $this->moveNext();
  82. }
  83. /**
  84. * @param integer $token
  85. */
  86. private function match($token)
  87. {
  88. if (null === $this->next) {
  89. throw new \InvalidArgumentException(sprintf('Expected token %s, but reached end of type.', $this->getTokenName($token)));
  90. }
  91. if ($this->next[2] !== $token) {
  92. throw new \InvalidArgumentException(sprintf('Expected token %s, but got %s at position %d.', $this->getTokenName($token), $this->getTokenName($this->next[2]), $this->next[1]));
  93. }
  94. $this->moveNext();
  95. }
  96. private function moveNext()
  97. {
  98. $this->pointer += 1;
  99. $this->token = $this->next;
  100. $this->next = isset($this->tokens[$this->pointer]) ? $this->tokens[$this->pointer] : null;
  101. return null !== $this->next;
  102. }
  103. /**
  104. * @param integer $token
  105. */
  106. public static function getTokenName($token)
  107. {
  108. $ref = new \ReflectionClass(get_called_class());
  109. foreach ($ref->getConstants() as $name => $value) {
  110. if ($value === $token) {
  111. return $name;
  112. }
  113. }
  114. throw new \LogicException(sprintf('The token %s does not exist.', json_encode($token)));
  115. }
  116. /**
  117. * @param string $type
  118. */
  119. private function tokenize($type)
  120. {
  121. $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);
  122. $this->pointer = -1;
  123. foreach ($this->tokens as &$token) {
  124. $token[2] = $this->getType($token[0]);
  125. }
  126. $this->moveNext();
  127. }
  128. private function getType(&$value)
  129. {
  130. switch ($value[0]) {
  131. case '"':
  132. case "'":
  133. $value = substr($value, 1, -1);
  134. return self::T_STRING;
  135. case '<':
  136. return self::T_OPEN_BRACKET;
  137. case '>':
  138. return self::T_CLOSE_BRACKET;
  139. case ',':
  140. return self::T_COMMA;
  141. default:
  142. 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)) {
  143. return self::T_NAME;
  144. }
  145. return self::T_NONE;
  146. }
  147. }
  148. }