/** * @inheritdoc */ public function typeCast(Token $token) { if (!$token->test(Token::T_INTEGER)) { throw new SyntaxErrorException('Timestamp type caster expects an integer token'); } return new \DateTime('@' . $token->getValue()); }
protected function getScalarValue(Token $token) { if ($token->test(Token::T_INTEGER)) { return $token->getValue(); } else { return parent::getScalarValue($token); } }
/** * @param string $rql * @param array $expected * @return void * * @dataProvider dataTokenize() */ public function testTokenize($rql, $expected) { $lexer = new Lexer(); $stream = $lexer->tokenize($rql); $this->assertSame(count($stream), count($expected) + 1); foreach ($expected as $token) { list($value, $type) = $token; $this->assertSame($value, $stream->getCurrent()->getValue(), sprintf('"%s" != "%s"', $value, $stream->getCurrent()->getValue())); $this->assertSame($type, $stream->getCurrent()->getType(), sprintf('"%s" != "%s"', Token::getTypeName($type), Token::getTypeName($stream->getCurrent()->getType()))); $stream->next(); } }
/** * @inheritdoc */ public function typeCast(Token $token) { if ($token->test(Token::T_NULL)) { return 0; } elseif ($token->test(Token::T_TRUE)) { return 1; } elseif ($token->test(Token::T_FALSE)) { return 0; } elseif ($token->test(Token::T_EMPTY)) { return 0; } elseif ($token->test(Token::T_DATE)) { return (int) (new \DateTime($token->getValue()))->format('YmdHis'); } else { return (int) $token->getValue(); } }
/** * @inheritdoc */ public function typeCast(Token $token) { if ($token->test(Token::T_NULL)) { return false; } elseif ($token->test(Token::T_TRUE)) { return true; } elseif ($token->test(Token::T_FALSE)) { return false; } elseif ($token->test(Token::T_EMPTY)) { return false; } elseif ($token->test(Token::T_DATE)) { return $token->getValue() === '0000-00-00T00:00:00Z'; } else { return (bool) $token->getValue(); } }
/** * @inheritdoc */ public function typeCast(Token $token) { if ($token->test(Token::T_NULL)) { return 0.0; } elseif ($token->test(Token::T_TRUE)) { return 1.0; } elseif ($token->test(Token::T_FALSE)) { return 0.0; } elseif ($token->test(Token::T_EMPTY)) { return 0.0; } elseif ($token->test(Token::T_DATE)) { return (double) DateTime::createFromRqlFormat($token->getValue())->format('YmdHis'); } else { return (double) $token->getValue(); } }
/** * @inheritdoc */ public function typeCast(Token $token) { if ($token->test(Token::T_NULL)) { return 'null'; } elseif ($token->test(Token::T_TRUE)) { return 'true'; } elseif ($token->test(Token::T_FALSE)) { return 'false'; } elseif ($token->test(Token::T_EMPTY)) { return ''; } elseif ($token->test(Token::T_GLOB)) { return rawurldecode($token->getValue()); } else { return $token->getValue(); } }
/** * @param int|int[] $type * @param string|string[] $value * @return Token * @throws SyntaxErrorException If the current token isn't expected */ public function expect($type, $value = null) { $token = $this->getCurrent(); if (!$this->test($type, $value)) { throw new SyntaxErrorException(sprintf('Unexpected token "%s" (%s) (%s)', $token->getValue(), $token->getName(), $value === null ? sprintf('expected %s', implode('|', array_map(function ($type) { return Token::getTypeName($type); }, (array) $type))) : sprintf('expected %s (%s)', implode('|', array_map(function ($value) { return '"' . $value . '"'; }, (array) $type)), implode('|', array_map(function ($type) { return Token::getTypeName($type); }, (array) $type))))); } $this->next(); return $token; }
/** * @param Token $token * @return mixed * @throws SyntaxErrorException */ protected function getScalarValue(Token $token) { if ($token->test(Token::T_FALSE)) { return false; } elseif ($token->test(Token::T_TRUE)) { return true; } elseif ($token->test(Token::T_NULL)) { return null; } elseif ($token->test(Token::T_EMPTY)) { return ''; } elseif ($token->test(Token::T_DATE)) { return new \DateTime($token->getValue()); } elseif ($token->test(Token::T_STRING)) { return $token->getValue(); } elseif ($token->test(Token::T_INTEGER)) { return (int) $token->getValue(); } elseif ($token->test(Token::T_FLOAT)) { return (double) $token->getValue(); } throw new SyntaxErrorException(sprintf('Invalid scalar token "%s" (%s)', $token->getValue(), $token->getName())); }