Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ClassMethods hydrator and wrong method definition #3352

Closed
wants to merge 2 commits into from

2 participants

@iwalz

I've noticed, that the ClassMethods hydrator will cause a fatal php error, if the function definition for has/get/is - method (no parameter) does not match. I know the conventions, but sometimes you may need a parameter (see https://github.com/zendframework/zf2/blob/master/library/Zend/ServiceManager/ServiceManager.php#L646 ).

This will simply skip the method, if the number of parameters is > 0.

P.S.: Are you interested in a hydrator, that does the same as the ClassMethods recursivly? I need the hydrator, to transform my objects to complete-non-object datatypes (for phing in my case). Is that a valid use-case for you?

@iwalz

Since we have the reflection, we can use it on line 81 that way:

$attributes[$attribute] = $this->extractValue($attribute, $reflectionMethod->invoke($object));

But I'm not sure about a performance impact/improvement/matter here.

@iwalz iwalz referenced this pull request
Merged

Added filter possibility #3359

@iwalz iwalz referenced this pull request from a commit in iwalz/zf2
@iwalz iwalz Added number of parameter filter. Fixed #3352 fe0db12
@weierophinney weierophinney commented on the diff
library/Zend/Stdlib/Hydrator/ClassMethods.php
@@ -64,6 +64,11 @@ public function extract($object)
continue;
}
+ $reflectionMethod = new \ReflectionMethod(get_class($object) . '::' . $method);
@weierophinney Owner

ReflectionMethod should be imported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ghost Unknown referenced this pull request from a commit
@iwalz iwalz Added number of parameter filter. Fixed #3352 82dac94
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#3352] Import ReflectionMethod aa49008
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3352'
Close #3352
2529b98
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3352' into develop
Forward port #3352
7c9f3ee
@gianarb gianarb referenced this pull request from a commit in zendframework/zend-stdlib
@iwalz iwalz Added number of parameter filter. Fixed zendframework/zf2#3352 e822d1f
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-stdlib
@weierophinney weierophinney [zendframework/zf2#3352] Import ReflectionMethod a385e2a
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-stdlib
@weierophinney weierophinney Merge branch 'hotfix/3352' c8fb359
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-stdlib
@weierophinney weierophinney Merge branch 'hotfix/3352' into develop 0eef13a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 5, 2013
  1. @iwalz
  2. @iwalz

    Added tests

    iwalz authored
This page is out of date. Refresh to see the latest.
View
5 library/Zend/Stdlib/Hydrator/ClassMethods.php
@@ -64,6 +64,11 @@ public function extract($object)
continue;
}
+ $reflectionMethod = new \ReflectionMethod(get_class($object) . '::' . $method);
@weierophinney Owner

ReflectionMethod should be imported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if ($reflectionMethod->getNumberOfParameters() > 0) {
+ continue;
+ }
+
$attribute = $method;
if (preg_match('/^get/', $method)) {
$attribute = substr($method, 3);
View
17 tests/ZendTest/Stdlib/HydratorTest.php
@@ -15,6 +15,7 @@
use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase;
use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore;
use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing;
+use ZendTest\Stdlib\TestAsset\ClassMethodsInvalidParameter;
use ZendTest\Stdlib\TestAsset\Reflection as ReflectionAsset;
/**
@@ -42,6 +43,11 @@ class HydratorTest extends \PHPUnit_Framework_TestCase
protected $classMethodsUnderscore;
/**
+ * @var ClassMethodsInvalidParameter
+ */
+ protected $classMethodsInvalidParameter;
+
+ /**
* @var ReflectionAsset
*/
protected $reflection;
@@ -51,6 +57,7 @@ public function setUp()
$this->classMethodsCamelCase = new ClassMethodsCamelCase();
$this->classMethodsCamelCaseMissing = new ClassMethodsCamelCaseMissing();
$this->classMethodsUnderscore = new ClassMethodsUnderscore();
+ $this->classMethodsInvalidParameter = new ClassMethodsInvalidParameter();
$this->reflection = new ReflectionAsset;
}
@@ -203,4 +210,14 @@ public function testHydratorClassMethodsCamelCaseWithSetterMissing()
$this->assertEquals($test->getFooBar(), 'foo');
$this->assertEquals($test->getFooBarBaz(), '2');
}
+
+ public function testHydratorClassMethodsWithServiceManager()
+ {
+ $hydrator = new ClassMethods(false);
+ $datas = $hydrator->extract($this->classMethodsInvalidParameter);
+
+ $this->assertTrue($datas['hasBar']);
+ $this->assertEquals('Bar', $datas['foo']);
+ $this->assertFalse($datas['isBla']);
+ }
}
View
43 tests/ZendTest/Stdlib/TestAsset/ClassMethodsInvalidParameter.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @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;
+
+class ClassMethodsInvalidParameter
+{
+ public function hasAlias($alias)
+ {
+ return $alias;
+ }
+
+ public function getTest($foo)
+ {
+ return $foo;
+ }
+
+ public function isTest($bar)
+ {
+ return $bar;
+ }
+
+ public function hasBar()
+ {
+ return true;
+ }
+
+ public function getFoo()
+ {
+ return "Bar";
+ }
+
+ public function isBla()
+ {
+ return false;
+ }
+}
Something went wrong with that request. Please try again.