-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[OptionResolver] resolve arrays #27435
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
Conversation
return $success; | ||
} | ||
|
||
$invalid = array_filter( // Filter out valid values, keeping invalid values in the resulting array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to remove the array_filter here, a foreach would be simpler
@nicolas-grekas hm.... you are right. I created new method |
/** | ||
* @param $type | ||
* @param array $value | ||
* @param array $invalidTypes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest removing these 3 lines, they provide no value over the signature
if ('[]' === substr($type, -2)) { | ||
$success = true; | ||
foreach ($value as $item) { | ||
if (!is_array($item)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!\is_array($item)) {
return $success; | ||
} | ||
|
||
$invalid = $this->getInvalidValues($value, $type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return !$this->getInvalidValues($value, $type);
@@ -1078,4 +1108,22 @@ private static function isValueValidType($type, $value) | |||
{ | |||
return (function_exists($isFunction = 'is_'.$type) && $isFunction($value)) || $value instanceof $type; | |||
} | |||
|
|||
/** | |||
* @param array $arrayValues |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not useful also
@@ -878,20 +878,17 @@ public function offsetGet($option) | |||
private function verifyTypes($type, $value, array &$invalidTypes) | |||
{ | |||
if ('[]' === substr($type, -2) && is_array($value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (\is_array($value) && '[]' === substr($type, -2)) {
@nicolas-grekas Thanks for comments. I fixed it |
Maybe a test more with mixed nested types? $resolver->setAllowedTypes('foo', 'int[][]');
$resolver->resolve(array(
'foo' => array(
array(1, true, 'str', array(2, 3)),
),
)); How will this look in the exception message? |
f2b8682
to
22bead3
Compare
@yceruto, Of course, you are right 👍 Your example here - f2b8682#diff-b9a7faded6c8bfc8c16c92999cc982f2R1728 And I added new message in exception. If system found first invalid argument in array. we get exception
instead of
|
Thank you @Doctrs. |
This PR was squashed before being merged into the 3.4 branch (closes #27435). Discussion ---------- [OptionResolver] resolve arrays | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Option resolver didn't work with nested arrays Before: $resolver->setDefaults([ 'integer' => [ [ 12, 23, ], ], ]); $resolver->setAllowedTypes('integer', 'integer[][]'); Error The option "host" with value array is expected to be of type "integer[][]", but is of type "integer[][]". Option expetcted type `integer[][]` but get... `integer[][]`. So strange Now that case work correct, and we get array (size=1) 'integer' => array (size=1) 0 => array (size=2) 0 => int 12 1 => int 23 Commits ------- 6d4812e [OptionResolver] resolve arrays
Option resolver didn't work with nested arrays
Before:
Error
Option expetcted type
integer[][]
but get...integer[][]
. So strangeNow that case work correct, and we get