Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 1 commit
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
Prev Previous commit
Next Next commit
Format the exception message clearer: in case complex typed arrays co…
…ntain valid and invalid arguments, list all the values they contain + add test case
  • Loading branch information
EVODelavega committed Dec 21, 2015
commit 82d463f2e9d9b6bd9bd886c96a03ca2aef0c5417
17 changes: 13 additions & 4 deletions 17 src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,9 @@ public function count()
*
* @return string The type of the value
*/
private function formatTypeOf($value, $option)
private function formatTypeOf($value, $option = null)
{
if (is_array($value)) {
if (is_array($value) && $option) {
foreach ($this->allowedTypes[$option] as $type) {
if (substr($type, -2) === '[]') {
return $this->formatComplexTypeOf($value, $type);
Expand Down Expand Up @@ -1031,12 +1031,21 @@ private function formatComplexTypeOf(array $value, $type)
$suffix .= '[]';
}
if (is_array($value)) {
$value = array_shift($value);//get first element
$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',
is_object($value) ? get_class($value) : gettype($value),
$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.

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,23 @@ public function testResolveFailsIfInvalidTypedArray()

/**
* @dataProvider provideInvalidTypes
* @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));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value 42 is expected to be of type "string", but is of type "integer".
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.