Permalink
Browse files

Merge branch 'feature/3359' into develop

Close #3359
  • Loading branch information...
2 parents a499ac5 + 7139cd7 commit f35275a0f570760d7b17f242e973c99e3b383829 @weierophinney weierophinney committed Jan 14, 2013
@@ -12,6 +12,7 @@
use ArrayObject;
use Zend\Stdlib\Exception;
+use Zend\Stdlib\Hydrator\Filter\FilterComposite;
use Zend\Stdlib\Hydrator\StrategyEnabledInterface;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
@@ -30,11 +31,18 @@
protected $strategies;
/**
+ * Composite to filter the methods, that need to be hydrated
+ * @var Filter\FilterComposite
+ */
+ protected $filterComposite;
+
+ /**
* Initializes a new instance of this class.
*/
public function __construct()
{
$this->strategies = new ArrayObject();
+ $this->filterComposite = new FilterComposite();
}
/**
@@ -128,4 +136,67 @@ public function hydrateValue($name, $value)
}
return $value;
}
+
+ /**
+ * Get the filter instance
+ *
+ * @return Filter\FilterComposite
+ */
+ public function getFilter()
+ {
+ return $this->filterComposite;
+ }
+
+ /**
+ * Add a new filter to take care of what needs to be hydrated.
+ * To exclude e.g. the method getServiceLocator:
+ *
+ * <code>
+ * $composite->addFilter("servicelocator",
+ * function($property) {
+ * list($class, $method) = explode('::', $property);
+ * if ($method === 'getServiceLocator') {
+ * return false;
+ * }
+ * return true;
+ * }, FilterComposite::CONDITION_AND
+ * );
+ * </code>
+ *
+ * @param string $name Index in the composite
+ * @param callable|Zend\Stdlib\Hydrator\Filter\FilterInterface $filter
+ * @param int $condition
+ * @return Filter\FilterComposite
+ */
+ public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
+ {
+ return $this->filterComposite->addFilter($name, $filter, $condition);
+ }
+
+ /**
+ * Check whether a specific filter exists at key $name or not
+ *
+ * @param string $name Index in the composite
+ * @return bool
+ */
+ public function hasFilter($name)
+ {
+ return $this->filterComposite->hasFilter($name);
+ }
+
+ /**
+ * Remove a filter from the composition.
+ * To not extract "has" methods, you simply need to unregister it
+ *
+ * <code>
+ * $filterComposite->removeFilter('has');
+ * </code>
+ *
+ * @param $name
+ * @return Filter\FilterComposite
+ */
+ public function removeFilter($name)
+ {
+ return $this->filterComposite->removeFilter($name);
+ }
}
@@ -39,9 +39,14 @@ public function extract($object)
$self = $this;
$data = $object->getArrayCopy();
- array_walk($data, function (&$value, $name) use ($self) {
- $value = $self->extractValue($name, $value);
+ array_walk($data, function (&$value, $name) use ($self, &$data) {
+ if (!$self->getFilter()->filter($name)) {
+ unset($data[$name]);
+ } else {
+ $value = $self->extractValue($name, $value);
+ }
});
+
return $data;
}
@@ -12,6 +12,13 @@
use ReflectionMethod;
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;
+use Zend\Stdlib\Hydrator\Filter\NumberOfParameterFilter;
/**
* @category Zend
@@ -34,10 +41,15 @@ public function __construct($underscoreSeparatedKeys = true)
{
parent::__construct();
$this->setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
+
+ $this->filterComposite->addFilter("is", new IsFilter());
+ $this->filterComposite->addFilter("has", new HasFilter());
+ $this->filterComposite->addFilter("get", new GetFilter());
+ $this->filterComposite->addFilter("parameter", new NumberOfParameterFilter(), FilterComposite::CONDITION_AND);
}
/**
- * @param array|\Traversable $options
+ * @param array|\Traversable $options
* @return ClassMethods
* @throws Exception\InvalidArgumentException
*/
@@ -58,12 +70,13 @@ public function setOptions($options)
}
/**
- * @param boolean $underscoreSeparatedKeys
+ * @param boolean $underscoreSeparatedKeys
* @return ClassMethods
*/
public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
{
$this->underscoreSeparatedKeys = $underscoreSeparatedKeys;
+
return $this;
}
@@ -80,7 +93,7 @@ public function getUnderscoreSeparatedKeys()
*
* Extracts the getter/setter of the given $object.
*
- * @param object $object
+ * @param object $object
* @return array
* @throws Exception\BadMethodCallException for a non-object $object
*/
@@ -92,15 +105,30 @@ public function extract($object)
));
}
+ $filter = null;
+ if ($object instanceof FilterProviderInterface) {
+ $filter = new FilterComposite(
+ array($object->getFilter()),
+ array(new MethodMatchFilter("getFilter"))
+ );
+ } else {
+ $filter = $this->filterComposite;
+ }
+
$transform = function ($letters) {
$letter = array_shift($letters);
+
return '_' . strtolower($letter);
};
$attributes = array();
$methods = get_class_methods($object);
foreach ($methods as $method) {
- if (!preg_match('/^(get|has|is)[A-Z]\w*/', $method)) {
+ if (
+ !$filter->filter(
+ get_class($object) . '::' . $method
+ )
+ ) {
continue;
}
@@ -129,8 +157,8 @@ public function extract($object)
*
* Hydrates an object by getter/setter methods of the object.
*
- * @param array $data
- * @param object $object
+ * @param array $data
+ * @param object $object
* @return object
* @throws Exception\BadMethodCallException for a non-object $object
*/
@@ -144,6 +172,7 @@ public function hydrate(array $data, $object)
$transform = function ($letters) {
$letter = substr(array_shift($letters), 1, 1);
+
return ucfirst($letter);
};
@@ -158,6 +187,8 @@ public function hydrate(array $data, $object)
$object->$method($value);
}
}
+
return $object;
}
+
}
Oops, something went wrong.

0 comments on commit f35275a

Please sign in to comment.