Skip to content
Permalink
Browse files

Update signature of `ContextInterface::val()`

All classes implementing ContextInterface now take into account
`$options` passes to `val()` method.
  • Loading branch information...
ADmad committed Apr 30, 2016
1 parent 35af288 commit e84a83da5d19f51b5402936dd9e5a065deb883ff
@@ -149,14 +149,27 @@ public function isCreate()
*
* @param string $field A dot separated path to the field a value
* is needed for.
* @param array $options Options:
* - `default`: Default value to return if no value found in request
* data or context record.
* - `schemaDefault`: Boolen indicating whether default value from
* context's schema should be used if it's not explicitly provided.
* @return mixed
*/
public function val($field)
public function val($field, $options = [])
{
$options += [
'default' => null,
'schemaDefault' => true
];
$val = $this->_request->data($field);
if ($val !== null) {
return $val;
}
if ($options['default'] !== null || !$options['schemaDefault']) {
return $options['default'];
}
if (empty($this->_context['defaults']) || !is_array($this->_context['defaults'])) {
return null;
}
@@ -48,9 +48,14 @@ public function isCreate();
*
* @param string $field A dot separated path to the field a value
* is needed for.
* @param array $options Options:
* - `default`: Default value to return if no value found in request
* data or context record.
* - `schemaDefault`: Boolen indicating whether default value from
* context's schema should be used if it's not explicitly provided.
* @return mixed
*/
public function val($field);
public function val($field, $options = []);
/**
* Check if a given field is 'required'.
@@ -75,9 +75,18 @@ public function isCreate()
/**
* {@inheritDoc}
*/
public function val($field)
public function val($field, $options = [])
{
return $this->_request->data($field);
$options += [
'default' => null,
'schemaDefault' => true
];
$val = $this->_request->data($field);
if ($val !== null) {
return $val;
}
return $options['default'];
}
/**
@@ -166,6 +166,24 @@ public function testValMissing()
$this->assertNull($context->val('Comments.field'));
}
/**
* Test getting default value
*
* @return void
*/
public function testValDefault()
{
$context = new ArrayContext($this->request, [
'defaults' => [
'title' => 'Default value',
]
]);
$this->assertEquals('Default value', $context->val('title'));
$result = $context->val('title', ['default' => 'explicit default']);
$this->assertEquals('explicit default', $result);
}
/**
* Test isRequired
*
@@ -98,6 +98,19 @@ public function testValMissing()
$this->assertNull($context->val('Comments.field'));
}
/**
* Test getting default value
*
* @return void
*/
public function testValDefault()
{
$context = new FormContext($this->request, ['entity' => new Form()]);
$result = $context->val('title', ['default' => 'default default']);
$this->assertEquals('default default', $result);
}
/**
* Test isRequired
*

0 comments on commit e84a83d

Please sign in to comment.
You can’t perform that action at this time.