Skip to content

[OptionsResolver] support array of instance validation #17032

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

Closed
wants to merge 22 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/OptionsResolver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.1.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be changed to 3.2.0 now.

-----

* Added typed array support as allowed type

2.6.0
-----

Expand Down
110 changes: 94 additions & 16 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,19 +794,8 @@ public function offsetGet($option)
$valid = false;

foreach ($this->allowedTypes[$option] as $type) {
$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;

if (function_exists($isFunction = 'is_'.$type)) {
if ($isFunction($value)) {
$valid = true;
break;
}

continue;
}

if ($value instanceof $type) {
$valid = true;
$valid = $this->verifyAllowedType($type, $value);
if ($valid) {
break;
}
}
Expand All @@ -818,7 +807,7 @@ public function offsetGet($option)
$option,
$this->formatValue($value),
implode('" or "', $this->allowedTypes[$option]),
$this->formatTypeOf($value)
$this->formatTypeOf($value, $option)
));
}
}
Expand Down Expand Up @@ -895,6 +884,42 @@ public function offsetGet($option)
return $value;
}

/**
* Verify value is of the allowed type. Recursive method to support
* typed array notation like ClassName[], or scalar arrays (int[]).
*
* @param string $type the required allowedType string
* @param mixed $value the value
*
* @return bool Whether the $value is of the allowed type
*/
private function verifyAllowedType($type, $value)
{
if (mb_substr($type, -2) === '[]') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the calls to mb_substr should be replaced by plain substr calls imho

//allowed type is typed array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a space after // (same for the other comments)

if (!is_array($value)) {
return false;
}
$subType = mb_substr($type, 0, -2);
foreach ($value as $v) {
//recursive call -> check subtype
if (!$this->verifyAllowedType($subType, $v)) {
return false;
}
}
//value was array, subtypes all matched -> allowed type OK
return true;
}

$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;

if (function_exists($isFunction = 'is_'.$type)) {
return $isFunction($value);
}

return ($value instanceof $type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parenthesis can be removed.

}

/**
* Returns whether a resolved option with the given name exists.
*
Expand Down Expand Up @@ -963,15 +988,68 @@ public function count()
* parameters should usually not be included in messages aimed at
* non-technical people.
*
* @param mixed $value The value to return the type of
* @param mixed $value The value to return the type of
* @param string $option The option that holds the value
*
* @return string The type of the value
*/
private function formatTypeOf($value)
private function formatTypeOf($value, $option = null)
{
if (is_array($value) && $option) {
foreach ($this->allowedTypes[$option] as $type) {
if (mb_substr($type, -2) === '[]') {
return $this->formatComplexTypeOf($value, $type);
}
}
}

return is_object($value) ? get_class($value) : gettype($value);
}

/**
* Returns a string representation of the complex type of the value.
*
* This method should be called in formatTypeOf, if there is a complex allowed type
* for an array value defined to get a more explicit exception message
*
* @param array $value The value to return the complex type of
* @param string $type the expected type
*
* @return string the complex type of the value
*/
private function formatComplexTypeOf(array $value, $type)
{
$suffix = '[]';
$type = mb_substr($type, 0, -2);
while (mb_substr($type, -2) === '[]') {
$value = array_shift($value);
if (!is_array($value)) {
//expected a nested array, but we've already hit a scalar
break;
}
$type = mb_substr($type, 0, -2);
$suffix .= '[]';
}
if (is_array($value)) {
$subTypes = array();
foreach ($value as $v) {
$v = $this->formatTypeOf($v);
if (!isset($subTypes[$v])) {
$subTypes[$v] = $v;//build unique map from the off
}
}
$vType = implode('|', $subTypes);
} else {
$vType = is_object($value) ? get_class($value) : gettype($value);
}

return sprintf(
'%s%s',
$vType,
$suffix
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be on one line.

}

/**
* Returns a string representation of the value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,53 @@ public function testFailIfSetAllowedTypesFromLazyOption()
$this->resolver->resolve();
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but is of type "DateTime[]".
*/
public function testResolveFailsIfInvalidTypedArray()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');

$this->resolver->resolve(array('foo' => array(new \DateTime())));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but is of type "double[][]".
*/
public function testResolveFailsWithCorrectLevelsButWrongScalar()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');

$this->resolver->resolve(
array(
'foo' => array(
array(1.2),
),
)
);
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but is of type "integer|stdClass|array|DateTime[]".
*/
public function testResolveFailsIfTypedArrayContainsInvalidTypes()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');
$values = range(1, 5);
$values[] = new \stdClass();
$values[] = array();
$values[] = new \DateTime();
$values[] = 123;

$this->resolver->resolve(array('foo' => $values));
}

/**
* @dataProvider provideInvalidTypes
*/
Expand Down Expand Up @@ -551,6 +598,46 @@ public function testResolveSucceedsIfValidTypeMultiple()
$this->assertNotEmpty($this->resolver->resolve());
}

public function testResolveSucceedsIfTypedArray()
{
$this->resolver->setDefault('foo', null);
$this->resolver->setAllowedTypes('foo', array('null', '\DateTime[]'));

$data = array(
'foo' => array(
new \DateTime(),
new \DateTime(),
),
);
$result = $this->resolver->resolve($data);
$this->assertEquals($data, $result);
}

public function testResolveSucceedsNestedTypedArray()
{
$this->resolver->setDefault('foo', null);
$this->resolver->setAllowedTypes('foo', 'int[][]');

$expect = array(
'foo' => array(
range(1, 10),
),
);
$result = $this->resolver->resolve($expect);
$this->assertEquals($expect, $result);
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testResolveFailsIfNotInstanceOfClass()
{
$this->resolver->setDefault('foo', 'bar');
$this->resolver->setAllowedTypes('foo', '\stdClass');

$this->resolver->resolve();
}

public function testResolveSucceedsIfInstanceOfClass()
{
$this->resolver->setDefault('foo', new \stdClass());
Expand Down