Permalink
Browse files

Implemented FilterProviderInterface and MethodMatchFilter

  • Loading branch information...
1 parent fe0db12 commit ec3e41e54751f822908f6e45c3c6e2708732ec8c @iwalz committed Jan 11, 2013
@@ -12,6 +12,8 @@
use Zend\Stdlib\Exception;
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;
@@ -101,6 +103,21 @@ public function extract($object)
));
}
+ $filter = null;
+ if ($object instanceof FilterProviderInterface) {
+ $filter = $object->getFilter();
+ if ($filter instanceof FilterComposite)
+ {
+ $filter->addFilter(
+ "getfilter",
+ new MethodMatchFilter("getFilter"),
+ FilterComposite::CONDITION_AND
+ );
+ }
+ } else {
+ $filter = $this->filterComposite;
+ }
+
$transform = function ($letters) {
$letter = array_shift($letters);
return '_' . strtolower($letter);
@@ -110,7 +127,7 @@ public function extract($object)
foreach ($methods as $method) {
if (
- !$this->filterComposite->filter(
+ !$filter->filter(
get_class($object) . '::' . $method
)
) {
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Service
+ */
+namespace Zend\Stdlib\Hydrator\Filter;
+
+/**
+ * @category Zend
+ * @package Zend_Stdlib
+ * @subpackage Hydrator
+ */
+interface FilterProviderInterface
+{
+ /**
+ * Provides a filter for hydration
+ *
+ * @return FilterInterface
+ */
+ public function getFilter();
+}
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Service
+ */
+namespace Zend\Stdlib\Hydrator\Filter;
+
+/**
+ * @category Zend
+ * @package Zend_Stdlib
+ * @subpackage Hydrator
+ */
+class MethodMatchFilter implements FilterInterface
+{
+ /**
+ * The method to exclude
+ * @var string
+ */
+ protected $method = null;
+
+ /**
+ * Either an exclude or an include
+ * @var bool
+ */
+ protected $exclude = null;
+
+ /**
+ * @param string $method The method to exclude
+ */
+ public function __construct($method, $exclude = true)
+ {
+ $this->method = $method;
+ $this->exclude = $exclude;
+ }
+
+ public function filter($property)
+ {
+ $pos = strpos($property, '::');
+ if ($pos !== false) {
+ $pos += 2;
+ } else {
+ $pos = 0;
+ }
+ if (substr($property, $pos) === $this->method) {
+ return $this->exclude ? false : true;
+ }
+ return $this->exclude ? true : false;
+ }
+}
@@ -16,6 +16,7 @@
use Zend\Stdlib\Hydrator\ArraySerializable;
use Zend\Stdlib\Hydrator\Filter\FilterComposite;
use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase;
+use ZendTest\Stdlib\TestAsset\ClassMethodsFilterProviderInterface;
use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore;
use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing;
use ZendTest\Stdlib\TestAsset\ClassMethodsInvalidParameter;
@@ -385,4 +386,14 @@ public function testHydratorClassMethodsWithInvalidNumberOfParameters()
$this->assertEquals('Bar', $datas['foo']);
$this->assertFalse($datas['isBla']);
}
+
+ public function testObjectBasedFilters()
+ {
+ $hydrator = new ClassMethods(false);
+ $foo = new ClassMethodsFilterProviderInterface();
+ $data = $hydrator->extract($foo);
+ $this->assertFalse(array_key_exists("filter", $data));
+ $this->assertSame("bar", $data["foo"]);
+ $this->assertSame("foo", $data["bar"]);
+ }
}
@@ -0,0 +1,70 @@
+<?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;
+
+use Zend\Stdlib\Hydrator\Filter\FilterComposite;
+use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface;
+use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter;
+use Zend\Stdlib\Hydrator\Filter\GetFilter;
+
+class ClassMethodsFilterProviderInterface implements FilterProviderInterface
+{
+ public function getBar()
+ {
+ return "foo";
+ }
+
+ public function getFoo()
+ {
+ return "bar";
+ }
+
+ public function isScalar($foo)
+ {
+ return false;
+ }
+
+ public function hasFooBar()
+ {
+ return true;
+ }
+
+ public function getServiceManager()
+ {
+ return "servicemanager";
+ }
+
+ public function getEventManager()
+ {
+ return "eventmanager";
+ }
+
+ public function getFilter()
+ {
+ $filterComposite = new FilterComposite();
+
+ $filterComposite->addFilter("get", new GetFilter());
+ $excludes = new FilterComposite();
+ $excludes->addFilter(
+ "servicemanager",
+ new MethodMatchFilter("getServiceManager"),
+ FilterComposite::CONDITION_AND
+ );
+ $excludes->addFilter(
+ "eventmanager",
+ new MethodMatchFilter("getEventManager"),
+ FilterComposite::CONDITION_AND
+ );
+ $filterComposite->addFilter("excludes", $excludes, FilterComposite::CONDITION_AND);
+
+ return $filterComposite;
+ }
+}

0 comments on commit ec3e41e

Please sign in to comment.