createForUnparsableToken() public static method

public static createForUnparsableToken ( Token $token, integer $code, Throwable $previous = null ) : ParseException
$token Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token
$code integer
$previous Throwable
return ParseException
Example #1
0
 /**
  * Parses expressions such as '$username'.
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token)
 {
     try {
         return new VariableValue(substr($token->getValue(), 1));
     } catch (\TypeError $error) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $error);
     }
 }
Example #2
0
 /**
  * Parses "10x @user*", "<randomNumber(0, 10)x @user<{param}>*", etc.
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token) : DynamicArrayValue
 {
     parent::parse($token);
     if (1 !== preg_match(self::REGEX, $token->getValue(), $matches)) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     return new DynamicArrayValue((int) $this->parser->parse($matches['quantifier']), $this->parser->parse($matches['elements']));
 }
Example #3
0
 /**
  * Parses expressions such as '60%? foo: bar'.
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token) : OptionalValue
 {
     parent::parse($token);
     if (1 !== preg_match(self::REGEX, $token->getValue(), $matches)) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     return new OptionalValue($this->parser->parse($matches['quantifier']), $this->parser->parse(trim($matches['first_member'])), array_key_exists('second_member', $matches) ? $this->parser->parse($matches['second_member']) : null);
 }
Example #4
0
 /**
  * Parses '<<', '@@'...
  *
  * {@inheritdoc}
  */
 public function parse(Token $token) : string
 {
     $value = $token->getValue();
     if ('' === $value) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     return $this->tokenizer->detokenize(substr($value, 1));
 }
 /**
  * Parses expressions such as "@user".
  *
  * {@inheritdoc}
  */
 public function parse(Token $token) : FixtureReferenceValue
 {
     $value = $token->getValue();
     try {
         return new FixtureReferenceValue(substr($value, 1));
     } catch (\InvalidArgumentException $exception) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $exception);
     }
 }
 /**
  * Parses expressions such as '$username'.
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token)
 {
     $value = $token->getValue();
     $fixtureId = substr($value, 1, strlen($value) - 2);
     if (false === $fixtureId) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     return FixtureMatchReferenceValue::createWildcardReference($fixtureId);
 }
Example #7
0
 /**
  * Parses '<{paramKey}>', '<{nested_<{param}>}>', etc.
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token) : ParameterValue
 {
     $value = $token->getValue();
     try {
         $paramKey = substr($value, 2, strlen($value) - 4);
         return new ParameterValue($paramKey);
     } catch (\TypeError $error) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $error);
     }
 }
 /**
  * @param Token $token
  *
  * @throws ParseException
  *
  * @return RangeName
  *
  * @example
  *  "@user{1..10}" => new RangeName('user', 1, 10)
  */
 private function buildRange(Token $token) : RangeName
 {
     $matches = [];
     $name = substr($token->getValue(), 1);
     if (1 !== preg_match(self::REGEX, (string) $name, $matches)) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     $reference = str_replace(sprintf('{%s}', $matches['range']), $this->token, $name);
     return new RangeName($reference, (int) $matches['from'], (int) $matches['to']);
 }
Example #9
0
 /**
  * Parses '<{paramKey}>', '<{nested_<{param}>}>'.
  *
  * {@inheritdoc}
  */
 public function parse(Token $token) : array
 {
     parent::parse($token);
     $value = $token->getValue();
     try {
         $elements = substr($value, 1, strlen($value) - 2);
         return $this->parseElements($this->parser, $elements);
     } catch (\TypeError $error) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $error);
     }
 }
 /**
  * Parses tokens values like "@user->username".
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token) : FixturePropertyValue
 {
     parent::parse($token);
     $explodedValue = explode('->', $token->getValue());
     if (count($explodedValue) !== 2) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     $reference = $this->parser->parse($explodedValue[0]);
     if (false === $reference instanceof ValueInterface) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     return new FixturePropertyValue($reference, $explodedValue[1]);
 }
 /**
  * Parses tokens values like "@user->getUserName()".
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token) : FixtureMethodCallValue
 {
     parent::parse($token);
     $explodedValue = explode('->', $token->getValue());
     if (count($explodedValue) !== 2) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     $reference = $this->parser->parse($explodedValue[0]);
     $method = $this->parser->parse(sprintf('<%s>', $explodedValue[1]));
     if ($reference instanceof FixtureReferenceValue && $method instanceof FunctionCallValue) {
         return new FixtureMethodCallValue($reference, $method);
     }
     throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
 }
 public function testTestCreateForUnparsableToken()
 {
     $token = new Token('foo', new TokenType(TokenType::DYNAMIC_ARRAY_TYPE));
     $exception = ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     $this->assertEquals('Could not parse the token "foo" (type: DYNAMIC_ARRAY_TYPE).', $exception->getMessage());
     $this->assertEquals(0, $exception->getCode());
     $this->assertNull($exception->getPrevious());
     $code = 500;
     $previous = new \Error();
     $exception = ExpressionLanguageExceptionFactory::createForUnparsableToken($token, $code, $previous);
     $this->assertEquals('Could not parse the token "foo" (type: DYNAMIC_ARRAY_TYPE).', $exception->getMessage());
     $this->assertEquals($code, $exception->getCode());
     $this->assertSame($previous, $exception->getPrevious());
 }
 /**
  * @param Token $token
  *
  * @throws ParseException
  *
  * @return string[]
  *
  * @example
  *  "@user_{alice, bob}" => ['user_alice', 'user_bob']
  */
 private function buildReferences(Token $token) : array
 {
     $matches = [];
     $name = (string) substr($token->getValue(), 1);
     if (1 !== preg_match(self::REGEX, $name, $matches)) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     $listElements = preg_split('/\\s*,\\s*/', $matches['list']);
     $references = [];
     foreach ($listElements as $element) {
         $fixtureId = str_replace(sprintf('{%s}', $matches['list']), $element, $name);
         $references[] = new FixtureReferenceValue($fixtureId);
     }
     return $references;
 }
 /**
  * Parses expressions such as "@username->getName()".
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token)
 {
     parent::parse($token);
     $values = preg_split('/->/', $token->getValue());
     if (2 !== count($values)) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     $fixture = $this->parser->parse($values[0]);
     $method = $this->parser->parse(sprintf('<%s>', $values[1]));
     try {
         return new FixtureMethodCallValue($fixture, $method);
     } catch (\TypeError $exception) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $exception);
     }
 }
Example #15
0
 /**
  * Parses expressions such as '<foo()>', '<foo(arg1, arg2)>'.
  *
  * {@inheritdoc}
  *
  * @throws ParseException
  */
 public function parse(Token $token) : FunctionCallValue
 {
     parent::parse($token);
     if (1 !== preg_match(self::REGEX, $token->getValue(), $matches)) {
         throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
     }
     $function = $matches['function'];
     if ('identity' === $function) {
         $arguments = [new EvaluatedValue($matches['arguments'])];
     } elseif ('current' === $function) {
         $arguments = [new ValueForCurrentValue()];
     } else {
         $arguments = $this->parseArguments($this->parser, trim($matches['arguments']));
     }
     return new FunctionCallValue($function, $arguments);
 }