From aa7c6a8d7d6370c552c8f521dd031c355885a6e2 Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Mon, 28 Jan 2013 13:37:10 +0100 Subject: [PATCH 01/20] Added ability to use the context in hydrator strategies. This is very similar how validators are implemented. --- src/Hydrator/AbstractHydrator.php | 10 +++--- src/Hydrator/ClassMethods.php | 4 +-- src/Hydrator/ObjectProperty.php | 4 +-- src/Hydrator/Reflection.php | 4 +-- test/HydratorStrategyTest.php | 24 ++++++++++++++ .../HydratorStrategyContextAware.php | 31 +++++++++++++++++++ 6 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 test/TestAsset/HydratorStrategyContextAware.php diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 5d62edc85..adccdad0d 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -104,13 +104,14 @@ public function removeStrategy($name) * * @param string $name The name of the strategy to use. * @param mixed $value The value that should be converted. + * @param array $data The object is optionally provided as context. * @return mixed */ - public function extractValue($name, $value) + public function extractValue($name, $value, $object = null) { if ($this->hasStrategy($name)) { $strategy = $this->getStrategy($name); - $value = $strategy->extract($value); + $value = $strategy->extract($value, $object); } return $value; } @@ -120,13 +121,14 @@ public function extractValue($name, $value) * * @param string $name The name of the strategy to use. * @param mixed $value The value that should be converted. + * @param array $data The whole data is optionally provided as context. * @return mixed */ - public function hydrateValue($name, $value) + public function hydrateValue($name, $value, $data = null) { if ($this->hasStrategy($name)) { $strategy = $this->getStrategy($name); - $value = $strategy->hydrate($value); + $value = $strategy->hydrate($value, $data); } return $value; } diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index d29185fe6..a3904013b 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -142,7 +142,7 @@ public function extract($object) if ($this->underscoreSeparatedKeys) { $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); } - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); + $attributes[$attribute] = $this->extractValue($attribute, $object->$method(), $object); } return $attributes; @@ -178,7 +178,7 @@ public function hydrate(array $data, $object) $method = preg_replace_callback('/(_[a-z])/', $transform, $method); } if (method_exists($object, $method)) { - $value = $this->hydrateValue($property, $value); + $value = $this->hydrateValue($property, $value, $data); $object->$method($value); } diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index e212dc79a..acff0359a 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -36,7 +36,7 @@ public function extract($object) if (!$self->getFilter()->filter($name)) { unset($data[$name]); } else { - $value = $self->extractValue($name, $value); + $value = $self->extractValue($name, $value, $object); } }); return $data; @@ -60,7 +60,7 @@ public function hydrate(array $data, $object) )); } foreach ($data as $property => $value) { - $object->$property = $this->hydrateValue($property, $value); + $object->$property = $this->hydrateValue($property, $value, $data); } return $object; } diff --git a/src/Hydrator/Reflection.php b/src/Hydrator/Reflection.php index 6fe230204..1c093c88b 100644 --- a/src/Hydrator/Reflection.php +++ b/src/Hydrator/Reflection.php @@ -36,7 +36,7 @@ public function extract($object) } $value = $property->getValue($object); - $result[$propertyName] = $this->extractValue($propertyName, $value); + $result[$propertyName] = $this->extractValue($propertyName, $value, $object); } return $result; @@ -54,7 +54,7 @@ public function hydrate(array $data, $object) $reflProperties = self::getReflProperties($object); foreach ($data as $key => $value) { if (isset($reflProperties[$key])) { - $reflProperties[$key]->setValue($object, $this->hydrateValue($key, $value)); + $reflProperties[$key]->setValue($object, $this->hydrateValue($key, $value, $data)); } } return $object; diff --git a/test/HydratorStrategyTest.php b/test/HydratorStrategyTest.php index 0d0d05fd4..926cfbf3f 100644 --- a/test/HydratorStrategyTest.php +++ b/test/HydratorStrategyTest.php @@ -144,4 +144,28 @@ public function underscoreHandlingDataProvider() array(false, 'fooBar'), ); } + + public function testContextAwarenessExtract() + { + $strategy = new TestAsset\HydratorStrategyContextAware(); + $this->hydrator->addStrategy('field2', $strategy); + + $entityB = new TestAsset\HydratorStrategyEntityB('X', 'Y'); + $attributes = $this->hydrator->extract($entityB); + + $this->assertEquals($entityB, $strategy->object); + } + + public function testContextAwarenessHydrate() + { + $strategy = new TestAsset\HydratorStrategyContextAware(); + $this->hydrator->addStrategy('field2', $strategy); + + $entityB = new TestAsset\HydratorStrategyEntityB('X', 'Y'); + $data = array('field1' => 'A', 'field2' => 'B'); + $attributes = $this->hydrator->hydrate($data, $entityB); + + $this->assertEquals($data, $strategy->data); + } + } diff --git a/test/TestAsset/HydratorStrategyContextAware.php b/test/TestAsset/HydratorStrategyContextAware.php new file mode 100644 index 000000000..d879aff73 --- /dev/null +++ b/test/TestAsset/HydratorStrategyContextAware.php @@ -0,0 +1,31 @@ +object = $object; + return $value; + } + + public function hydrate($value, $data = null) + { + $this->data = $data; + return $value; + } +} From 663db4de5b72fe594abd2dc473c625f8d5bbe1de Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Fri, 1 Feb 2013 11:23:47 +0100 Subject: [PATCH 02/20] Remove package tag --- test/TestAsset/HydratorStrategyContextAware.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/TestAsset/HydratorStrategyContextAware.php b/test/TestAsset/HydratorStrategyContextAware.php index d879aff73..8400efd5a 100644 --- a/test/TestAsset/HydratorStrategyContextAware.php +++ b/test/TestAsset/HydratorStrategyContextAware.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Stdlib */ namespace ZendTest\Stdlib\TestAsset; From 6f6b6bc3d4a6e99042b96c5b938500a2b9d1c0a2 Mon Sep 17 00:00:00 2001 From: Gugu Mabuza Date: Sun, 17 Feb 2013 22:22:56 -0500 Subject: [PATCH 03/20] Update library/Zend/Stdlib/Hydrator/ClassMethods.php Formated according to formatting standars --- src/Hydrator/ClassMethods.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index bdbb053f1..6c30e231c 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -143,12 +143,12 @@ public function extract($object) } - if(property_exists($object, $attribute)) { - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); - } else { - $attribute = lcfirst($attribute); - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); - } + if(property_exists($object, $attribute)) { + $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); + } else { + $attribute = lcfirst($attribute); + $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); + } } return $attributes; From bdcb56bd2d7b62b960c705448613485e964b6576 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 19 Feb 2013 11:12:34 -0600 Subject: [PATCH 04/20] [zendframework/zf2#3580] Fix test failure - $object was not being passed to closure that used it --- src/Hydrator/ObjectProperty.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index acff0359a..fe36d8828 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -32,7 +32,7 @@ public function extract($object) $self = $this; $data = get_object_vars($object); - array_walk($data, function (&$value, $name) use ($self, &$data) { + array_walk($data, function (&$value, $name) use ($self, &$data, $object) { if (!$self->getFilter()->filter($name)) { unset($data[$name]); } else { From 3efb86f985dda79c4910facd513089dc92ac1b2e Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 12 Mar 2013 11:01:10 -0500 Subject: [PATCH 05/20] [zendframework/zf2#3951] Add error handler for deprecation warning - Added an error handler to catch the deprecation warning now emitted by Zend\Stdlib\DateTime --- test/DateTimeTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/DateTimeTest.php b/test/DateTimeTest.php index f6c2e0553..7f4cfa00b 100644 --- a/test/DateTimeTest.php +++ b/test/DateTimeTest.php @@ -22,6 +22,16 @@ class DateTimeTest extends \PHPUnit_Framework_TestCase { public $dateTime; + public function setUp() + { + set_error_handler(function ($errno, $errstr) { + if (!stristr($errstr, 'datetime extension deprecated')) { + return false; + } + return true; + }, E_USER_DEPRECATED); + } + public function testCreatesIS08601WithoutFractionalSeconds() { $time = '2009-03-07T08:03:50Z'; From dbd8eaf5aa5191e5f092346b73fcd686d578ae62 Mon Sep 17 00:00:00 2001 From: "Mabuza, Gugu" Date: Sat, 13 Apr 2013 21:38:53 -0400 Subject: [PATCH 06/20] Fixing classMethods Hydrator to work for TitleCase properties --- src/Hydrator/ClassMethods.php | 16 ++++------- test/HydratorTest.php | 54 ++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 6c30e231c..942133d14 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -136,19 +136,15 @@ public function extract($object) $attribute = $method; if (preg_match('/^get/', $method)) { $attribute = substr($method, 3); + if(!property_exists($object, $attribute)) { + $attribute = lcfirst($attribute); + } } if ($this->underscoreSeparatedKeys) { $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); - } - - - if(property_exists($object, $attribute)) { - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); - } else { - $attribute = lcfirst($attribute); - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); - } + } + $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); } return $attributes; @@ -193,4 +189,4 @@ public function hydrate(array $data, $object) return $object; } -} +} \ No newline at end of file diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 1c6938185..19e3b97c8 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -16,6 +16,7 @@ use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Stdlib\Hydrator\Filter\FilterComposite; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase; +use ZendTest\Stdlib\TestAsset\ClassMethodsTitleCase; use ZendTest\Stdlib\TestAsset\ClassMethodsFilterProviderInterface; use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing; @@ -41,6 +42,11 @@ class HydratorTest extends \PHPUnit_Framework_TestCase * @var ClassMethodsCamelCase */ protected $classMethodsCamelCase; + + /** + * @var ClassMethodsTitleCase + */ + protected $classMethodsTitleCase; /** * @var ClassMethodsCamelCaseMissing @@ -65,6 +71,7 @@ class HydratorTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->classMethodsCamelCase = new ClassMethodsCamelCase(); + $this->classMethodsTitleCase = new ClassMethodsTitleCase(); $this->classMethodsCamelCaseMissing = new ClassMethodsCamelCaseMissing(); $this->classMethodsUnderscore = new ClassMethodsUnderscore(); $this->classMethodsInvalidParameter = new ClassMethodsInvalidParameter(); @@ -79,7 +86,13 @@ public function testInitiateValues() $this->assertEquals($this->classMethodsCamelCase->getIsFoo(), true); $this->assertEquals($this->classMethodsCamelCase->isBar(), true); $this->assertEquals($this->classMethodsCamelCase->getHasFoo(), true); - $this->assertEquals($this->classMethodsCamelCase->hasBar(), true); + $this->assertEquals($this->classMethodsCamelCase->hasBar(), true); + $this->assertEquals($this->classMethodsTitleCase->getFooBar(), '1'); + $this->assertEquals($this->classMethodsTitleCase->getFooBarBaz(), '2'); + $this->assertEquals($this->classMethodsTitleCase->getIsFoo(), true); + $this->assertEquals($this->classMethodsTitleCase->getIsBar(), true); + $this->assertEquals($this->classMethodsTitleCase->getHasFoo(), true); + $this->assertEquals($this->classMethodsTitleCase->getHasBar(), true); $this->assertEquals($this->classMethodsUnderscore->getFooBar(), '1'); $this->assertEquals($this->classMethodsUnderscore->getFooBarBaz(), '2'); $this->assertEquals($this->classMethodsUnderscore->getIsFoo(), true); @@ -140,6 +153,45 @@ public function testHydratorClassMethodsCamelCase() $this->assertEquals($test->getHasFoo(), false); $this->assertEquals($test->hasBar(), false); } + + + + public function testHydratorClassMethodsTitleCase() + { + $hydrator = new ClassMethods(false); + $datas = $hydrator->extract($this->classMethodsTitleCase); + $this->assertTrue(isset($datas['FooBar'])); + $this->assertEquals($datas['FooBar'], '1'); + $this->assertTrue(isset($datas['FooBarBaz'])); + $this->assertFalse(isset($datas['foo_bar'])); + $this->assertTrue(isset($datas['IsFoo'])); + $this->assertEquals($datas['IsFoo'], true); + $this->assertTrue(isset($datas['IsBar'])); + $this->assertEquals($datas['IsBar'], true); + $this->assertTrue(isset($datas['HasFoo'])); + $this->assertEquals($datas['HasFoo'], true); + $this->assertTrue(isset($datas['HasBar'])); + $this->assertEquals($datas['HasBar'], true); + $test = $hydrator->hydrate( + array( + 'FooBar' => 'foo', + 'FooBarBaz' => 'bar', + 'IsFoo' => false, + 'IsBar' => false, + 'HasFoo' => false, + 'HasBar' => false, + ), + $this->classMethodsTitleCase + ); + $this->assertSame($this->classMethodsTitleCase, $test); + $this->assertEquals($test->getFooBar(), 'foo'); + $this->assertEquals($test->getFooBarBaz(), 'bar'); + $this->assertEquals($test->getIsFoo(), false); + $this->assertEquals($test->getIsBar(), false); + $this->assertEquals($test->getHasFoo(), false); + $this->assertEquals($test->getHasBar(), false); + } + public function testHydratorClassMethodsUnderscore() { From cbd155f880cabf240796718cd3df0e2d74c47ad7 Mon Sep 17 00:00:00 2001 From: "Mabuza, Gugu" Date: Sat, 13 Apr 2013 21:45:16 -0400 Subject: [PATCH 07/20] Added TestAssest for ClassMethods Hydrator --- test/TestAsset/ClassMethodsTitleCase.php | 100 +++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 test/TestAsset/ClassMethodsTitleCase.php diff --git a/test/TestAsset/ClassMethodsTitleCase.php b/test/TestAsset/ClassMethodsTitleCase.php new file mode 100644 index 000000000..8be4dedb7 --- /dev/null +++ b/test/TestAsset/ClassMethodsTitleCase.php @@ -0,0 +1,100 @@ +FooBar; + } + + public function setFooBar($value) + { + $this->FooBar = $value; + return $this; + } + + public function getFooBarBaz() + { + return $this->FooBarBaz; + } + + + public function setFooBarBaz($value) + { + $this->FooBarBaz = $value; + return $this; + } + + public function getIsFoo() + { + return $this->IsFoo; + } + + public function setIsFoo($IsFoo) + { + $this->IsFoo = $IsFoo; + return $this; + } + + + public function getIsBar() + { + return $this->IsBar; + } + + public function setIsBar($IsBar) + { + $this->IsBar = $IsBar; + return $this; + } + + + public function getHasFoo() + { + return $this->HasFoo; + } + + + public function getHasBar() + { + return $this->HasBar; + } + + + public function setHasFoo($HasFoo) + { + $this->HasFoo = $HasFoo; + return $this; + } + + + public function setHasBar($HasBar) + { + $this->HasBar = $HasBar; + } + + + +} From 0a561f5455cae4dbda3bb3ae5ebb8daff3d26147 Mon Sep 17 00:00:00 2001 From: wryck7 Date: Mon, 15 Apr 2013 07:12:31 -0400 Subject: [PATCH 08/20] Trimmed trailing whitespaces --- src/Hydrator/ClassMethods.php | 2 +- test/HydratorTest.php | 12 ++++---- test/TestAsset/ClassMethodsTitleCase.php | 38 ++++++++++++------------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 942133d14..4c5cceb7d 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -143,7 +143,7 @@ public function extract($object) if ($this->underscoreSeparatedKeys) { $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); - } + } $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); } diff --git a/test/HydratorTest.php b/test/HydratorTest.php index f27a0f774..7bde44768 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -42,7 +42,7 @@ class HydratorTest extends \PHPUnit_Framework_TestCase * @var ClassMethodsCamelCase */ protected $classMethodsCamelCase; - + /** * @var ClassMethodsTitleCase */ @@ -86,7 +86,7 @@ public function testInitiateValues() $this->assertEquals($this->classMethodsCamelCase->getIsFoo(), true); $this->assertEquals($this->classMethodsCamelCase->isBar(), true); $this->assertEquals($this->classMethodsCamelCase->getHasFoo(), true); - $this->assertEquals($this->classMethodsCamelCase->hasBar(), true); + $this->assertEquals($this->classMethodsCamelCase->hasBar(), true); $this->assertEquals($this->classMethodsTitleCase->getFooBar(), '1'); $this->assertEquals($this->classMethodsTitleCase->getFooBarBaz(), '2'); $this->assertEquals($this->classMethodsTitleCase->getIsFoo(), true); @@ -153,9 +153,9 @@ public function testHydratorClassMethodsCamelCase() $this->assertEquals($test->getHasFoo(), false); $this->assertEquals($test->hasBar(), false); } - - - + + + public function testHydratorClassMethodsTitleCase() { $hydrator = new ClassMethods(false); @@ -191,7 +191,7 @@ public function testHydratorClassMethodsTitleCase() $this->assertEquals($test->getHasFoo(), false); $this->assertEquals($test->getHasBar(), false); } - + public function testHydratorClassMethodsUnderscore() { diff --git a/test/TestAsset/ClassMethodsTitleCase.php b/test/TestAsset/ClassMethodsTitleCase.php index 8be4dedb7..3ca4191a1 100644 --- a/test/TestAsset/ClassMethodsTitleCase.php +++ b/test/TestAsset/ClassMethodsTitleCase.php @@ -23,8 +23,8 @@ class ClassMethodsTitleCase protected $HasFoo = true; protected $HasBar = true; - - public function getFooBar() + + public function getFooBar() { return $this->FooBar; } @@ -34,67 +34,67 @@ public function setFooBar($value) $this->FooBar = $value; return $this; } - + public function getFooBarBaz() { return $this->FooBarBaz; } - - + + public function setFooBarBaz($value) { $this->FooBarBaz = $value; return $this; } - public function getIsFoo() + public function getIsFoo() { return $this->IsFoo; } - + public function setIsFoo($IsFoo) { $this->IsFoo = $IsFoo; return $this; } - - public function getIsBar() + + public function getIsBar() { return $this->IsBar; } - + public function setIsBar($IsBar) { $this->IsBar = $IsBar; return $this; } - - public function getHasFoo() + + public function getHasFoo() { return $this->HasFoo; } - - public function getHasBar() + + public function getHasBar() { return $this->HasBar; } - - public function setHasFoo($HasFoo) + + public function setHasFoo($HasFoo) { $this->HasFoo = $HasFoo; return $this; } - - public function setHasBar($HasBar) + + public function setHasBar($HasBar) { $this->HasBar = $HasBar; } - + } From 8b7718948ac0243dfd5609a2fca12aa62168ca5c Mon Sep 17 00:00:00 2001 From: wryck7 Date: Mon, 15 Apr 2013 07:14:06 -0400 Subject: [PATCH 09/20] Converted identation from tabs to spaces --- src/Hydrator/ClassMethods.php | 2 +- test/HydratorTest.php | 64 +++++++------- test/TestAsset/ClassMethodsTitleCase.php | 106 +++++++++++------------ 3 files changed, 86 insertions(+), 86 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 4c5cceb7d..d188e7f92 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -137,7 +137,7 @@ public function extract($object) if (preg_match('/^get/', $method)) { $attribute = substr($method, 3); if(!property_exists($object, $attribute)) { - $attribute = lcfirst($attribute); + $attribute = lcfirst($attribute); } } diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 7bde44768..fe83f4833 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -158,38 +158,38 @@ public function testHydratorClassMethodsCamelCase() public function testHydratorClassMethodsTitleCase() { - $hydrator = new ClassMethods(false); - $datas = $hydrator->extract($this->classMethodsTitleCase); - $this->assertTrue(isset($datas['FooBar'])); - $this->assertEquals($datas['FooBar'], '1'); - $this->assertTrue(isset($datas['FooBarBaz'])); - $this->assertFalse(isset($datas['foo_bar'])); - $this->assertTrue(isset($datas['IsFoo'])); - $this->assertEquals($datas['IsFoo'], true); - $this->assertTrue(isset($datas['IsBar'])); - $this->assertEquals($datas['IsBar'], true); - $this->assertTrue(isset($datas['HasFoo'])); - $this->assertEquals($datas['HasFoo'], true); - $this->assertTrue(isset($datas['HasBar'])); - $this->assertEquals($datas['HasBar'], true); - $test = $hydrator->hydrate( - array( - 'FooBar' => 'foo', - 'FooBarBaz' => 'bar', - 'IsFoo' => false, - 'IsBar' => false, - 'HasFoo' => false, - 'HasBar' => false, - ), - $this->classMethodsTitleCase - ); - $this->assertSame($this->classMethodsTitleCase, $test); - $this->assertEquals($test->getFooBar(), 'foo'); - $this->assertEquals($test->getFooBarBaz(), 'bar'); - $this->assertEquals($test->getIsFoo(), false); - $this->assertEquals($test->getIsBar(), false); - $this->assertEquals($test->getHasFoo(), false); - $this->assertEquals($test->getHasBar(), false); + $hydrator = new ClassMethods(false); + $datas = $hydrator->extract($this->classMethodsTitleCase); + $this->assertTrue(isset($datas['FooBar'])); + $this->assertEquals($datas['FooBar'], '1'); + $this->assertTrue(isset($datas['FooBarBaz'])); + $this->assertFalse(isset($datas['foo_bar'])); + $this->assertTrue(isset($datas['IsFoo'])); + $this->assertEquals($datas['IsFoo'], true); + $this->assertTrue(isset($datas['IsBar'])); + $this->assertEquals($datas['IsBar'], true); + $this->assertTrue(isset($datas['HasFoo'])); + $this->assertEquals($datas['HasFoo'], true); + $this->assertTrue(isset($datas['HasBar'])); + $this->assertEquals($datas['HasBar'], true); + $test = $hydrator->hydrate( + array( + 'FooBar' => 'foo', + 'FooBarBaz' => 'bar', + 'IsFoo' => false, + 'IsBar' => false, + 'HasFoo' => false, + 'HasBar' => false, + ), + $this->classMethodsTitleCase + ); + $this->assertSame($this->classMethodsTitleCase, $test); + $this->assertEquals($test->getFooBar(), 'foo'); + $this->assertEquals($test->getFooBarBaz(), 'bar'); + $this->assertEquals($test->getIsFoo(), false); + $this->assertEquals($test->getIsBar(), false); + $this->assertEquals($test->getHasFoo(), false); + $this->assertEquals($test->getHasBar(), false); } diff --git a/test/TestAsset/ClassMethodsTitleCase.php b/test/TestAsset/ClassMethodsTitleCase.php index 3ca4191a1..d1326b95d 100644 --- a/test/TestAsset/ClassMethodsTitleCase.php +++ b/test/TestAsset/ClassMethodsTitleCase.php @@ -24,76 +24,76 @@ class ClassMethodsTitleCase protected $HasBar = true; - public function getFooBar() - { - return $this->FooBar; - } + public function getFooBar() + { + return $this->FooBar; + } - public function setFooBar($value) - { - $this->FooBar = $value; - return $this; - } + public function setFooBar($value) + { + $this->FooBar = $value; + return $this; + } - public function getFooBarBaz() - { - return $this->FooBarBaz; - } + public function getFooBarBaz() + { + return $this->FooBarBaz; + } - public function setFooBarBaz($value) - { - $this->FooBarBaz = $value; - return $this; - } + public function setFooBarBaz($value) + { + $this->FooBarBaz = $value; + return $this; + } - public function getIsFoo() - { - return $this->IsFoo; - } + public function getIsFoo() + { + return $this->IsFoo; + } - public function setIsFoo($IsFoo) - { - $this->IsFoo = $IsFoo; - return $this; - } + public function setIsFoo($IsFoo) + { + $this->IsFoo = $IsFoo; + return $this; + } - public function getIsBar() - { - return $this->IsBar; - } + public function getIsBar() + { + return $this->IsBar; + } - public function setIsBar($IsBar) - { - $this->IsBar = $IsBar; - return $this; - } + public function setIsBar($IsBar) + { + $this->IsBar = $IsBar; + return $this; + } - public function getHasFoo() - { - return $this->HasFoo; - } + public function getHasFoo() + { + return $this->HasFoo; + } - public function getHasBar() - { - return $this->HasBar; - } + public function getHasBar() + { + return $this->HasBar; + } - public function setHasFoo($HasFoo) - { - $this->HasFoo = $HasFoo; - return $this; - } + public function setHasFoo($HasFoo) + { + $this->HasFoo = $HasFoo; + return $this; + } - public function setHasBar($HasBar) - { - $this->HasBar = $HasBar; - } + public function setHasBar($HasBar) + { + $this->HasBar = $HasBar; + } From a242a20874f6e220f209d3fec8ca983413e99c1d Mon Sep 17 00:00:00 2001 From: wryck7 Date: Mon, 15 Apr 2013 07:20:39 -0400 Subject: [PATCH 10/20] Fixed CS issues --- src/Hydrator/ClassMethods.php | 3 +-- test/HydratorTest.php | 18 +++++++++--------- test/TestAsset/ClassMethodsTitleCase.php | 9 --------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index d188e7f92..de018a3c9 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -188,5 +188,4 @@ public function hydrate(array $data, $object) return $object; } - -} \ No newline at end of file +} diff --git a/test/HydratorTest.php b/test/HydratorTest.php index fe83f4833..c43cb7d9c 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -173,15 +173,15 @@ public function testHydratorClassMethodsTitleCase() $this->assertTrue(isset($datas['HasBar'])); $this->assertEquals($datas['HasBar'], true); $test = $hydrator->hydrate( - array( - 'FooBar' => 'foo', - 'FooBarBaz' => 'bar', - 'IsFoo' => false, - 'IsBar' => false, - 'HasFoo' => false, - 'HasBar' => false, - ), - $this->classMethodsTitleCase + array( + 'FooBar' => 'foo', + 'FooBarBaz' => 'bar', + 'IsFoo' => false, + 'IsBar' => false, + 'HasFoo' => false, + 'HasBar' => false, + ), + $this->classMethodsTitleCase ); $this->assertSame($this->classMethodsTitleCase, $test); $this->assertEquals($test->getFooBar(), 'foo'); diff --git a/test/TestAsset/ClassMethodsTitleCase.php b/test/TestAsset/ClassMethodsTitleCase.php index d1326b95d..8375e18a9 100644 --- a/test/TestAsset/ClassMethodsTitleCase.php +++ b/test/TestAsset/ClassMethodsTitleCase.php @@ -40,7 +40,6 @@ public function getFooBarBaz() return $this->FooBarBaz; } - public function setFooBarBaz($value) { $this->FooBarBaz = $value; @@ -58,7 +57,6 @@ public function setIsFoo($IsFoo) return $this; } - public function getIsBar() { return $this->IsBar; @@ -70,31 +68,24 @@ public function setIsBar($IsBar) return $this; } - public function getHasFoo() { return $this->HasFoo; } - public function getHasBar() { return $this->HasBar; } - public function setHasFoo($HasFoo) { $this->HasFoo = $HasFoo; return $this; } - public function setHasBar($HasBar) { $this->HasBar = $HasBar; } - - - } From c6d18e4d2a38e020b38db110832bf301e7ab045c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Thu, 25 Apr 2013 18:51:09 +0200 Subject: [PATCH 11/20] Add untracked files --- src/Hydrator/HydratorPluginManager.php | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Hydrator/HydratorPluginManager.php diff --git a/src/Hydrator/HydratorPluginManager.php b/src/Hydrator/HydratorPluginManager.php new file mode 100644 index 000000000..efb7ff647 --- /dev/null +++ b/src/Hydrator/HydratorPluginManager.php @@ -0,0 +1,49 @@ + 'Zend\Stdlib\Hydrator\ArraySerializable', + 'classmethods' => 'Zend\Stdlib\Hydrator\ClassMethods', + 'objectproperty' => 'Zend\Stdlib\Hydrator\ObjectProperty', + 'reflection' => 'Zend\Stdlib\Hydrator\Reflection' + ); + + /** + * {@inheritDoc} + */ + public function validatePlugin($plugin) + { + if ($plugin instanceof HydratorInterface) { + // we're okay + return; + } + + throw new Exception\RuntimeException(sprintf( + 'Plugin of type %s is invalid; must implement Zend\Stdlib\Hydrator\HydratorInterface', + (is_object($plugin) ? get_class($plugin) : gettype($plugin)) + )); + } +} From 59d42c588bec72529119ddc1fe8204a4f75b8552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Thu, 25 Apr 2013 22:26:11 +0200 Subject: [PATCH 12/20] Inject validator chain in input filter factory --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 10bc34828..f736431b5 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "suggest": { "zendframework/zend-eventmanager": "To support aggregate hydrator usage", "zendframework/zend-serializer": "Zend\\Serializer component", - "zendframework/zend-servicemanager": "To support hydrator plugin manager usage", + "zendframework/zend-servicemanager": "For using the hydrator plugin manager", "zendframework/zend-filter": "To support naming strategy hydrator usage", "pecl-weakref": "Implementation of weak references for Stdlib\\CallbackHandler" }, From 50fa97b380d5af2f3228f4715268d1ff601fa9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Fri, 26 Apr 2013 12:09:39 +0200 Subject: [PATCH 13/20] Fix CS --- composer.json | 2 +- test/Hydrator/HydratorManagerTest.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index f736431b5..10bc34828 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "suggest": { "zendframework/zend-eventmanager": "To support aggregate hydrator usage", "zendframework/zend-serializer": "Zend\\Serializer component", - "zendframework/zend-servicemanager": "For using the hydrator plugin manager", + "zendframework/zend-servicemanager": "To support hydrator plugin manager usage", "zendframework/zend-filter": "To support naming strategy hydrator usage", "pecl-weakref": "Implementation of weak references for Stdlib\\CallbackHandler" }, diff --git a/test/Hydrator/HydratorManagerTest.php b/test/Hydrator/HydratorManagerTest.php index 370d13cee..f5f74cb7d 100644 --- a/test/Hydrator/HydratorManagerTest.php +++ b/test/Hydrator/HydratorManagerTest.php @@ -15,8 +15,6 @@ /** * @category Zend - * @package Zend_Stdlib - * @subpackage UnitTests * @group Zend_Stdlib */ class HydratorManagerTest extends \PHPUnit_Framework_TestCase From 2fc1ee11cce9bfb7cfc854bfc26cfa6636b4ec13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Fri, 26 Apr 2013 22:27:58 +0200 Subject: [PATCH 14/20] Feedbacks --- test/Hydrator/HydratorManagerTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/Hydrator/HydratorManagerTest.php b/test/Hydrator/HydratorManagerTest.php index f5f74cb7d..a2272ed3f 100644 --- a/test/Hydrator/HydratorManagerTest.php +++ b/test/Hydrator/HydratorManagerTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Form */ namespace ZendTest\Stdlib\Hydrator; @@ -14,8 +13,7 @@ use Zend\Stdlib\Hydrator\HydratorPluginManager; /** - * @category Zend - * @group Zend_Stdlib + * @group Zend_Stdlib */ class HydratorManagerTest extends \PHPUnit_Framework_TestCase { From f1ccaa9121340b6227c9407483c559dc55b2fbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Fri, 26 Apr 2013 22:45:06 +0200 Subject: [PATCH 15/20] Set hydrators not shared by default --- src/Hydrator/HydratorPluginManager.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Hydrator/HydratorPluginManager.php b/src/Hydrator/HydratorPluginManager.php index efb7ff647..0e9892a25 100644 --- a/src/Hydrator/HydratorPluginManager.php +++ b/src/Hydrator/HydratorPluginManager.php @@ -19,6 +19,13 @@ */ class HydratorPluginManager extends AbstractPluginManager { + /** + * Whether or not to share by default + * + * @var bool + */ + protected $shareByDefault = false; + /** * Default set of adapters * From 1ddc7e07a5c8d291f466055807f64ce1b120f9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 28 Apr 2013 12:49:12 +0200 Subject: [PATCH 16/20] Docblock doesn't match --- src/ArrayObject/PhpLegacyCompatibility.php | 7 +++---- src/ArrayObject/PhpReferenceCompatibility.php | 7 +++---- src/Hydrator/AbstractHydrator.php | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ArrayObject/PhpLegacyCompatibility.php b/src/ArrayObject/PhpLegacyCompatibility.php index 4d0f44d47..9a71605e7 100644 --- a/src/ArrayObject/PhpLegacyCompatibility.php +++ b/src/ArrayObject/PhpLegacyCompatibility.php @@ -24,10 +24,9 @@ abstract class PhpLegacyCompatibility extends PhpArrayObject /** * Constructor * - * @param array $input - * @param int $flags - * @param string $iteratorClass - * @return ArrayObject + * @param array $input + * @param int $flags + * @param string $iteratorClass */ public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator') { diff --git a/src/ArrayObject/PhpReferenceCompatibility.php b/src/ArrayObject/PhpReferenceCompatibility.php index 117543995..e25257c76 100644 --- a/src/ArrayObject/PhpReferenceCompatibility.php +++ b/src/ArrayObject/PhpReferenceCompatibility.php @@ -59,10 +59,9 @@ abstract class PhpReferenceCompatibility implements IteratorAggregate, ArrayAcce /** * Constructor * - * @param array $input - * @param int $flags - * @param string $iteratorClass - * @return ArrayObject + * @param array $input + * @param int $flags + * @param string $iteratorClass */ public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator') { diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 6e4e4d219..4cb9a4356 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -102,9 +102,9 @@ public function removeStrategy($name) /** * Converts a value for extraction. If no strategy exists the plain value is returned. * - * @param string $name The name of the strategy to use. - * @param mixed $value The value that should be converted. - * @param array $data The object is optionally provided as context. + * @param string $name The name of the strategy to use. + * @param mixed $value The value that should be converted. + * @param array $object The object is optionally provided as context. * @return mixed */ public function extractValue($name, $value, $object = null) From d20256c16e6ce73c5ba054ee08980024b9580501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 28 Apr 2013 19:35:44 +0200 Subject: [PATCH 17/20] Alphabetically order use statements (related to zendframework/zf2#4338) --- src/Hydrator/ClassMethods.php | 2 +- src/Hydrator/Filter/NumberOfParameterFilter.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index c50d821d0..7f1952f70 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -15,10 +15,10 @@ use Zend\Stdlib\ArrayUtils; use Zend\Stdlib\Hydrator\Filter\FilterComposite; use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface; -use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter; use Zend\Stdlib\Hydrator\Filter\GetFilter; use Zend\Stdlib\Hydrator\Filter\HasFilter; use Zend\Stdlib\Hydrator\Filter\IsFilter; +use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter; use Zend\Stdlib\Hydrator\Filter\NumberOfParameterFilter; class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface diff --git a/src/Hydrator/Filter/NumberOfParameterFilter.php b/src/Hydrator/Filter/NumberOfParameterFilter.php index c9e2dd5a3..e3b8c1c9f 100644 --- a/src/Hydrator/Filter/NumberOfParameterFilter.php +++ b/src/Hydrator/Filter/NumberOfParameterFilter.php @@ -9,8 +9,8 @@ namespace Zend\Stdlib\Hydrator\Filter; -use ReflectionMethod; use ReflectionException; +use ReflectionMethod; use Zend\Stdlib\Exception\InvalidArgumentException; use Zend\Stdlib\Hydrator\Filter\FilterInterface; From e144cd0bd6bf034103a9945de8a1588a89a4a2ed Mon Sep 17 00:00:00 2001 From: Nicolas Eeckeloo Date: Mon, 29 Apr 2013 08:41:10 +0200 Subject: [PATCH 18/20] Fix doc blocks consistency --- src/Glob.php | 12 ++++++------ src/StringWrapper/AbstractStringWrapper.php | 6 +++--- src/StringWrapper/StringWrapperInterface.php | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Glob.php b/src/Glob.php index b40f0d9a2..cc927d6d4 100644 --- a/src/Glob.php +++ b/src/Glob.php @@ -31,7 +31,7 @@ abstract class Glob * * @see http://docs.php.net/glob * @param string $pattern - * @param integer $flags + * @param int $flags * @param bool $forceFallback * @return array|false */ @@ -48,7 +48,7 @@ public static function glob($pattern, $flags, $forceFallback = false) * Use the glob function provided by the system. * * @param string $pattern - * @param integer $flags + * @param int $flags * @return array|false */ protected static function systemGlob($pattern, $flags) @@ -82,7 +82,7 @@ protected static function systemGlob($pattern, $flags) * Expand braces manually, then use the system glob. * * @param string $pattern - * @param integer $flags + * @param int $flags * @return array|false */ protected static function fallbackGlob($pattern, $flags) @@ -162,9 +162,9 @@ protected static function fallbackGlob($pattern, $flags) * Find the end of the sub-pattern in a brace expression. * * @param string $pattern - * @param integer $begin - * @param integer $flags - * @return integer|null + * @param int $begin + * @param int $flags + * @return int|null */ protected static function nextBraceSub($pattern, $begin, $flags) { diff --git a/src/StringWrapper/AbstractStringWrapper.php b/src/StringWrapper/AbstractStringWrapper.php index 38a4d540d..e22649e7f 100644 --- a/src/StringWrapper/AbstractStringWrapper.php +++ b/src/StringWrapper/AbstractStringWrapper.php @@ -140,7 +140,7 @@ public function convert($str, $reverse = false) * Wraps a string to a given number of characters * * @param string $string - * @param integer $width + * @param int $width * @param string $break * @param bool $cut * @return string|false @@ -221,9 +221,9 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false) * Pad a string to a certain length with another string * * @param string $input - * @param integer $padLength + * @param int $padLength * @param string $padString - * @param integer $padType + * @param int $padType * @return string */ public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT) diff --git a/src/StringWrapper/StringWrapperInterface.php b/src/StringWrapper/StringWrapperInterface.php index a6a79bfef..974b0be48 100644 --- a/src/StringWrapper/StringWrapperInterface.php +++ b/src/StringWrapper/StringWrapperInterface.php @@ -91,7 +91,7 @@ public function convert($str, $reverse = false); * Wraps a string to a given number of characters * * @param string $str - * @param integer $width + * @param int $width * @param string $break * @param bool $cut * @return string @@ -102,9 +102,9 @@ public function wordWrap($str, $width = 75, $break = "\n", $cut = false); * Pad a string to a certain length with another string * * @param string $input - * @param integer $padLength + * @param int $padLength * @param string $padString - * @param integer $padType + * @param int $padType * @return string */ public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT); From 2bf51249053c0edbbdc005c18172e4652c94ed7d Mon Sep 17 00:00:00 2001 From: Nicolas Eeckeloo Date: Mon, 29 Apr 2013 09:38:13 +0200 Subject: [PATCH 19/20] Fix coding standards PSR-2 --- src/ArrayUtils.php | 4 ++-- src/ErrorHandler.php | 2 +- src/Hydrator/ClassMethods.php | 2 +- src/Hydrator/Filter/FilterComposite.php | 6 +++--- src/Hydrator/Filter/NumberOfParameterFilter.php | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ArrayUtils.php b/src/ArrayUtils.php index b073f3b45..4261d2e65 100644 --- a/src/ArrayUtils.php +++ b/src/ArrayUtils.php @@ -93,11 +93,11 @@ public static function hasNumericKeys($value, $allowEmpty = false) * * For example: * - * $list = array( 'a','b','c','d' ); + * $list = array('a', 'b', 'c', 'd'); * $list = array( * 0 => 'foo', * 1 => 'bar', - * 2 => array( 'foo' => 'baz' ), + * 2 => array('foo' => 'baz'), * ); * * diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 97b4e95d6..5096f53d9 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -109,7 +109,7 @@ public static function clean() */ public static function addError($errno, $errstr = '', $errfile = '', $errline = 0) { - $stack = & static::$stack[ count(static::$stack) - 1 ]; + $stack = & static::$stack[count(static::$stack) - 1]; $stack = new ErrorException($errstr, 0, $errno, $errfile, $errline, $stack); } } diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index c50d821d0..6cbb92557 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -136,7 +136,7 @@ public function extract($object) $attribute = $method; if (preg_match('/^get/', $method)) { $attribute = substr($method, 3); - if(!property_exists($object, $attribute)) { + if (!property_exists($object, $attribute)) { $attribute = lcfirst($attribute); } } diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index 179e209c3..f5ce7e9c8 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -96,7 +96,7 @@ function($value, $key) { */ public function addFilter($name, $filter, $condition = self::CONDITION_OR) { - if ( !is_callable($filter) && !($filter instanceof FilterInterface) ) { + if (!is_callable($filter) && !($filter instanceof FilterInterface)) { throw new InvalidArgumentException( 'The value of ' . $name . ' should be either a callable or ' . 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' @@ -166,13 +166,13 @@ public function filter($property) // Check if 1 from the or filters return true foreach ($this->orFilter as $filter) { if (is_callable($filter)) { - if ( $filter($property) === true) { + if ($filter($property) === true) { $returnValue = true; break; } continue; } else { - if ( $filter->filter($property) === true) { + if ($filter->filter($property) === true) { $returnValue = true; break; } diff --git a/src/Hydrator/Filter/NumberOfParameterFilter.php b/src/Hydrator/Filter/NumberOfParameterFilter.php index c9e2dd5a3..428d57dad 100644 --- a/src/Hydrator/Filter/NumberOfParameterFilter.php +++ b/src/Hydrator/Filter/NumberOfParameterFilter.php @@ -39,7 +39,7 @@ public function filter($property) { try { $reflectionMethod = new ReflectionMethod($property); - } catch( ReflectionException $exception) { + } catch (ReflectionException $exception) { throw new InvalidArgumentException( "Method $property doesn't exist" ); From 11415da3c939d560e3f6f229b2e14ebf733d7d30 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 29 Apr 2013 13:32:02 +0200 Subject: [PATCH 20/20] Glob::glob() should throw an exception on error --- src/Glob.php | 22 +++++++++++++++++----- test/GlobTest.php | 9 +++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Glob.php b/src/Glob.php index b40f0d9a2..714265f70 100644 --- a/src/Glob.php +++ b/src/Glob.php @@ -9,6 +9,9 @@ namespace Zend\Stdlib; +use Zend\Stdlib\Exception; +use Zend\Stdlib\ErrorHandler; + /** * Wrapper for glob with fallback if GLOB_BRACE is not available. */ @@ -33,9 +36,10 @@ abstract class Glob * @param string $pattern * @param integer $flags * @param bool $forceFallback - * @return array|false + * @return array + * @throws Exception\RuntimeException */ - public static function glob($pattern, $flags, $forceFallback = false) + public static function glob($pattern, $flags = 0, $forceFallback = false) { if (!defined('GLOB_BRACE') || $forceFallback) { return static::fallbackGlob($pattern, $flags); @@ -49,7 +53,8 @@ public static function glob($pattern, $flags, $forceFallback = false) * * @param string $pattern * @param integer $flags - * @return array|false + * @return array + * @throws Exception\RuntimeException */ protected static function systemGlob($pattern, $flags) { @@ -75,7 +80,13 @@ protected static function systemGlob($pattern, $flags) $globFlags = 0; } - return glob($pattern, $globFlags); + ErrorHandler::start(); + $res = glob($pattern, $globFlags); + $err = ErrorHandler::stop(); + if ($res === false) { + throw new Exception\RuntimeException("glob('{$pattern}', {$globFlags}) failed", 0, $err); + } + return $res; } /** @@ -83,7 +94,8 @@ protected static function systemGlob($pattern, $flags) * * @param string $pattern * @param integer $flags - * @return array|false + * @return array + * @throws Exception\RuntimeException */ protected static function fallbackGlob($pattern, $flags) { diff --git a/test/GlobTest.php b/test/GlobTest.php index de269d86e..996c80e2e 100644 --- a/test/GlobTest.php +++ b/test/GlobTest.php @@ -32,4 +32,13 @@ public function testNonMatchingGlobReturnsArray() $result = Glob::glob('/some/path/{,*.}{this,orthis}.php', Glob::GLOB_BRACE); $this->assertInternalType('array', $result); } + + public function testThrowExceptionOnError() + { + $this->setExpectedException('Zend\Stdlib\Exception\RuntimeException'); + + // run into a max path lengh error + $path = '/' . str_repeat('a', 10000); + Glob::glob($path); + } }