Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Oro\ORM\Query\AST\Functions;
  4.  
  5. use Doctrine\ORM\Query\AST\Literal;
  6. use Doctrine\ORM\Query\Parser;
  7. use Doctrine\ORM\Query\Lexer;
  8.  
  9. class Cast extends AbstractPlatformAwareFunctionNode
  10. {
  11. const PARAMETER_KEY = 'expression';
  12. const TYPE_KEY = 'type';
  13.  
  14. protected $supportedTypes = array(
  15. 'char',
  16. 'string',
  17. 'text',
  18. 'date',
  19. 'datetime',
  20. 'time',
  21. 'int',
  22. 'integer',
  23. 'decimal',
  24. 'json',
  25. 'bool',
  26. 'boolean'
  27. );
  28.  
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function parse(Parser $parser)
  33. {
  34. $parser->match(Lexer::T_IDENTIFIER);
  35. $parser->match(Lexer::T_OPEN_PARENTHESIS);
  36. $this->parameters[self::PARAMETER_KEY] = $parser->ArithmeticExpression();
  37.  
  38. $parser->match(Lexer::T_AS);
  39.  
  40. $parser->match(Lexer::T_IDENTIFIER);
  41. $lexer = $parser->getLexer();
  42. $type = $lexer->token['value'];
  43.  
  44. if ($lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
  45. $parser->match(Lexer::T_OPEN_PARENTHESIS);
  46. /** @var Literal $parameter */
  47. $parameter = $parser->Literal();
  48. $parameters = array(
  49. $parameter->value
  50. );
  51. if ($lexer->isNextToken(Lexer::T_COMMA)) {
  52. while ($lexer->isNextToken(Lexer::T_COMMA)) {
  53. $parser->match(Lexer::T_COMMA);
  54. $parameter = $parser->Literal();
  55. $parameters[] = $parameter->value;
  56. }
  57. }
  58. $parser->match(Lexer::T_CLOSE_PARENTHESIS);
  59. $type .= '(' . implode(', ', $parameters) . ')';
  60. }
  61.  
  62. if (!$this->checkType($type)) {
  63. $parser->syntaxError(
  64. sprintf(
  65. 'Type unsupported. Supported types are: "%s"',
  66. implode(', ', $this->supportedTypes)
  67. ),
  68. $lexer->token
  69. );
  70. }
  71.  
  72. $this->parameters[self::TYPE_KEY] = $type;
  73.  
  74. $parser->match(Lexer::T_CLOSE_PARENTHESIS);
  75. }
  76.  
  77. /**
  78. * Check that given type is supported.
  79. *
  80. * @param string $type
  81. * @return bool
  82. */
  83. protected function checkType($type)
  84. {
  85. $type = strtolower(trim($type));
  86. foreach ($this->supportedTypes as $supportedType) {
  87. if (strpos($type, $supportedType) === 0) {
  88. return true;
  89. }
  90. }
  91.  
  92. return false;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement