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

Skip to content

Commit f2b8682

Browse files
committed
Create new Exception message
1 parent 22b87b6 commit f2b8682

File tree

2 files changed

+107
-30
lines changed

2 files changed

+107
-30
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -785,14 +785,13 @@ public function offsetGet($option)
785785
}
786786

787787
if (!$valid) {
788-
throw new InvalidOptionsException(sprintf(
789-
'The option "%s" with value %s is expected to be of type '.
790-
'"%s", but is of type "%s".',
791-
$option,
792-
$this->formatValue($value),
793-
implode('" or "', $this->allowedTypes[$option]),
794-
implode('|', array_keys($invalidTypes))
795-
));
788+
$keys = array_keys($invalidTypes);
789+
790+
if (1 === \count($keys) && '[]' === substr($keys[0], -2)) {
791+
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), implode('|', array_keys($invalidTypes))));
792+
}
793+
794+
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), implode('|', array_keys($invalidTypes))));
796795
}
797796
}
798797

@@ -878,19 +877,7 @@ public function offsetGet($option)
878877
private function verifyTypes($type, $value, array &$invalidTypes)
879878
{
880879
if (\is_array($value) && '[]' === substr($type, -2)) {
881-
if ($this->verifyArrayType($type, $value, $invalidTypes, $type)) {
882-
return true;
883-
}
884-
885-
$invalidValues = $this->getInvalidValues($value, $type);
886-
887-
if (!$invalidValues) {
888-
return true;
889-
}
890-
891-
$invalidTypes[$this->formatTypeOf($value, $type)] = true;
892-
893-
return false;
880+
return $this->verifyArrayType($type, $value, $invalidTypes);
894881
}
895882

896883
if (self::isValueValidType($type, $value)) {
@@ -907,28 +894,41 @@ private function verifyTypes($type, $value, array &$invalidTypes)
907894
/**
908895
* @return bool
909896
*/
910-
private function verifyArrayType($type, array $value, array &$invalidTypes)
897+
private function verifyArrayType($type, array $value, array &$invalidTypes, $level = 0)
911898
{
912899
$type = substr($type, 0, -2);
913900

901+
$suffix = '[]';
902+
while (\strlen($suffix) <= $level * 2) {
903+
$suffix .= '[]';
904+
}
905+
914906
if ('[]' === substr($type, -2)) {
915907
$success = true;
916908
foreach ($value as $item) {
917909
if (!\is_array($item)) {
918-
$invalidTypes[$this->formatTypeOf($item, null)] = true;
910+
$invalidTypes[$this->formatTypeOf($item, null).$suffix] = true;
919911

920912
return false;
921913
}
922914

923-
if (!$this->verifyArrayType($type, $item, $invalidTypes)) {
915+
if (!$this->verifyArrayType($type, $item, $invalidTypes, $level + 1)) {
924916
$success = false;
925917
}
926918
}
927919

928920
return $success;
929921
}
930922

931-
return !$this->getInvalidValues($value, $type);
923+
foreach ($value as $item) {
924+
if (!self::isValueValidType($type, $item)) {
925+
$invalidTypes[$this->formatTypeOf($item, $type).$suffix] = $value;
926+
927+
return false;
928+
}
929+
}
930+
931+
return true;
932932
}
933933

934934
/**

src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public function testFailIfSetAllowedTypesFromLazyOption()
511511

512512
/**
513513
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
514-
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but is of type "DateTime[]".
514+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "DateTime[]".
515515
*/
516516
public function testResolveFailsIfInvalidTypedArray()
517517
{
@@ -535,7 +535,7 @@ public function testResolveFailsWithNonArray()
535535

536536
/**
537537
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
538-
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but is of type "integer|stdClass|array|DateTime[]".
538+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "stdClass[]".
539539
*/
540540
public function testResolveFailsIfTypedArrayContainsInvalidTypes()
541541
{
@@ -552,7 +552,7 @@ public function testResolveFailsIfTypedArrayContainsInvalidTypes()
552552

553553
/**
554554
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
555-
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but is of type "double[][]".
555+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double[][]".
556556
*/
557557
public function testResolveFailsWithCorrectLevelsButWrongScalar()
558558
{
@@ -1701,9 +1701,9 @@ public function testNested2Arrays()
17011701

17021702
/**
17031703
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1704-
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "float[][][][]", but is of type "integer[][][][]".
1704+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer[][][][]".
17051705
*/
1706-
public function testNestedArraysError()
1706+
public function testNestedArraysException()
17071707
{
17081708
$this->resolver->setDefined('foo');
17091709
$this->resolver->setAllowedTypes('foo', 'float[][][][]');
@@ -1720,4 +1720,81 @@ public function testNestedArraysError()
17201720
)
17211721
);
17221722
}
1723+
1724+
/**
1725+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1726+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".
1727+
*/
1728+
public function testNestedArrayException1()
1729+
{
1730+
$this->resolver->setDefined('foo');
1731+
$this->resolver->setAllowedTypes('foo', 'int[][]');
1732+
$this->resolver->resolve(array(
1733+
'foo' => array(
1734+
array(1, true, 'str', array(2, 3)),
1735+
),
1736+
));
1737+
}
1738+
1739+
/**
1740+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1741+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".
1742+
*/
1743+
public function testNestedArrayException2()
1744+
{
1745+
$this->resolver->setDefined('foo');
1746+
$this->resolver->setAllowedTypes('foo', 'int[][]');
1747+
$this->resolver->resolve(array(
1748+
'foo' => array(
1749+
array(true, 'str', array(2, 3)),
1750+
),
1751+
));
1752+
}
1753+
1754+
/**
1755+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1756+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string[][]".
1757+
*/
1758+
public function testNestedArrayException3()
1759+
{
1760+
$this->resolver->setDefined('foo');
1761+
$this->resolver->setAllowedTypes('foo', 'string[][][]');
1762+
$this->resolver->resolve(array(
1763+
'foo' => array(
1764+
array('str', array(1, 2)),
1765+
),
1766+
));
1767+
}
1768+
1769+
/**
1770+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1771+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer[][][]".
1772+
*/
1773+
public function testNestedArrayException4()
1774+
{
1775+
$this->resolver->setDefined('foo');
1776+
$this->resolver->setAllowedTypes('foo', 'string[][][]');
1777+
$this->resolver->resolve(array(
1778+
'foo' => array(
1779+
array(
1780+
array('str'), array(1, 2), ),
1781+
),
1782+
));
1783+
}
1784+
1785+
/**
1786+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1787+
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[]", but one of the elements is of type "array[]".
1788+
*/
1789+
public function testNestedArrayException5()
1790+
{
1791+
$this->resolver->setDefined('foo');
1792+
$this->resolver->setAllowedTypes('foo', 'string[]');
1793+
$this->resolver->resolve(array(
1794+
'foo' => array(
1795+
array(
1796+
array('str'), array(1, 2), ),
1797+
),
1798+
));
1799+
}
17231800
}

0 commit comments

Comments
 (0)