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

[OptionResolver] resolve arrays #27400

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 1 commit 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
[OptionResolver] resolve arrays
  • Loading branch information
Doctrs committed May 28, 2018
commit dca2b69d29961744ff284aeac4f444a8c88d80b5
48 changes: 44 additions & 4 deletions 48 src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,10 @@ public function offsetGet($option)
private function verifyTypes($type, $value, array &$invalidTypes)
{
if ('[]' === substr($type, -2) && is_array($value)) {
$originalType = $type;
$type = substr($type, 0, -2);
if ($this->verifyArrayType($type, $value, $invalidTypes, $type)) {
return true;
}

$invalidValues = array_filter( // Filter out valid values, keeping invalid values in the resulting array
$value,
function ($value) use ($type) {
Expand All @@ -891,7 +893,7 @@ function ($value) use ($type) {
return true;
}

$invalidTypes[$this->formatTypeOf($value, $originalType)] = true;
$invalidTypes[$this->formatTypeOf($value, $type)] = true;

return false;
}
Expand All @@ -907,6 +909,44 @@ function ($value) use ($type) {
return false;
}

/**
* @param $type
* @param array $value
* @param array $invalidTypes
*
* @return bool
*/
private function verifyArrayType($type, array $value, array &$invalidTypes)
{
$type = substr($type, 0, -2);

if ('[]' === substr($type, -2)) {
$success = true;
foreach ($value as $item) {
if (!is_array($item)) {
$invalidTypes[$this->formatTypeOf($item, null)] = true;

return false;
}

if (!$this->verifyArrayType($type, $item, $invalidTypes)) {
$success = false;
}
}

return $success;
}

$invalid = array_filter( // Filter out valid values, keeping invalid values in the resulting array
$value,
function ($value) use ($type) {
return !self::isValueValidType($type, $value);
}
);

return !count($invalid);
}

/**
* Returns whether a resolved option with the given name exists.
*
Expand Down Expand Up @@ -975,7 +1015,7 @@ 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
*/
private function formatTypeOf($value, ?string $type): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1650,4 +1650,74 @@ public function testCountFailsOutsideResolve()

count($this->resolver);
}

public function testNestedArrays()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');

$this->assertEquals(array(
'foo' => array(
array(
1, 2,
),
),
), $this->resolver->resolve(
array(
'foo' => array(
array(1, 2),
),
)
));
}

public function testNested2Arrays()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][][][]');

$this->assertEquals(array(
'foo' => array(
array(
array(
array(
1, 2,
),
),
),
),
), $this->resolver->resolve(
array(
'foo' => array(
array(
array(
array(1, 2),
),
),
),
)
));
}

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

$this->resolver->resolve(
array(
'foo' => array(
array(
array(
array(1, 2),
),
),
),
)
);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.