123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace Zend\Stdlib\Hydrator;
- use Traversable;
- use Zend\Stdlib\Exception;
- use Zend\Stdlib\ArrayUtils;
- use Zend\Stdlib\Hydrator\Filter\FilterComposite;
- use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface;
- use Zend\Stdlib\Hydrator\Filter\GetFilter;
- use Zend\Stdlib\Hydrator\Filter\HasFilter;
- use Zend\Stdlib\Hydrator\Filter\IsFilter;
- use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter;
- use Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter;
- use Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy;
- class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface
- {
-
- protected $underscoreSeparatedKeys = true;
-
- private $callableMethodFilter;
-
- public function __construct($underscoreSeparatedKeys = true)
- {
- parent::__construct();
- $this->setUnderscoreSeparatedKeys($underscoreSeparatedKeys);
- $this->callableMethodFilter = new OptionalParametersFilter();
- $this->filterComposite->addFilter("is", new IsFilter());
- $this->filterComposite->addFilter("has", new HasFilter());
- $this->filterComposite->addFilter("get", new GetFilter());
- $this->filterComposite->addFilter("parameter", new OptionalParametersFilter(), 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 UnderscoreNamingStrategy);
- } elseif ($this->getNamingStrategy() instanceof 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__
- ));
- }
- $filter = null;
- if ($object instanceof FilterProviderInterface) {
- $filter = new FilterComposite(
- array($object->getFilter()),
- array(new MethodMatchFilter("getFilter"))
- );
- } else {
- $filter = $this->filterComposite;
- }
- $attributes = array();
- $methods = get_class_methods($object);
- foreach ($methods as $method) {
- if (
- !$filter->filter(
- get_class($object) . '::' . $method
- )
- ) {
- continue;
- }
- if (!$this->callableMethodFilter->filter(get_class($object) . '::' . $method)) {
- continue;
- }
- $attribute = $method;
- if (preg_match('/^get/', $method)) {
- $attribute = substr($method, 3);
- if (!property_exists($object, $attribute)) {
- $attribute = lcfirst($attribute);
- }
- }
- $attribute = $this->extractName($attribute, $object);
- $attributes[$attribute] = $this->extractValue($attribute, $object->$method(), $object);
- }
- return $attributes;
- }
-
- 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__
- ));
- }
- foreach ($data as $property => $value) {
- $method = 'set' . ucfirst($this->hydrateName($property, $data));
- if (is_callable(array($object, $method))) {
- $value = $this->hydrateValue($property, $value, $data);
- $object->$method($value);
- }
- }
- return $object;
- }
- }
|