Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
Jul 7, 2018
Merged

[OptionResolver] resolve arrays #27435

merged 1 commit into from
Jul 7, 2018

Conversation

Doctrs
Copy link
Contributor

@Doctrs Doctrs commented May 30, 2018

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

return $success;
}

$invalid = array_filter( // Filter out valid values, keeping invalid values in the resulting array
Copy link
Member

@nicolas-grekas nicolas-grekas May 31, 2018

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

@Doctrs
Copy link
Contributor Author

Doctrs commented May 31, 2018

@nicolas-grekas hm.... you are right. I created new method getInvalidValues with foreach instead of array_filter

/**
* @param $type
* @param array $value
* @param array $invalidTypes
Copy link
Member

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)) {
Copy link
Member

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);
Copy link
Member

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
Copy link
Member

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)) {
Copy link
Member

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)) {

@Doctrs
Copy link
Contributor Author

Doctrs commented Jun 16, 2018

@nicolas-grekas Thanks for comments. I fixed it

@yceruto
Copy link
Member

yceruto commented Jun 20, 2018

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?

@Doctrs Doctrs force-pushed the 3.4 branch 2 times, most recently from f2b8682 to 22bead3 Compare June 21, 2018 19:17
@Doctrs
Copy link
Contributor Author

Doctrs commented Jun 21, 2018

@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

The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".

instead of

The option "%s" with value %s is expected to be of type "%s", but is of type "%s".

@nicolas-grekas
Copy link
Member

Thank you @Doctrs.

@nicolas-grekas nicolas-grekas merged commit 6d4812e into symfony:3.4 Jul 7, 2018
nicolas-grekas added a commit that referenced this pull request Jul 7, 2018
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
This was referenced Jul 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants