View source
<?php
namespace Zend\Hydrator;
use Traversable;
use Zend\Stdlib\ArrayUtils;
class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface {
private $hydrationMethodsCache = [];
private $extractionMethodsCache = [];
protected $underscoreSeparatedKeys = true;
private $callableMethodFilter;
public function __construct($underscoreSeparatedKeys = true) {
parent::__construct();
$this
->setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
$this->callableMethodFilter = new Filter\OptionalParametersFilter();
$this->filterComposite
->addFilter('is', new Filter\IsFilter());
$this->filterComposite
->addFilter('has', new Filter\HasFilter());
$this->filterComposite
->addFilter('get', new Filter\GetFilter());
$this->filterComposite
->addFilter('parameter', new Filter\OptionalParametersFilter(), Filter\FilterComposite::CONDITION_AND);
}
public function setOptions($options) {
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException('The options parameter must be an array or a Traversable');
}
if (isset($options['underscoreSeparatedKeys'])) {
$this
->setUnderscoreSeparatedKeys($options['underscoreSeparatedKeys']);
}
return $this;
}
public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys) {
$this->underscoreSeparatedKeys = (bool) $underscoreSeparatedKeys;
if ($this->underscoreSeparatedKeys) {
$this
->setNamingStrategy(new NamingStrategy\UnderscoreNamingStrategy());
}
elseif ($this
->getNamingStrategy() instanceof NamingStrategy\UnderscoreNamingStrategy) {
$this
->removeNamingStrategy();
}
return $this;
}
public function getUnderscoreSeparatedKeys() {
return $this->underscoreSeparatedKeys;
}
public function extract($object) {
if (!is_object($object)) {
throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
}
$objectClass = get_class($object);
if ($object instanceof Filter\FilterProviderInterface) {
$this->extractionMethodsCache[$objectClass] = null;
}
if (!isset($this->extractionMethodsCache[$objectClass])) {
$this->extractionMethodsCache[$objectClass] = [];
$filter = $this->filterComposite;
$methods = get_class_methods($object);
if ($object instanceof Filter\FilterProviderInterface) {
$filter = new Filter\FilterComposite([
$object
->getFilter(),
], [
new Filter\MethodMatchFilter('getFilter'),
]);
}
foreach ($methods as $method) {
$methodFqn = $objectClass . '::' . $method;
if (!($filter
->filter($methodFqn) && $this->callableMethodFilter
->filter($methodFqn))) {
continue;
}
$attribute = $method;
if (strpos($method, 'get') === 0) {
$attribute = substr($method, 3);
if (!property_exists($object, $attribute)) {
$attribute = lcfirst($attribute);
}
}
$this->extractionMethodsCache[$objectClass][$method] = $attribute;
}
}
$values = [];
foreach ($this->extractionMethodsCache[$objectClass] as $methodName => $attributeName) {
$realAttributeName = $this
->extractName($attributeName, $object);
$values[$realAttributeName] = $this
->extractValue($realAttributeName, $object
->{$methodName}(), $object);
}
return $values;
}
public function hydrate(array $data, $object) {
if (!is_object($object)) {
throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
}
$objectClass = get_class($object);
foreach ($data as $property => $value) {
$propertyFqn = $objectClass . '::$' . $property;
if (!isset($this->hydrationMethodsCache[$propertyFqn])) {
$setterName = 'set' . ucfirst($this
->hydrateName($property, $data));
$this->hydrationMethodsCache[$propertyFqn] = is_callable([
$object,
$setterName,
]) ? $setterName : false;
}
if ($this->hydrationMethodsCache[$propertyFqn]) {
$object
->{$this->hydrationMethodsCache[$propertyFqn]}($this
->hydrateValue($property, $value, $data));
}
}
return $object;
}
public function addFilter($name, $filter, $condition = Filter\FilterComposite::CONDITION_OR) {
$this
->resetCaches();
return parent::addFilter($name, $filter, $condition);
}
public function removeFilter($name) {
$this
->resetCaches();
return parent::removeFilter($name);
}
public function setNamingStrategy(NamingStrategy\NamingStrategyInterface $strategy) {
$this
->resetCaches();
return parent::setNamingStrategy($strategy);
}
public function removeNamingStrategy() {
$this
->resetCaches();
return parent::removeNamingStrategy();
}
private function resetCaches() {
$this->hydrationMethodsCache = $this->extractionMethodsCache = [];
}
}