Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/3359' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 14, 2013
179 parents 0e8bebe + 9d80a9f + efcb00e + 0a0842f + b6d0c88 + 7edee62 + 60ea64c + a08bcca + b40ec3e + 63172ed + 448f428 + 92a516a + 5ecbc99 + a2df21b + 4de87f2 + 7c259ec + a22bdcb + 084ad9f + 9414e5a + 489be93 + cb39e7e + 54a28dc + c9c769e + dda791d + 70d382a + 8bbad0e + 9321185 + 7ab35a6 + b93694e + 3ea7087 + 0fe3d3a + bd5e189 + d1cba17 + 8d75392 + 3fb5b55 + 6cb0ccb + 30aa565 + 8409977 + 8074ba0 + 8f92486 + 94860d1 + 05d33c4 + 425826b + f0e91f0 + e31468f + 7d2af87 + 2e4dc80 + 19d128f + 1b9e4b2 + 1c46483 + fdda3f2 + 595fcd1 + 213395c + 8e514a8 + 2f30186 + bb4ed65 + 132d5b6 + 030ff33 + f2f20f3 + a50e133 + 4c554ee + dbfb1b8 + ccab83f + 00b350f + 78945d0 + f0e5f4b + ceb7d8c + 9e124d1 + 3de5912 + b6a974a + 10a6438 + cb6c1e7 + 18afd6c + 3baf1bd + c800904 + f52dcb8 + 126ccb2 + e7d6206 + e2d24ab + ec1abfc + 290ea90 + 9f4ca1b + edaa760 + c4c0c95 + d21f055 + 5b18029 + e6b97af + 010fb36 + 64c7b8d + 636523e + 4cc2cd6 + e34098a + 16367cd + 943c77f + 8226e5b + 0b47726 + 3cd8a03 + cc4782c + 9c653a6 + 656dbe5 + 9bce1ba + 7dc18ca + 861130d + 2d2ffbd + 4f413a5 + 2e1067a + 1d082e4 + e8aeb79 + b562091 + ff2fdc3 + 4aa72c0 + 1bb67ac + cd015c8 + 5e89910 + 0c21258 + dd54faf + 57f9063 + b88ce2e + af68643 + 06cd3b4 + 2c71b71 + ee02c35 + 9456314 + 5a77a7b + e98a077 + 738f2e6 + cb1e63c + 736df07 + d0a0154 + 990523c + 78687de + a5b6e79 + 6e9dfe9 + e201a1c + d9b45ef + 76222ad + 16d67da + 1ab2258 + b81d711 + ed2e9bc + 61efe82 + f353ea5 + 1f02519 + 58c1fe8 + ed502d9 + 2defba6 + 4885013 + 06a8384 + 17d9eed + 3b21b5d + c62101c + 909ef34 + 13d376a + 8a75367 + 98a3cf5 + 89d1d50 + f2ce9f7 + 2eb75ba + f1c4b1b + b67f270 + ff615a6 + 5cf3c74 + 8c883f5 + 3f0ab11 + ba47a31 + ede9ab9 + daaff96 + bb8e9d5 + 0fc5628 + 270f2c4 + b0e848a + 1112202 + c8fb359 + 8d37cd0 + 136dbdd + 4d868a7 + 9506764 + 555cb91 + 53db121 commit 1c3fd99
Show file tree
Hide file tree
Showing 20 changed files with 1,150 additions and 11 deletions.
71 changes: 71 additions & 0 deletions src/Hydrator/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,12 +30,19 @@ abstract class AbstractHydrator implements HydratorInterface, StrategyEnabledInt
*/
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();
}

/**
Expand Down Expand Up @@ -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);
}
}
9 changes: 7 additions & 2 deletions src/Hydrator/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
43 changes: 37 additions & 6 deletions src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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;
}

Expand All @@ -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
*/
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
*/
Expand All @@ -144,6 +172,7 @@ public function hydrate(array $data, $object)

$transform = function ($letters) {
$letter = substr(array_shift($letters), 1, 1);

return ucfirst($letter);
};

Expand All @@ -158,6 +187,8 @@ public function hydrate(array $data, $object)
$object->$method($value);
}
}

return $object;
}

}
Loading

0 comments on commit 1c3fd99

Please sign in to comment.