Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable default values #150

Merged
merged 2 commits into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion Tests/Parser/VariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@ public function testGetNullValueException()
$var->getValue();
}

public function testGetValueReturnsDefaultValueIfNoValueSet()
{
$var = new Variable('foo', 'bar', false, false, new Location(1,1));
$var->setDefaultValue('default-value');

$this->assertEquals(
'default-value',
$var->getValue()
);
}

public function testGetValueReturnsSetValueEvenWithDefaultValue()
{
$var = new Variable('foo', 'bar', false, false, new Location(1,1));
$var->setValue('real-value');
$var->setDefaultValue('default-value');

$this->assertEquals(
'real-value',
$var->getValue()
);
}

public function testIndicatesDefaultValuePresent()
{
$var = new Variable('foo', 'bar', false, false, new Location(1,1));
$var->setDefaultValue('default-value');

$this->assertTrue(
$var->hasDefaultValue()
);
}

public function testHasNoDefaultValue()
{
$var = new Variable('foo', 'bar', false, false, new Location(1,1));

$this->assertFalse(
$var->hasDefaultValue()
);
}

/**
* @return array Array of <mixed: value to set, mixed: expected value>
*/
Expand All @@ -41,4 +83,4 @@ public static function variableProvider()
]
];
}
}
}
6 changes: 6 additions & 0 deletions src/Execution/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public function __construct($data = [], $variables = [])
if (array_key_exists('variableReferences', $data)) {
foreach ($data['variableReferences'] as $ref) {
if (!array_key_exists($ref->getName(), $variables)) {
/** @var Variable $variable */
$variable = $ref->getVariable();
if ($variable->hasDefaultValue()) {
$variables[$variable->getName()] = $variable->getDefaultValue()->getValue();
continue;
}
throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation());
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/Parser/Ast/ArgumentValue/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class Variable extends AbstractAst implements ValueInterface
/** @var bool */
private $arrayElementNullable = true;

/** @var bool */
private $hasDefaultValue = false;

/** @var mixed */
private $defaultValue = null;

/**
* @param string $name
* @param string $type
Expand Down Expand Up @@ -62,6 +68,9 @@ public function __construct($name, $type, $nullable, $isArray, Location $locatio
public function getValue()
{
if (null === $this->value) {
if ($this->hasDefaultValue()) {
return $this->defaultValue;
}
throw new \LogicException('Value is not set for variable "' . $this->name . '"');
}

Expand Down Expand Up @@ -140,6 +149,32 @@ public function setNullable($nullable)
$this->nullable = $nullable;
}

/**
* @return bool
*/
public function hasDefaultValue()
{
return $this->hasDefaultValue;
}

/**
* @return mixed
*/
public function getDefaultValue()
{
return $this->defaultValue;
}

/**
* @param mixed $defaultValue
*/
public function setDefaultValue($defaultValue)
{
$this->hasDefaultValue = true;

$this->defaultValue = $defaultValue;
}

/**
* @return boolean
*/
Expand Down
9 changes: 8 additions & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,21 @@ protected function parseVariables()
$this->eat(Token::TYPE_REQUIRED);
}

$this->data['variables'][] = new Variable(
$variable = new Variable(
$nameToken->getData(),
$type,
$required,
$isArray,
new Location($variableToken->getLine(), $variableToken->getColumn()),
$arrayElementNullable
);

if ($this->match(Token::TYPE_EQUAL)) {
$this->eat(Token::TYPE_EQUAL);
$variable->setDefaultValue($this->parseValue());
}

$this->data['variables'][] = $variable;
}

$this->expect(Token::TYPE_RPAREN);
Expand Down
1 change: 1 addition & 0 deletions src/Parser/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Token
const TYPE_VARIABLE = '$';
const TYPE_POINT = '.';
const TYPE_REQUIRED = '!';
const TYPE_EQUAL = '=';

const TYPE_NULL = 'null';
const TYPE_TRUE = 'true';
Expand Down
5 changes: 5 additions & 0 deletions src/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ protected function scan()

return new Token(Token::TYPE_COLON, $this->getLine(), $this->getColumn());

case Token::TYPE_EQUAL:
++$this->pos;

return new Token(Token::TYPE_EQUAL, $this->getLine(), $this->getColumn());

case Token::TYPE_POINT:
if ($this->checkFragment()) {
return new Token(Token::TYPE_FRAGMENT_REFERENCE, $this->getLine(), $this->getColumn());
Expand Down