diff --git a/CHANGELOG.md b/CHANGELOG.md index 45dfa2df1..e941a47c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,16 +9,66 @@ All notable changes to this project will be documented in this file, in reverse - [#19](https://github.com/zendframework/zend-stdlib/pull/19) adds a new `FastPriorityQueue` implementation. It follows the same signature as `SplPriorityQueue`, but uses a performance-optimized algorithm: + - inserts are 2x faster than `SplPriorityQueue` and 3x faster than the `Zend\Stdlib\PriorityQueue` implementation. - extracts are 4x faster than `SplPriorityQueue` and 4-5x faster than the `Zend\Stdlib\PriorityQueue` implementation. + The intention is to use this as a drop-in replacement in the `zend-eventmanager` component to provide performance benefits. ### Deprecated -- Nothing. +- [#20](https://github.com/zendframework/zend-stdlib/pull/20) deprecates *all + hydrator* classes, in favor of the new [zend-hydrator](https://github.com/zendframework/zend-hydrator) + component. All classes were updated to extend their zend-hydrator equivalents, + and marked as `@deprecated`, indicating the equivalent class from the other + repository. + + Users *should* immediately start changing their code to use the zend-hydrator + equivalents; in most cases, this can be as easy as removing the `Stdlib` + namespace from import statements or hydrator configuration. Hydrators will be + removed entirely from zend-stdlib in v3.0, and all future updates to hydrators + will occur in the zend-hydrator library. + + Changes with backwards compatibility implications: + + - Users implementing `Zend\Stdlib\Hydrator\HydratorAwareInterface` will need to + update their `setHydrator()` implementation to typehint on + `Zend\Hydrator\HydratorInterface`. This can be done by changing the import + statement for that interface as follows: + + ```php + // Replace this: + use Zend\Stdlib\Hydrator\HydratorInterface; + // with this: + use Zend\Hydrator\HydratorInterface; + ``` + + If you are not using imports, change the typehint within the signature itself: + + ```php + // Replace this: + public function setHydrator(\Zend\Stdlib\Hydrator\HydratorInterface $hydrator) + // with this: + public function setHydrator(\Zend\Hydrator\HydratorInterface $hydrator) + ``` + + If you are using `Zend\Stdlib\Hydrator\HydratorAwareTrait`, no changes are + necessary, unless you override that method. + + - If you were catching hydrator-generated exceptions, these were previously in + the `Zend\Stdlib\Exception` namespace. You will need to update your code to + catch exceptions in the `Zend\Hydrator\Exception` namespace. + + - Users who *do* migrate to zend-hydrator may end up in a situation where + their code will not work with existing libraries that are still type-hinting + on the zend-stdlib interfaces. We will be attempting to address that ASAP, + but the deprecation within zend-stdlib is necessary as a first step. + + In the meantime, you can write hydrators targeting zend-stdlib still in + order to guarantee compatibility. ### Removed diff --git a/composer.json b/composer.json index ee6fd3505..fd0dcea27 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } }, "require": { - "php": ">=5.5" + "php": ">=5.5", + "zendframework/zend-hydrator": "~1.0" }, "require-dev": { "zendframework/zend-config": "~2.5", diff --git a/src/Extractor/ExtractionInterface.php b/src/Extractor/ExtractionInterface.php index 297d55774..6c84720c8 100644 --- a/src/Extractor/ExtractionInterface.php +++ b/src/Extractor/ExtractionInterface.php @@ -9,13 +9,11 @@ namespace Zend\Stdlib\Extractor; -interface ExtractionInterface +use Zend\Hydrator\ExtractionInterface as BaseExtractionInterface; + +/** + * @deprecated Use Zend\Hydrator\ExtractionInterface from zendframework/zend-hydrator instead. + */ +interface ExtractionInterface extends BaseExtractionInterface { - /** - * Extract values from an object - * - * @param object $object - * @return array - */ - public function extract($object); } diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 338ed8044..a4cf08a5a 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -9,279 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use ArrayObject; -use Zend\Stdlib\Exception; -use Zend\Stdlib\Hydrator\Filter\FilterComposite; -use Zend\Stdlib\Hydrator\NamingStrategy\NamingStrategyInterface; -use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; +use Zend\Hydrator\AbstractHydrator as BaseAbstractHydrator; -abstract class AbstractHydrator implements - HydratorInterface, - StrategyEnabledInterface, - FilterEnabledInterface, - NamingStrategyEnabledInterface +/** + * @deprecated Use Zend\Hydrator\AbstractHydrator from zendframework/zend-hydrator instead. + */ +abstract class AbstractHydrator extends BaseAbstractHydrator implements HydratorInterface { - /** - * The list with strategies that this hydrator has. - * - * @var ArrayObject - */ - protected $strategies; - - /** - * An instance of NamingStrategyInterface - * - * @var NamingStrategyInterface - */ - protected $namingStrategy; - - /** - * 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(); - } - - /** - * Gets the strategy with the given name. - * - * @param string $name The name of the strategy to get. - * - * @throws \Zend\Stdlib\Exception\InvalidArgumentException - * @return StrategyInterface - */ - public function getStrategy($name) - { - if (isset($this->strategies[$name])) { - return $this->strategies[$name]; - } - - if (!isset($this->strategies['*'])) { - throw new Exception\InvalidArgumentException(sprintf( - '%s: no strategy by name of "%s", and no wildcard strategy present', - __METHOD__, - $name - )); - } - - return $this->strategies['*']; - } - - /** - * Checks if the strategy with the given name exists. - * - * @param string $name The name of the strategy to check for. - * @return bool - */ - public function hasStrategy($name) - { - return array_key_exists($name, $this->strategies) - || array_key_exists('*', $this->strategies); - } - - /** - * Adds the given strategy under the given name. - * - * @param string $name The name of the strategy to register. - * @param StrategyInterface $strategy The strategy to register. - * @return HydratorInterface - */ - public function addStrategy($name, StrategyInterface $strategy) - { - $this->strategies[$name] = $strategy; - return $this; - } - - /** - * Removes the strategy with the given name. - * - * @param string $name The name of the strategy to remove. - * @return HydratorInterface - */ - public function removeStrategy($name) - { - unset($this->strategies[$name]); - return $this; - } - - /** - * Converts a value for extraction. If no strategy exists the plain value is returned. - * - * @param string $name The name of the strategy to use. - * @param mixed $value The value that should be converted. - * @param mixed $object The object is optionally provided as context. - * @return mixed - */ - public function extractValue($name, $value, $object = null) - { - if ($this->hasStrategy($name)) { - $strategy = $this->getStrategy($name); - $value = $strategy->extract($value, $object); - } - return $value; - } - - /** - * Converts a value for hydration. If no strategy exists the plain value is returned. - * - * @param string $name The name of the strategy to use. - * @param mixed $value The value that should be converted. - * @param array $data The whole data is optionally provided as context. - * @return mixed - */ - public function hydrateValue($name, $value, $data = null) - { - if ($this->hasStrategy($name)) { - $strategy = $this->getStrategy($name); - $value = $strategy->hydrate($value, $data); - } - return $value; - } - - /** - * Convert a name for extraction. If no naming strategy exists, the plain value is returned. - * - * @param string $name The name to convert. - * @param null $object The object is optionally provided as context. - * @return mixed - */ - public function extractName($name, $object = null) - { - if ($this->hasNamingStrategy()) { - $name = $this->getNamingStrategy()->extract($name, $object); - } - return $name; - } - - /** - * Converts a value for hydration. If no naming strategy exists, the plain value is returned. - * - * @param string $name The name to convert. - * @param array $data The whole data is optionally provided as context. - * @return mixed - */ - public function hydrateName($name, $data = null) - { - if ($this->hasNamingStrategy()) { - $name = $this->getNamingStrategy()->hydrate($name, $data); - } - return $name; - } - - /** - * 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: - * - * - * $composite->addFilter("servicelocator", - * function ($property) { - * list($class, $method) = explode('::', $property); - * if ($method === 'getServiceLocator') { - * return false; - * } - * return true; - * }, FilterComposite::CONDITION_AND - * ); - * - * - * @param string $name Index in the composite - * @param callable|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 - * - * - * $filterComposite->removeFilter('has'); - * - * - * @param $name - * @return Filter\FilterComposite - */ - public function removeFilter($name) - { - return $this->filterComposite->removeFilter($name); - } - - /** - * Adds the given naming strategy - * - * @param NamingStrategyInterface $strategy The naming to register. - * @return self - */ - public function setNamingStrategy(NamingStrategyInterface $strategy) - { - $this->namingStrategy = $strategy; - - return $this; - } - - /** - * Gets the naming strategy. - * - * @return NamingStrategyInterface - */ - public function getNamingStrategy() - { - return $this->namingStrategy; - } - - /** - * Checks if a naming strategy exists. - * - * @return bool - */ - public function hasNamingStrategy() - { - return isset($this->namingStrategy); - } - - /** - * Removes the naming strategy - * - * @return self - */ - public function removeNamingStrategy() - { - $this->namingStrategy = null; - - return $this; - } } diff --git a/src/Hydrator/Aggregate/AggregateHydrator.php b/src/Hydrator/Aggregate/AggregateHydrator.php index 6bba864d2..62c6b32a7 100644 --- a/src/Hydrator/Aggregate/AggregateHydrator.php +++ b/src/Hydrator/Aggregate/AggregateHydrator.php @@ -9,77 +9,14 @@ namespace Zend\Stdlib\Hydrator\Aggregate; -use Zend\EventManager\EventManager; -use Zend\EventManager\EventManagerAwareInterface; -use Zend\EventManager\EventManagerInterface; +use Zend\Hydrator\Aggregate\AggregateHydrator as BaseAggregateHydrator; use Zend\Stdlib\Hydrator\HydratorInterface; /** * Aggregate hydrator that composes multiple hydrators via events + * + * @deprecated Use Zend\Hydrator\Aggregate\AggregateHydrator from zendframework/zend-hydrator instead. */ -class AggregateHydrator implements HydratorInterface, EventManagerAwareInterface +class AggregateHydrator extends BaseAggregateHydrator implements HydratorInterface { - const DEFAULT_PRIORITY = 1; - - /** - * @var \Zend\EventManager\EventManagerInterface|null - */ - protected $eventManager; - - /** - * Attaches the provided hydrator to the list of hydrators to be used while hydrating/extracting data - * - * @param \Zend\Stdlib\Hydrator\HydratorInterface $hydrator - * @param int $priority - */ - public function add(HydratorInterface $hydrator, $priority = self::DEFAULT_PRIORITY) - { - $this->getEventManager()->attachAggregate(new HydratorListener($hydrator), $priority); - } - - /** - * {@inheritDoc} - */ - public function extract($object) - { - $event = new ExtractEvent($this, $object); - - $this->getEventManager()->trigger($event); - - return $event->getExtractedData(); - } - - /** - * {@inheritDoc} - */ - public function hydrate(array $data, $object) - { - $event = new HydrateEvent($this, $object, $data); - - $this->getEventManager()->trigger($event); - - return $event->getHydratedObject(); - } - - /** - * {@inheritDoc} - */ - public function setEventManager(EventManagerInterface $eventManager) - { - $eventManager->setIdentifiers([__CLASS__, get_class($this)]); - - $this->eventManager = $eventManager; - } - - /** - * {@inheritDoc} - */ - public function getEventManager() - { - if (null === $this->eventManager) { - $this->setEventManager(new EventManager()); - } - - return $this->eventManager; - } } diff --git a/src/Hydrator/Aggregate/ExtractEvent.php b/src/Hydrator/Aggregate/ExtractEvent.php index a82671033..9563efb2c 100644 --- a/src/Hydrator/Aggregate/ExtractEvent.php +++ b/src/Hydrator/Aggregate/ExtractEvent.php @@ -9,90 +9,14 @@ namespace Zend\Stdlib\Hydrator\Aggregate; -use Zend\EventManager\Event; +use Zend\Hydrator\Aggregate\ExtractEvent as BaseExtractEvent; /** * Event triggered when the {@see \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator} extracts * data from an object + * + * @deprecated Use Zend\Hydrator\Aggregate\ExtractEvent from zendframework/zend-hydrator instead. */ -class ExtractEvent extends Event +class ExtractEvent extends BaseExtractEvent { - const EVENT_EXTRACT = 'extract'; - - /** - * {@inheritDoc} - */ - protected $name = self::EVENT_EXTRACT; - - /** - * @var object - */ - protected $extractionObject; - - /** - * @var array - */ - protected $extractedData = []; - - /** - * @param object $target - * @param object $extractionObject - */ - public function __construct($target, $extractionObject) - { - $this->target = $target; - $this->extractionObject = $extractionObject; - } - - /** - * Retrieves the object from which data is extracted - * - * @return object - */ - public function getExtractionObject() - { - return $this->extractionObject; - } - - /** - * @param object $extractionObject - * - * @return void - */ - public function setExtractionObject($extractionObject) - { - $this->extractionObject = $extractionObject; - } - - /** - * Retrieves the data that has been extracted - * - * @return array - */ - public function getExtractedData() - { - return $this->extractedData; - } - - /** - * @param array $extractedData - * - * @return void - */ - public function setExtractedData(array $extractedData) - { - $this->extractedData = $extractedData; - } - - /** - * Merge provided data with the extracted data - * - * @param array $additionalData - * - * @return void - */ - public function mergeExtractedData(array $additionalData) - { - $this->extractedData = array_merge($this->extractedData, $additionalData); - } } diff --git a/src/Hydrator/Aggregate/HydrateEvent.php b/src/Hydrator/Aggregate/HydrateEvent.php index a7c91eec2..29c9fd67b 100644 --- a/src/Hydrator/Aggregate/HydrateEvent.php +++ b/src/Hydrator/Aggregate/HydrateEvent.php @@ -9,76 +9,14 @@ namespace Zend\Stdlib\Hydrator\Aggregate; -use Zend\EventManager\Event; +use Zend\Hydrator\Aggregate\HydrateEvent as BaseHydrateEvent; /** * Event triggered when the {@see \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator} hydrates * data into an object + * + * @deprecated Use Zend\Hydrator\Aggregate\HydrateEvent from zendframework/zend-hydrator instead. */ -class HydrateEvent extends Event +class HydrateEvent extends BaseHydrateEvent { - const EVENT_HYDRATE = 'hydrate'; - - /** - * {@inheritDoc} - */ - protected $name = self::EVENT_HYDRATE; - - /** - * @var object - */ - protected $hydratedObject; - - /** - * @var array - */ - protected $hydrationData; - - /** - * @param object $target - * @param object $hydratedObject - * @param array $hydrationData - */ - public function __construct($target, $hydratedObject, array $hydrationData) - { - $this->target = $target; - $this->hydratedObject = $hydratedObject; - $this->hydrationData = $hydrationData; - } - - /** - * Retrieves the object that is being hydrated - * - * @return object - */ - public function getHydratedObject() - { - return $this->hydratedObject; - } - - /** - * @param object $hydratedObject - */ - public function setHydratedObject($hydratedObject) - { - $this->hydratedObject = $hydratedObject; - } - - /** - * Retrieves the data that is being used for hydration - * - * @return array - */ - public function getHydrationData() - { - return $this->hydrationData; - } - - /** - * @param array $hydrationData - */ - public function setHydrationData(array $hydrationData) - { - $this->hydrationData = $hydrationData; - } } diff --git a/src/Hydrator/Aggregate/HydratorListener.php b/src/Hydrator/Aggregate/HydratorListener.php index 84c8c1289..e15cae476 100644 --- a/src/Hydrator/Aggregate/HydratorListener.php +++ b/src/Hydrator/Aggregate/HydratorListener.php @@ -9,72 +9,15 @@ namespace Zend\Stdlib\Hydrator\Aggregate; -use Zend\EventManager\AbstractListenerAggregate; -use Zend\EventManager\EventManagerInterface; -use Zend\Stdlib\Hydrator\HydratorInterface; +use Zend\Hydrator\Aggregate\HydratorListener as BaseHydratorListener; /** * Aggregate listener wrapping around a hydrator. Listens * to {@see \Zend\Stdlib\Hydrator\Aggregate::EVENT_HYDRATE} and * {@see \Zend\Stdlib\Hydrator\Aggregate::EVENT_EXTRACT} + * + * @deprecated Use Zend\Hydrator\Aggregate\HydratorListener from zendframework/zend-hydrator instead. */ -class HydratorListener extends AbstractListenerAggregate +class HydratorListener extends BaseHydratorListener { - /** - * @var \Zend\Stdlib\Hydrator\HydratorInterface - */ - protected $hydrator; - - /** - * @param \Zend\Stdlib\Hydrator\HydratorInterface $hydrator - */ - public function __construct(HydratorInterface $hydrator) - { - $this->hydrator = $hydrator; - } - - /** - * {@inheritDoc} - */ - public function attach(EventManagerInterface $events, $priority = 1) - { - $this->listeners[] = $events->attach(HydrateEvent::EVENT_HYDRATE, [$this, 'onHydrate'], $priority); - $this->listeners[] = $events->attach(ExtractEvent::EVENT_EXTRACT, [$this, 'onExtract'], $priority); - } - - /** - * Callback to be used when {@see \Zend\Stdlib\Hydrator\Aggregate\HydrateEvent::EVENT_HYDRATE} is triggered - * - * @param \Zend\Stdlib\Hydrator\Aggregate\HydrateEvent $event - * - * @return object - * - * @internal - */ - public function onHydrate(HydrateEvent $event) - { - $object = $this->hydrator->hydrate($event->getHydrationData(), $event->getHydratedObject()); - - $event->setHydratedObject($object); - - return $object; - } - - /** - * Callback to be used when {@see \Zend\Stdlib\Hydrator\Aggregate\ExtractEvent::EVENT_EXTRACT} is triggered - * - * @param \Zend\Stdlib\Hydrator\Aggregate\ExtractEvent $event - * - * @return array - * - * @internal - */ - public function onExtract(ExtractEvent $event) - { - $data = $this->hydrator->extract($event->getExtractionObject()); - - $event->mergeExtractedData($data); - - return $data; - } } diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index eb75e2228..415a44d1a 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -9,75 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Exception; +use Zend\Hydrator\ArraySerializable as BaseArraySerializable; -class ArraySerializable extends AbstractHydrator +/** + * @deprecated Use Zend\Hydrator\ArraySerializable from zendframework/zend-hydrator instead. + */ +class ArraySerializable extends BaseArraySerializable implements HydratorInterface { - /** - * Extract values from the provided object - * - * Extracts values via the object's getArrayCopy() method. - * - * @param object $object - * @return array - * @throws Exception\BadMethodCallException for an $object not implementing getArrayCopy() - */ - public function extract($object) - { - if (!is_callable([$object, 'getArrayCopy'])) { - throw new Exception\BadMethodCallException( - sprintf('%s expects the provided object to implement getArrayCopy()', __METHOD__) - ); - } - - $data = $object->getArrayCopy(); - $filter = $this->getFilter(); - - foreach ($data as $name => $value) { - if (!$filter->filter($name)) { - unset($data[$name]); - continue; - } - $extractedName = $this->extractName($name, $object); - // replace the original key with extracted, if differ - if ($extractedName !== $name) { - unset($data[$name]); - $name = $extractedName; - } - $data[$name] = $this->extractValue($name, $value, $object); - } - - return $data; - } - - /** - * Hydrate an object - * - * Hydrates an object by passing $data to either its exchangeArray() or - * populate() method. - * - * @param array $data - * @param object $object - * @return object - * @throws Exception\BadMethodCallException for an $object not implementing exchangeArray() or populate() - */ - public function hydrate(array $data, $object) - { - $replacement = []; - foreach ($data as $key => $value) { - $name = $this->hydrateName($key, $data); - $replacement[$name] = $this->hydrateValue($name, $value, $data); - } - - if (is_callable([$object, 'exchangeArray'])) { - $object->exchangeArray($replacement); - } elseif (is_callable([$object, 'populate'])) { - $object->populate($replacement); - } else { - throw new Exception\BadMethodCallException( - sprintf('%s expects the provided object to implement exchangeArray() or populate()', __METHOD__) - ); - } - return $object; - } } diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index f95b0d1e3..ee6bdadc0 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -9,266 +9,11 @@ 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\NamingStrategyInterface; -use Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy; +use Zend\Hydrator\ClassMethods as BaseClassMethods; -class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface +/** + * @deprecated Use Zend\Hydrator\ClassMethods from zendframework/zend-hydrator instead. + */ +class ClassMethods extends BaseClassMethods implements HydratorInterface, HydratorOptionsInterface { - /** - * Holds the names of the methods used for hydration, indexed by class::property name, - * false if the hydration method is not callable/usable for hydration purposes - * - * @var string[]|bool[] - */ - private $hydrationMethodsCache = []; - - /** - * A map of extraction methods to property name to be used during extraction, indexed - * by class name and method name - * - * @var string[][] - */ - private $extractionMethodsCache = []; - - /** - * Flag defining whether array keys are underscore-separated (true) or camel case (false) - * - * @var bool - */ - protected $underscoreSeparatedKeys = true; - - /** - * @var \Zend\Stdlib\Hydrator\Filter\FilterInterface - */ - private $callableMethodFilter; - - /** - * Define if extract values will use camel case or name with underscore - * @param bool|array $underscoreSeparatedKeys - */ - 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); - } - - /** - * @param array|Traversable $options - * @return ClassMethods - * @throws Exception\InvalidArgumentException - */ - 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; - } - - /** - * @param bool $underscoreSeparatedKeys - * @return ClassMethods - */ - public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys) - { - $this->underscoreSeparatedKeys = (bool) $underscoreSeparatedKeys; - - if ($this->underscoreSeparatedKeys) { - $this->setNamingStrategy(new UnderscoreNamingStrategy); - } elseif ($this->getNamingStrategy() instanceof UnderscoreNamingStrategy) { - $this->removeNamingStrategy(); - } - - return $this; - } - - /** - * @return bool - */ - public function getUnderscoreSeparatedKeys() - { - return $this->underscoreSeparatedKeys; - } - - /** - * Extract values from an object with class methods - * - * Extracts the getter/setter of the given $object. - * - * @param object $object - * @return array - * @throws Exception\BadMethodCallException for a non-object $object - */ - 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); - - // reset the hydrator's hydrator's cache for this object, as the filter may be per-instance - if ($object instanceof FilterProviderInterface) { - $this->extractionMethodsCache[$objectClass] = null; - } - - // pass 1 - finding out which properties can be extracted, with which methods (populate hydration cache) - if (! isset($this->extractionMethodsCache[$objectClass])) { - $this->extractionMethodsCache[$objectClass] = []; - $filter = $this->filterComposite; - $methods = get_class_methods($object); - - if ($object instanceof FilterProviderInterface) { - $filter = new FilterComposite( - [$object->getFilter()], - [new 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 = []; - - // pass 2 - actually extract data - foreach ($this->extractionMethodsCache[$objectClass] as $methodName => $attributeName) { - $realAttributeName = $this->extractName($attributeName, $object); - $values[$realAttributeName] = $this->extractValue($realAttributeName, $object->$methodName(), $object); - } - - return $values; - } - - /** - * Hydrate an object by populating getter/setter methods - * - * Hydrates an object by getter/setter methods of the object. - * - * @param array $data - * @param object $object - * @return object - * @throws Exception\BadMethodCallException for a non-object $object - */ - 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; - } - - /** - * {@inheritDoc} - */ - public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR) - { - $this->resetCaches(); - - return parent::addFilter($name, $filter, $condition); - } - - /** - * {@inheritDoc} - */ - public function removeFilter($name) - { - $this->resetCaches(); - - return parent::removeFilter($name); - } - - /** - * {@inheritDoc} - */ - public function setNamingStrategy(NamingStrategyInterface $strategy) - { - $this->resetCaches(); - - return parent::setNamingStrategy($strategy); - } - - /** - * {@inheritDoc} - */ - public function removeNamingStrategy() - { - $this->resetCaches(); - - return parent::removeNamingStrategy(); - } - - /** - * Reset all local hydration/extraction caches - */ - private function resetCaches() - { - $this->hydrationMethodsCache = $this->extractionMethodsCache = []; - } } diff --git a/src/Hydrator/DelegatingHydrator.php b/src/Hydrator/DelegatingHydrator.php index db234d38a..eb65d9531 100644 --- a/src/Hydrator/DelegatingHydrator.php +++ b/src/Hydrator/DelegatingHydrator.php @@ -9,49 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\Hydrator\DelegatingHydrator as BaseDelegatingHydrator; -class DelegatingHydrator implements HydratorInterface +/** + * @deprecated Use Zend\Hydrator\DelegatingHydrator from zendframework/zend-hydrator instead. + */ +class DelegatingHydrator extends BaseDelegatingHydrator implements HydratorInterface { - /** - * @var ServiceLocatorInterface - */ - protected $hydrators; - - /** - * Constructor - * - * @param ServiceLocatorInterface $hydrators - */ - public function __construct(ServiceLocatorInterface $hydrators) - { - $this->hydrators = $hydrators; - } - - /** - * {@inheritdoc} - */ - public function hydrate(array $data, $object) - { - return $this->getHydrator($object)->hydrate($data, $object); - } - - /** - * {@inheritdoc} - */ - public function extract($object) - { - return $this->getHydrator($object)->extract($object); - } - - /** - * Gets hydrator of an object - * - * @param object $object - * @return HydratorInterface - */ - protected function getHydrator($object) - { - return $this->hydrators->get(get_class($object)); - } } diff --git a/src/Hydrator/DelegatingHydratorFactory.php b/src/Hydrator/DelegatingHydratorFactory.php index c3a0da25e..170308d97 100644 --- a/src/Hydrator/DelegatingHydratorFactory.php +++ b/src/Hydrator/DelegatingHydratorFactory.php @@ -9,21 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\Hydrator\DelegatingHydratorFactory as BaseDelegatingHydratorFactory; -class DelegatingHydratorFactory implements FactoryInterface +/** + * @deprecated Use Zend\Hydrator\DelegatingHydratorFactory from zendframework/zend-hydrator instead. + */ +class DelegatingHydratorFactory extends BaseDelegatingHydratorFactory { - /** - * Creates DelegatingHydrator - * - * @param ServiceLocatorInterface $serviceLocator - * @return DelegatingHydrator - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - // Assume that this factory is registered with the HydratorManager, - // and just pass it directly on. - return new DelegatingHydrator($serviceLocator); - } } diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index f9d4c5642..b230c97ba 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -8,189 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -use ArrayObject; -use Zend\Stdlib\Exception\InvalidArgumentException; +use Zend\Hydrator\Filter\FilterComposite as BaseFilterComposite; -class FilterComposite implements FilterInterface +/** + * @deprecated Use Zend\Hydrator\Filter\FilterComposite from zendframework/zend-hydrator instead. + */ +class FilterComposite extends BaseFilterComposite implements FilterInterface { - /** - * @var ArrayObject - */ - protected $orFilter; - - /** - * @var ArrayObject - */ - protected $andFilter; - - /** - * Constant to add with "or" conditition - */ - const CONDITION_OR = 1; - - /** - * Constant to add with "and" conditition - */ - const CONDITION_AND = 2; - - /** - * Define default Filter - * - * @param array $orFilter - * @param array $andFilter - * @throws InvalidArgumentException - */ - public function __construct($orFilter = [], $andFilter = []) - { - array_walk( - $orFilter, - function ($value, $key) { - if (!is_callable($value) && !$value instanceof FilterInterface) { - throw new InvalidArgumentException( - 'The value of ' . $key . ' should be either a callable or ' . - 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' - ); - } - } - ); - - array_walk( - $andFilter, - function ($value, $key) { - if (!is_callable($value) && !$value instanceof FilterInterface) { - throw new InvalidArgumentException( - 'The value of ' . $key . ' should be either a callable or ' . - 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' - ); - } - } - ); - - $this->orFilter = new ArrayObject($orFilter); - $this->andFilter = new ArrayObject($andFilter); - } - - /** - * Add a filter to the composite. Has to be indexed with $name in - * order to identify a specific filter. - * - * This example will exclude all methods from the hydration, that starts with 'getService' - * - * $composite->addFilter('exclude', - * function ($method) { - * if (preg_match('/^getService/', $method) { - * return false; - * } - * return true; - * }, FilterComposite::CONDITION_AND - * ); - * - * - * @param string $name - * @param callable|FilterInterface $filter - * @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND - * @throws InvalidArgumentException - * @return FilterComposite - */ - public function addFilter($name, $filter, $condition = self::CONDITION_OR) - { - if (!is_callable($filter) && !($filter instanceof FilterInterface)) { - throw new InvalidArgumentException( - 'The value of ' . $name . ' should be either a callable or ' . - 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' - ); - } - - if ($condition === self::CONDITION_OR) { - $this->orFilter[$name] = $filter; - } elseif ($condition === self::CONDITION_AND) { - $this->andFilter[$name] = $filter; - } - - return $this; - } - - /** - * Remove a filter from the composition - * - * @param $name string Identifier for the filter - * @return FilterComposite - */ - public function removeFilter($name) - { - if (isset($this->orFilter[$name])) { - unset($this->orFilter[$name]); - } - - if (isset($this->andFilter[$name])) { - unset($this->andFilter[$name]); - } - - return $this; - } - - /** - * Check if $name has a filter registered - * - * @param $name string Identifier for the filter - * @return bool - */ - public function hasFilter($name) - { - return isset($this->orFilter[$name]) || isset($this->andFilter[$name]); - } - - /** - * Filter the composite based on the AND and OR condition - * Will return true if one from the "or conditions" and all from - * the "and condition" returns true. Otherwise false - * - * @param $property string Parameter will be e.g. Parent\Namespace\Class::method - * @return bool - */ - public function filter($property) - { - $andCount = count($this->andFilter); - $orCount = count($this->orFilter); - // return true if no filters are registered - if ($orCount === 0 && $andCount === 0) { - return true; - } elseif ($orCount === 0 && $andCount !== 0) { - $returnValue = true; - } else { - $returnValue = false; - } - - // Check if 1 from the or filters return true - foreach ($this->orFilter as $filter) { - if (is_callable($filter)) { - if ($filter($property) === true) { - $returnValue = true; - break; - } - continue; - } else { - if ($filter->filter($property) === true) { - $returnValue = true; - break; - } - } - } - - // Check if all of the and condition return true - foreach ($this->andFilter as $filter) { - if (is_callable($filter)) { - if ($filter($property) === false) { - return false; - } - continue; - } else { - if ($filter->filter($property) === false) { - return false; - } - } - } - - return $returnValue; - } } diff --git a/src/Hydrator/Filter/FilterInterface.php b/src/Hydrator/Filter/FilterInterface.php index 16df098f5..95c4e2402 100644 --- a/src/Hydrator/Filter/FilterInterface.php +++ b/src/Hydrator/Filter/FilterInterface.php @@ -8,14 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -interface FilterInterface +use Zend\Hydrator\Filter\FilterInterface as BaseFilterInterface; + +/** + * @deprecated Use Zend\Hydrator\Filter\FilterInterface from zendframework/zend-hydrator instead. + */ +interface FilterInterface extends BaseFilterInterface { - /** - * Should return true, if the given filter - * does not match - * - * @param string $property The name of the property - * @return bool - */ - public function filter($property); } diff --git a/src/Hydrator/Filter/FilterProviderInterface.php b/src/Hydrator/Filter/FilterProviderInterface.php index c2e978877..142ddf1ff 100644 --- a/src/Hydrator/Filter/FilterProviderInterface.php +++ b/src/Hydrator/Filter/FilterProviderInterface.php @@ -8,12 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -interface FilterProviderInterface +use Zend\Hydrator\Filter\FilterProviderInterface as BaseFilterProviderInterface; + +/** + * @deprecated Use Zend\Hydrator\Filter\FilterProviderInterface from zendframework/zend-hydrator instead. + */ +interface FilterProviderInterface extends BaseFilterProviderInterface { - /** - * Provides a filter for hydration - * - * @return FilterInterface - */ - public function getFilter(); } diff --git a/src/Hydrator/Filter/GetFilter.php b/src/Hydrator/Filter/GetFilter.php index b4d898dc2..ca7dd3cbe 100644 --- a/src/Hydrator/Filter/GetFilter.php +++ b/src/Hydrator/Filter/GetFilter.php @@ -8,20 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -class GetFilter implements FilterInterface -{ - public function filter($property) - { - $pos = strpos($property, '::'); - if ($pos !== false) { - $pos += 2; - } else { - $pos = 0; - } +use Zend\Hydrator\Filter\GetFilter as BaseGetFilter; - if (substr($property, $pos, 3) === 'get') { - return true; - } - return false; - } +/** + * @deprecated Use Zend\Hydrator\Filter\GetFilter from zendframework/zend-hydrator instead. + */ +class GetFilter extends BaseGetFilter implements FilterInterface +{ } diff --git a/src/Hydrator/Filter/HasFilter.php b/src/Hydrator/Filter/HasFilter.php index 0cf57f95e..af00600bf 100644 --- a/src/Hydrator/Filter/HasFilter.php +++ b/src/Hydrator/Filter/HasFilter.php @@ -8,20 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -class HasFilter implements FilterInterface -{ - public function filter($property) - { - $pos = strpos($property, '::'); - if ($pos !== false) { - $pos += 2; - } else { - $pos = 0; - } +use Zend\Hydrator\Filter\HasFilter as BaseHasFilter; - if (substr($property, $pos, 3) === 'has') { - return true; - } - return false; - } +/** + * @deprecated Use Zend\Hydrator\Filter\HasFilter from zendframework/zend-hydrator instead. + */ +class HasFilter extends BaseHasFilter implements FilterInterface +{ } diff --git a/src/Hydrator/Filter/IsFilter.php b/src/Hydrator/Filter/IsFilter.php index 3b6e37637..9e51643f5 100644 --- a/src/Hydrator/Filter/IsFilter.php +++ b/src/Hydrator/Filter/IsFilter.php @@ -8,20 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -class IsFilter implements FilterInterface -{ - public function filter($property) - { - $pos = strpos($property, '::'); - if ($pos !== false) { - $pos += 2; - } else { - $pos = 0; - } +use Zend\Hydrator\Filter\IsFilter as BaseIsFilter; - if (substr($property, $pos, 2) === 'is') { - return true; - } - return false; - } +/** + * @deprecated Use Zend\Hydrator\Filter\IsFilter from zendframework/zend-hydrator instead. + */ +class IsFilter extends BaseIsFilter implements FilterInterface +{ } diff --git a/src/Hydrator/Filter/MethodMatchFilter.php b/src/Hydrator/Filter/MethodMatchFilter.php index 2601a6fb3..471849ce8 100644 --- a/src/Hydrator/Filter/MethodMatchFilter.php +++ b/src/Hydrator/Filter/MethodMatchFilter.php @@ -8,41 +8,11 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -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 or include - * @param bool $exclude If the method should be excluded - */ - public function __construct($method, $exclude = true) - { - $this->method = $method; - $this->exclude = $exclude; - } +use Zend\Hydrator\Filter\MethodMatchFilter as BaseMethodMatchFilter; - public function filter($property) - { - $pos = strpos($property, '::'); - if ($pos !== false) { - $pos += 2; - } else { - $pos = 0; - } - if (substr($property, $pos) === $this->method) { - return !$this->exclude; - } - return $this->exclude; - } +/** + * @deprecated Use Zend\Hydrator\Filter\MethodMatchFilter from zendframework/zend-hydrator instead. + */ +class MethodMatchFilter extends BaseMethodMatchFilter implements FilterInterface +{ } diff --git a/src/Hydrator/Filter/NumberOfParameterFilter.php b/src/Hydrator/Filter/NumberOfParameterFilter.php index 1254b63b3..95309b160 100644 --- a/src/Hydrator/Filter/NumberOfParameterFilter.php +++ b/src/Hydrator/Filter/NumberOfParameterFilter.php @@ -9,41 +9,11 @@ namespace Zend\Stdlib\Hydrator\Filter; -use ReflectionException; -use ReflectionMethod; -use Zend\Stdlib\Exception\InvalidArgumentException; +use Zend\Hydrator\Filter\NumberOfParameterFilter as BaseNumberOfParameterFilter; -class NumberOfParameterFilter implements FilterInterface +/** + * @deprecated Use Zend\Hydrator\Filter\NumberOfParameterFilter from zendframework/zend-hydrator instead. + */ +class NumberOfParameterFilter extends BaseNumberOfParameterFilter implements FilterInterface { - /** - * The number of parameters beeing accepted - * @var int - */ - protected $numberOfParameters = null; - - /** - * @param int $numberOfParameters Number of accepted parameters - */ - public function __construct($numberOfParameters = 0) - { - $this->numberOfParameters = (int) $numberOfParameters; - } - - /** - * @param string $property the name of the property - * @return bool - * @throws InvalidArgumentException - */ - public function filter($property) - { - try { - $reflectionMethod = new ReflectionMethod($property); - } catch (ReflectionException $exception) { - throw new InvalidArgumentException( - "Method $property doesn't exist" - ); - } - - return $reflectionMethod->getNumberOfParameters() === $this->numberOfParameters; - } } diff --git a/src/Hydrator/Filter/OptionalParametersFilter.php b/src/Hydrator/Filter/OptionalParametersFilter.php index 8204e786f..07c8dfdc4 100644 --- a/src/Hydrator/Filter/OptionalParametersFilter.php +++ b/src/Hydrator/Filter/OptionalParametersFilter.php @@ -8,47 +8,13 @@ */ namespace Zend\Stdlib\Hydrator\Filter; -use InvalidArgumentException; -use ReflectionException; -use ReflectionMethod; -use ReflectionParameter; +use Zend\Hydrator\Filter\OptionalParametersFilter as BaseOptionalParametersFilter; /** * Filter that includes methods which have no parameters or only optional parameters + * + * @deprecated Use Zend\Hydrator\Filter\OptionalParametersFilter from zendframework/zend-hydrator instead. */ -class OptionalParametersFilter implements FilterInterface +class OptionalParametersFilter extends BaseOptionalParametersFilter implements FilterInterface { - /** - * Map of methods already analyzed - * by {@see \Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter::filter()}, - * cached for performance reasons - * - * @var bool[] - */ - protected static $propertiesCache = []; - - /** - * {@inheritDoc} - */ - public function filter($property) - { - if (isset(static::$propertiesCache[$property])) { - return static::$propertiesCache[$property]; - } - - try { - $reflectionMethod = new ReflectionMethod($property); - } catch (ReflectionException $exception) { - throw new InvalidArgumentException(sprintf('Method %s doesn\'t exist', $property)); - } - - $mandatoryParameters = array_filter( - $reflectionMethod->getParameters(), - function (ReflectionParameter $parameter) { - return ! $parameter->isOptional(); - } - ); - - return static::$propertiesCache[$property] = empty($mandatoryParameters); - } } diff --git a/src/Hydrator/FilterEnabledInterface.php b/src/Hydrator/FilterEnabledInterface.php index 380aade05..9643f1427 100644 --- a/src/Hydrator/FilterEnabledInterface.php +++ b/src/Hydrator/FilterEnabledInterface.php @@ -9,55 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Hydrator\Filter\FilterInterface; -use Zend\Stdlib\Hydrator\Filter\FilterComposite; -use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface; +use Zend\Hydrator\FilterEnabledInterface as BaseFilterEnabledInterface; -interface FilterEnabledInterface extends FilterProviderInterface +/** + * @deprecated Use Zend\Hydrator\FilterEnabledInterface from zendframework/zend-hydrator instead. + */ +interface FilterEnabledInterface extends BaseFilterEnabledInterface { - /** - * Add a new filter to take care of what needs to be hydrated. - * To exclude e.g. the method getServiceLocator: - * - * - * $composite->addFilter( - * "servicelocator", - * function ($property) { - * list($class, $method) = explode('::', $property); - * if ($method === 'getServiceLocator') { - * return false; - * } - * return true; - * }, - * FilterComposite::CONDITION_AND - * ); - * - * - * @param string $name Index in the composite - * @param callable|FilterInterface $filter - * @param int $condition - * @return FilterComposite - */ - public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR); - - /** - * Check whether a specific filter exists at key $name or not - * - * @param string $name Index in the composite - * @return bool - */ - public function hasFilter($name); - - /** - * Remove a filter from the composition. - * To not extract "has" methods, you simply need to unregister it - * - * - * $filterComposite->removeFilter('has'); - * - * - * @param $name - * @return FilterComposite - */ - public function removeFilter($name); } diff --git a/src/Hydrator/HydrationInterface.php b/src/Hydrator/HydrationInterface.php index e7deff491..8395169b4 100644 --- a/src/Hydrator/HydrationInterface.php +++ b/src/Hydrator/HydrationInterface.php @@ -9,14 +9,11 @@ namespace Zend\Stdlib\Hydrator; -interface HydrationInterface +use Zend\Hydrator\HydrationInterface as BaseHydrationInterface; + +/** + * @deprecated Use Zend\Hydrator\HydrationInterface from zendframework/zend-hydrator instead. + */ +interface HydrationInterface extends BaseHydrationInterface { - /** - * Hydrate $object with the provided $data. - * - * @param array $data - * @param object $object - * @return object - */ - public function hydrate(array $data, $object); } diff --git a/src/Hydrator/HydratorAwareInterface.php b/src/Hydrator/HydratorAwareInterface.php index d64782ea4..625a609b7 100644 --- a/src/Hydrator/HydratorAwareInterface.php +++ b/src/Hydrator/HydratorAwareInterface.php @@ -9,20 +9,11 @@ namespace Zend\Stdlib\Hydrator; -interface HydratorAwareInterface -{ - /** - * Set hydrator - * - * @param HydratorInterface $hydrator - * @return HydratorAwareInterface - */ - public function setHydrator(HydratorInterface $hydrator); +use Zend\Hydrator\HydratorAwareInterface as BaseHydratorAwareInterface; - /** - * Retrieve hydrator - * - * @return HydratorInterface - */ - public function getHydrator(); +/** + * @deprecated Use Zend\Hydrator\HydratorAwareInterface from zendframework/zend-hydrator instead. + */ +interface HydratorAwareInterface extends BaseHydratorAwareInterface +{ } diff --git a/src/Hydrator/HydratorAwareTrait.php b/src/Hydrator/HydratorAwareTrait.php index 9c772c277..e7d608209 100644 --- a/src/Hydrator/HydratorAwareTrait.php +++ b/src/Hydrator/HydratorAwareTrait.php @@ -9,39 +9,12 @@ namespace Zend\Stdlib\Hydrator; +use Zend\Hydrator\HydratorAwareTrait as BaseHydratorAwareTrait; + +/** + * @deprecated Use Zend\Hydrator\HydratorAwareTrait from zendframework/zend-hydrator instead. + */ trait HydratorAwareTrait { - /** - * Hydrator instance - * - * @var HydratorInterface - * @access protected - */ - protected $hydrator = null; - - /** - * Set hydrator - * - * @param HydratorInterface $hydrator - * @return self - * @access public - */ - public function setHydrator(HydratorInterface $hydrator) - { - $this->hydrator = $hydrator; - - return $this; - } - - /** - * Retrieve hydrator - * - * @param void - * @return null|HydratorInterface - * @access public - */ - public function getHydrator() - { - return $this->hydrator; - } + use BaseHydratorAwareTrait; } diff --git a/src/Hydrator/HydratorInterface.php b/src/Hydrator/HydratorInterface.php index bc9983d9a..bd7122c32 100644 --- a/src/Hydrator/HydratorInterface.php +++ b/src/Hydrator/HydratorInterface.php @@ -11,6 +11,9 @@ use Zend\Stdlib\Extractor\ExtractionInterface; +/** + * @deprecated Use Zend\Hydrator\HydratorInterface from zendframework/zend-hydrator instead. + */ interface HydratorInterface extends HydrationInterface, ExtractionInterface { } diff --git a/src/Hydrator/HydratorOptionsInterface.php b/src/Hydrator/HydratorOptionsInterface.php index 44610f7c1..dfda9253f 100644 --- a/src/Hydrator/HydratorOptionsInterface.php +++ b/src/Hydrator/HydratorOptionsInterface.php @@ -9,11 +9,11 @@ namespace Zend\Stdlib\Hydrator; -interface HydratorOptionsInterface +use Zend\Hydrator\HydratorOptionsInterface as BaseHydratorOptionsInterface; + +/** + * @deprecated Use Zend\Hydrator\HydratorOptionsInterface from zendframework/zend-hydrator instead. + */ +interface HydratorOptionsInterface extends BaseHydratorOptionsInterface { - /** - * @param array|\Traversable $options - * @return HydratorOptionsInterface - */ - public function setOptions($options); } diff --git a/src/Hydrator/HydratorPluginManager.php b/src/Hydrator/HydratorPluginManager.php index 537202fc6..6b7049b8d 100644 --- a/src/Hydrator/HydratorPluginManager.php +++ b/src/Hydrator/HydratorPluginManager.php @@ -9,66 +9,15 @@ namespace Zend\Stdlib\Hydrator; -use Zend\ServiceManager\AbstractPluginManager; -use Zend\Stdlib\Exception; +use Zend\Hydrator\HydratorPluginManager as BaseHydratorPluginManager; /** * Plugin manager implementation for hydrators. * * Enforces that adapters retrieved are instances of HydratorInterface + * + * @deprecated Use Zend\Hydrator\HydratorPluginManager from zendframework/zend-hydrator instead. */ -class HydratorPluginManager extends AbstractPluginManager +class HydratorPluginManager extends BaseHydratorPluginManager { - /** - * Whether or not to share by default - * - * @var bool - */ - protected $shareByDefault = false; - - /** - * Default aliases - * - * @var array - */ - protected $aliases = [ - 'delegatinghydrator' => 'Zend\Stdlib\Hydrator\DelegatingHydrator', - ]; - - /** - * Default set of adapters - * - * @var array - */ - protected $invokableClasses = [ - 'arrayserializable' => 'Zend\Stdlib\Hydrator\ArraySerializable', - 'classmethods' => 'Zend\Stdlib\Hydrator\ClassMethods', - 'objectproperty' => 'Zend\Stdlib\Hydrator\ObjectProperty', - 'reflection' => 'Zend\Stdlib\Hydrator\Reflection' - ]; - - /** - * Default factory-based adapters - * - * @var array - */ - protected $factories = [ - 'Zend\Stdlib\Hydrator\DelegatingHydrator' => 'Zend\Stdlib\Hydrator\DelegatingHydratorFactory', - ]; - - /** - * {@inheritDoc} - */ - public function validatePlugin($plugin) - { - if ($plugin instanceof HydratorInterface) { - // we're okay - return; - } - - throw new Exception\RuntimeException(sprintf( - 'Plugin of type %s is invalid; must implement Zend\Stdlib\Hydrator\HydratorInterface', - (is_object($plugin) ? get_class($plugin) : gettype($plugin)) - )); - } } diff --git a/src/Hydrator/Iterator/HydratingArrayIterator.php b/src/Hydrator/Iterator/HydratingArrayIterator.php index 22bc81ab9..f51befe44 100644 --- a/src/Hydrator/Iterator/HydratingArrayIterator.php +++ b/src/Hydrator/Iterator/HydratingArrayIterator.php @@ -9,28 +9,11 @@ namespace Zend\Stdlib\Hydrator\Iterator; -use ArrayIterator; -use Zend\Stdlib\Hydrator\HydratorInterface; +use Zend\Hydrator\Iterator\HydratingArrayIterator as BaseHydratingArrayIterator; -class HydratingArrayIterator extends HydratingIteratorIterator +/** + * @deprecated Use Zend\Hydrator\Iterator\HydratingArrayIterator from zendframework/zend-hydrator instead. + */ +class HydratingArrayIterator extends BaseHydratingArrayIterator implements HydratingIteratorInterface { - /** - * @var HydratorInterface - */ - protected $hydrator; - - /** - * @var object - */ - protected $prototype; - - /** - * @param HydratorInterface $hydrator - * @param array $data - * @param string|object $prototype Object, or class name to use for prototype. - */ - public function __construct(HydratorInterface $hydrator, array $data, $prototype) - { - parent::__construct($hydrator, new ArrayIterator($data), $prototype); - } } diff --git a/src/Hydrator/Iterator/HydratingIteratorInterface.php b/src/Hydrator/Iterator/HydratingIteratorInterface.php index ba8c5967b..81a9999b5 100644 --- a/src/Hydrator/Iterator/HydratingIteratorInterface.php +++ b/src/Hydrator/Iterator/HydratingIteratorInterface.php @@ -9,25 +9,11 @@ namespace Zend\Stdlib\Hydrator\Iterator; -use Iterator; -use Zend\Stdlib\Hydrator\HydratorInterface; +use Zend\Hydrator\Iterator\HydratingIteratorInterface as BaseHydratingIteratorInterface; -interface HydratingIteratorInterface extends Iterator +/** + * @deprecated Use Zend\Hydrator\Iterator\HydratingIteratorInterface from zendframework/zend-hydrator instead. + */ +interface HydratingIteratorInterface extends BaseHydratingIteratorInterface { - /** - * This sets the prototype to hydrate. - * - * This prototype can be the name of the class or the object itself; - * iteration will clone the object. - * - * @param string|object $prototype - */ - public function setPrototype($prototype); - - /** - * Sets the hydrator to use during iteration. - * - * @param HydratorInterface $hydrator - */ - public function setHydrator(HydratorInterface $hydrator); } diff --git a/src/Hydrator/Iterator/HydratingIteratorIterator.php b/src/Hydrator/Iterator/HydratingIteratorIterator.php index 2cd1d65fa..1d2ac7ad9 100644 --- a/src/Hydrator/Iterator/HydratingIteratorIterator.php +++ b/src/Hydrator/Iterator/HydratingIteratorIterator.php @@ -9,70 +9,11 @@ namespace Zend\Stdlib\Hydrator\Iterator; -use Iterator; -use IteratorIterator; -use Zend\Stdlib\Exception\InvalidArgumentException; -use Zend\Stdlib\Hydrator\HydratorInterface; +use Zend\Hydrator\Iterator\HydratingIteratorIterator as BaseHydratingIteratorIterator; -class HydratingIteratorIterator extends IteratorIterator implements HydratingIteratorInterface +/** + * @deprecated Use Zend\Hydrator\Iterator\HydratingIteratorIterator from zendframework/zend-hydrator instead. + */ +class HydratingIteratorIterator extends BaseHydratingIteratorIterator implements HydratingIteratorInterface { - /** - * @var HydratorInterface - */ - protected $hydrator; - - /** - * @var object - */ - protected $prototype; - - /** - * @param HydratorInterface $hydrator - * @param Iterator $data - * @param string|object $prototype Object or class name to use for prototype. - */ - public function __construct(HydratorInterface $hydrator, Iterator $data, $prototype) - { - $this->setHydrator($hydrator); - $this->setPrototype($prototype); - parent::__construct($data); - } - - /** - * @inheritdoc - */ - public function setPrototype($prototype) - { - if (is_object($prototype)) { - $this->prototype = $prototype; - return; - } - - if (!class_exists($prototype)) { - throw new InvalidArgumentException( - sprintf('Method %s was passed an invalid class name: %s', __METHOD__, $prototype) - ); - } - - $this->prototype = new $prototype; - } - - /** - * @inheritdoc - */ - public function setHydrator(HydratorInterface $hydrator) - { - $this->hydrator = $hydrator; - } - - /** - * @return object Returns hydrated clone of $prototype - */ - public function current() - { - $currentValue = parent::current(); - $object = clone $this->prototype; - $this->hydrator->hydrate($currentValue, $object); - return $object; - } } diff --git a/src/Hydrator/NamingStrategy/ArrayMapNamingStrategy.php b/src/Hydrator/NamingStrategy/ArrayMapNamingStrategy.php index 6ca3711f3..d440c531e 100644 --- a/src/Hydrator/NamingStrategy/ArrayMapNamingStrategy.php +++ b/src/Hydrator/NamingStrategy/ArrayMapNamingStrategy.php @@ -9,43 +9,11 @@ namespace Zend\Stdlib\Hydrator\NamingStrategy; -final class ArrayMapNamingStrategy implements NamingStrategyInterface -{ - /** - * @var string[] - */ - private $extractionMap = []; - - /** - * @var string[] - */ - private $hydrationMap = []; - - /** - * Constructor - * - * @param array $extractionMap A map of string keys and values for symmetric translation of hydrated - * and extracted field names - */ - public function __construct(array $extractionMap) - { - $this->extractionMap = $extractionMap; - $this->hydrationMap = array_flip($extractionMap); - } - - /** - * {@inheritDoc} - */ - public function hydrate($name) - { - return isset($this->hydrationMap[$name]) ? $this->hydrationMap[$name] : $name; - } +use Zend\Hydrator\NamingStrategy\ArrayMapNamingStrategy as BaseArrayMapNamingStrategy; - /** - * {@inheritDoc} - */ - public function extract($name) - { - return isset($this->extractionMap[$name]) ? $this->extractionMap[$name] : $name; - } +/** + * @deprecated Use Zend\Hydrator\NamingStrategy\ArrayMapNamingStrategy from zendframework/zend-hydrator instead. + */ +class ArrayMapNamingStrategy extends BaseArrayMapNamingStrategy implements NamingStrategyInterface +{ } diff --git a/src/Hydrator/NamingStrategy/CompositeNamingStrategy.php b/src/Hydrator/NamingStrategy/CompositeNamingStrategy.php index 76a3082bd..d529a8b03 100644 --- a/src/Hydrator/NamingStrategy/CompositeNamingStrategy.php +++ b/src/Hydrator/NamingStrategy/CompositeNamingStrategy.php @@ -9,56 +9,11 @@ namespace Zend\Stdlib\Hydrator\NamingStrategy; -final class CompositeNamingStrategy implements NamingStrategyInterface -{ - /** - * @var array - */ - private $namingStrategies = []; - - /** - * @var NamingStrategyInterface - */ - private $defaultNamingStrategy; - - /** - * @param NamingStrategyInterface[] $strategies indexed by the name they translate - * @param NamingStrategyInterface|null $defaultNamingStrategy - */ - public function __construct(array $strategies, NamingStrategyInterface $defaultNamingStrategy = null) - { - $this->namingStrategies = array_map( - function (NamingStrategyInterface $strategy) { - // this callback is here only to ensure type-safety - return $strategy; - }, - $strategies - ); - - $this->defaultNamingStrategy = $defaultNamingStrategy ?: new IdentityNamingStrategy(); - } - - /** - * {@inheritDoc} - */ - public function extract($name) - { - $strategy = isset($this->namingStrategies[$name]) - ? $this->namingStrategies[$name] - : $this->defaultNamingStrategy; +use Zend\Hydrator\NamingStrategy\CompositeNamingStrategy as BaseCompositeNamingStrategy; - return $strategy->extract($name); - } - - /** - * {@inheritDoc} - */ - public function hydrate($name) - { - $strategy = isset($this->namingStrategies[$name]) - ? $this->namingStrategies[$name] - : $this->defaultNamingStrategy; - - return $strategy->hydrate($name); - } +/** + * @deprecated Use Zend\Hydrator\NamingStrategy\CompositeNamingStrategy from zendframework/zend-hydrator instead. + */ +class CompositeNamingStrategy extends BaseCompositeNamingStrategy implements NamingStrategyInterface +{ } diff --git a/src/Hydrator/NamingStrategy/IdentityNamingStrategy.php b/src/Hydrator/NamingStrategy/IdentityNamingStrategy.php index ee4b328d3..be5783917 100644 --- a/src/Hydrator/NamingStrategy/IdentityNamingStrategy.php +++ b/src/Hydrator/NamingStrategy/IdentityNamingStrategy.php @@ -9,21 +9,11 @@ namespace Zend\Stdlib\Hydrator\NamingStrategy; -final class IdentityNamingStrategy implements NamingStrategyInterface -{ - /** - * {@inheritDoc} - */ - public function hydrate($name) - { - return $name; - } +use Zend\Hydrator\NamingStrategy\IdentityNamingStrategy as BaseIdentityNamingStrategy; - /** - * {@inheritDoc} - */ - public function extract($name) - { - return $name; - } +/** + * @deprecated Use Zend\Hydrator\NamingStrategy\IdentityNamingStrategy from zendframework/zend-hydrator instead. + */ +class IdentityNamingStrategy extends BaseIdentityNamingStrategy implements NamingStrategyInterface +{ } diff --git a/src/Hydrator/NamingStrategy/MapNamingStrategy.php b/src/Hydrator/NamingStrategy/MapNamingStrategy.php index 7d04e5787..59e3f7f8f 100644 --- a/src/Hydrator/NamingStrategy/MapNamingStrategy.php +++ b/src/Hydrator/NamingStrategy/MapNamingStrategy.php @@ -9,81 +9,11 @@ namespace Zend\Stdlib\Hydrator\NamingStrategy; -use Zend\Stdlib\Exception\InvalidArgumentException; +use Zend\Hydrator\NamingStrategy\MapNamingStrategy as BaseMapNamingStrategy; -class MapNamingStrategy implements NamingStrategyInterface +/** + * @deprecated Use Zend\Hydrator\NamingStrategy\MapNamingStrategy from zendframework/zend-hydrator instead. + */ +class MapNamingStrategy extends BaseMapNamingStrategy implements NamingStrategyInterface { - /** - * Map for hydrate name conversion. - * - * @var array - */ - protected $mapping = []; - - /** - * Reversed map for extract name conversion. - * - * @var array - */ - protected $reverse = []; - - /** - * Initialize. - * - * @param array $mapping Map for name conversion on hydration - * @param array $reverse Reverse map for name conversion on extraction - */ - public function __construct(array $mapping, array $reverse = null) - { - $this->mapping = $mapping; - $this->reverse = $reverse ?: $this->flipMapping($mapping); - } - - /** - * Safelly flip mapping array. - * - * @param array $array Array to flip - * @return array Flipped array - * @throws InvalidArgumentException - */ - protected function flipMapping(array $array) - { - array_walk($array, function ($value) { - if (!is_string($value) && !is_int($value)) { - throw new InvalidArgumentException('Mapping array can\'t be flipped because of invalid value'); - } - }); - - return array_flip($array); - } - - /** - * Converts the given name so that it can be extracted by the hydrator. - * - * @param string $name The original name - * @return mixed The hydrated name - */ - public function hydrate($name) - { - if (array_key_exists($name, $this->mapping)) { - return $this->mapping[$name]; - } - - return $name; - } - - /** - * Converts the given name so that it can be hydrated by the hydrator. - * - * @param string $name The original name - * @return mixed The extracted name - */ - public function extract($name) - { - if (array_key_exists($name, $this->reverse)) { - return $this->reverse[$name]; - } - - return $name; - } } diff --git a/src/Hydrator/NamingStrategy/NamingStrategyInterface.php b/src/Hydrator/NamingStrategy/NamingStrategyInterface.php index ff99385c1..4d82ee1d6 100644 --- a/src/Hydrator/NamingStrategy/NamingStrategyInterface.php +++ b/src/Hydrator/NamingStrategy/NamingStrategyInterface.php @@ -9,29 +9,13 @@ namespace Zend\Stdlib\Hydrator\NamingStrategy; +use Zend\Hydrator\NamingStrategy\NamingStrategyInterface as BaseNamingStrategyInterface; + /** * Allow property extraction / hydration for hydrator * - * Interface PropertyStrategyInterface - * @package Zend\Stdlib\Hydrator\NamingStrategy + * @deprecated Use Zend\Hydrator\NamingStrategy\NamingStrategyInterface from zendframework/zend-hydrator instead. */ -interface NamingStrategyInterface +interface NamingStrategyInterface extends BaseNamingStrategyInterface { - /** - * Converts the given name so that it can be extracted by the hydrator. - * - * @param string $name The original name - * @param object $object (optional) The original object for context. - * @return mixed The hydrated name - */ - public function hydrate($name); - - /** - * Converts the given name so that it can be hydrated by the hydrator. - * - * @param string $name The original name - * @param array $data (optional) The original data for context. - * @return mixed The extracted name - */ - public function extract($name); } diff --git a/src/Hydrator/NamingStrategy/UnderscoreNamingStrategy.php b/src/Hydrator/NamingStrategy/UnderscoreNamingStrategy.php index 023b4eec7..2b9d6efbc 100644 --- a/src/Hydrator/NamingStrategy/UnderscoreNamingStrategy.php +++ b/src/Hydrator/NamingStrategy/UnderscoreNamingStrategy.php @@ -9,72 +9,11 @@ namespace Zend\Stdlib\Hydrator\NamingStrategy; -use Zend\Filter\FilterChain; +use Zend\Hydrator\NamingStrategy\UnderscoreNamingStrategy as BaseUnderscoreNamingStrategy; -class UnderscoreNamingStrategy implements NamingStrategyInterface +/** + * @deprecated Use Zend\Hydrator\NamingStrategy\UnderscoreNamingStrategy from zendframework/zend-hydrator instead. + */ +class UnderscoreNamingStrategy extends BaseUnderscoreNamingStrategy implements NamingStrategyInterface { - /** - * @var FilterChain|null - */ - protected static $camelCaseToUnderscoreFilter; - - /** - * @var FilterChain|null - */ - protected static $underscoreToStudlyCaseFilter; - - /** - * Remove underscores and capitalize letters - * - * @param string $name - * @return string - */ - public function hydrate($name) - { - return $this->getUnderscoreToStudlyCaseFilter()->filter($name); - } - - /** - * Remove capitalized letters and prepend underscores. - * - * @param string $name - * @return string - */ - public function extract($name) - { - return $this->getCamelCaseToUnderscoreFilter()->filter($name); - } - - /** - * @return FilterChain - */ - protected function getUnderscoreToStudlyCaseFilter() - { - if (static::$underscoreToStudlyCaseFilter instanceof FilterChain) { - return static::$underscoreToStudlyCaseFilter; - } - - $filter = new FilterChain(); - - $filter->attachByName('WordUnderscoreToStudlyCase'); - - return static::$underscoreToStudlyCaseFilter = $filter; - } - - /** - * @return FilterChain - */ - protected function getCamelCaseToUnderscoreFilter() - { - if (static::$camelCaseToUnderscoreFilter instanceof FilterChain) { - return static::$camelCaseToUnderscoreFilter; - } - - $filter = new FilterChain(); - - $filter->attachByName('WordCamelCaseToUnderscore'); - $filter->attachByName('StringToLower'); - - return static::$camelCaseToUnderscoreFilter = $filter; - } } diff --git a/src/Hydrator/NamingStrategyEnabledInterface.php b/src/Hydrator/NamingStrategyEnabledInterface.php index f9c6288a7..8f8acabc5 100644 --- a/src/Hydrator/NamingStrategyEnabledInterface.php +++ b/src/Hydrator/NamingStrategyEnabledInterface.php @@ -9,36 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Hydrator\NamingStrategy\NamingStrategyInterface; +use Zend\Hydrator\NamingStrategyEnabledInterface as BaseNamingStrategyEnabledInterface; -interface NamingStrategyEnabledInterface +/** + * @deprecated Use Zend\Hydrator\NamingStrategy\NamingStrategyEnabledInterface from zendframework/zend-hydrator instead. + */ +interface NamingStrategyEnabledInterface extends BaseNamingStrategyEnabledInterface { - /** - * Adds the given naming strategy - * - * @param NamingStrategyInterface $strategy The naming to register. - * @return NamingStrategyEnabledInterface - */ - public function setNamingStrategy(NamingStrategyInterface $strategy); - - /** - * Gets the naming strategy. - * - * @return NamingStrategyInterface - */ - public function getNamingStrategy(); - - /** - * Checks if a naming strategy exists. - * - * @return bool - */ - public function hasNamingStrategy(); - - /** - * Removes the naming with the given name. - * - * @return NamingStrategyEnabledInterface - */ - public function removeNamingStrategy(); } diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index 8b886eb1e..c846fe5d5 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -9,102 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Exception; -use ReflectionClass; -use ReflectionProperty; +use Zend\Hydrator\ObjectProperty as BaseObjectProperty; -class ObjectProperty extends AbstractHydrator +/** + * @deprecated Use Zend\Hydrator\ObjectProperty from zendframework/zend-hydrator instead. + */ +class ObjectProperty extends BaseObjectProperty implements HydratorInterface { - /** - * @var array[] indexed by class name and then property name - */ - private static $skippedPropertiesCache = []; - - /** - * {@inheritDoc} - * - * Extracts the accessible non-static properties of the given $object. - * - * @throws Exception\BadMethodCallException for a non-object $object - */ - public function extract($object) - { - if (!is_object($object)) { - throw new Exception\BadMethodCallException( - sprintf('%s expects the provided $object to be a PHP object)', __METHOD__) - ); - } - - $data = get_object_vars($object); - $filter = $this->getFilter(); - - foreach ($data as $name => $value) { - // Filter keys, removing any we don't want - if (! $filter->filter($name)) { - unset($data[$name]); - continue; - } - - // Replace name if extracted differ - $extracted = $this->extractName($name, $object); - - if ($extracted !== $name) { - unset($data[$name]); - $name = $extracted; - } - - $data[$name] = $this->extractValue($name, $value, $object); - } - - return $data; - } - - /** - * {@inheritDoc} - * - * Hydrate an object by populating public properties - * - * Hydrates an object by setting public properties of the object. - * - * @throws Exception\BadMethodCallException for a non-object $object - */ - 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__) - ); - } - - $properties = & self::$skippedPropertiesCache[get_class($object)]; - - if (! isset($properties)) { - $reflection = new ReflectionClass($object); - $properties = array_fill_keys( - array_map( - function (ReflectionProperty $property) { - return $property->getName(); - }, - $reflection->getProperties( - ReflectionProperty::IS_PRIVATE - + ReflectionProperty::IS_PROTECTED - + ReflectionProperty::IS_STATIC - ) - ), - true - ); - } - - foreach ($data as $name => $value) { - $property = $this->hydrateName($name, $data); - - if (isset($properties[$property])) { - continue; - } - - $object->$property = $this->hydrateValue($property, $value, $data); - } - - return $object; - } } diff --git a/src/Hydrator/Reflection.php b/src/Hydrator/Reflection.php index a963d1f60..22d8cdfde 100644 --- a/src/Hydrator/Reflection.php +++ b/src/Hydrator/Reflection.php @@ -9,87 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use ReflectionClass; -use Zend\Stdlib\Exception; +use Zend\Hydrator\Reflection as BaseReflection; -class Reflection extends AbstractHydrator +/** + * @deprecated Use Zend\Hydrator\Reflection from zendframework/zend-hydrator instead. + */ +class Reflection extends BaseReflection implements HydratorInterface { - /** - * Simple in-memory array cache of ReflectionProperties used. - * @var \ReflectionProperty[] - */ - protected static $reflProperties = []; - - /** - * Extract values from an object - * - * @param object $object - * @return array - */ - public function extract($object) - { - $result = []; - foreach (self::getReflProperties($object) as $property) { - $propertyName = $this->extractName($property->getName(), $object); - if (!$this->filterComposite->filter($propertyName)) { - continue; - } - - $value = $property->getValue($object); - $result[$propertyName] = $this->extractValue($propertyName, $value, $object); - } - - return $result; - } - - /** - * Hydrate $object with the provided $data. - * - * @param array $data - * @param object $object - * @return object - */ - public function hydrate(array $data, $object) - { - $reflProperties = self::getReflProperties($object); - foreach ($data as $key => $value) { - $name = $this->hydrateName($key, $data); - if (isset($reflProperties[$name])) { - $reflProperties[$name]->setValue($object, $this->hydrateValue($name, $value, $data)); - } - } - return $object; - } - - /** - * Get a reflection properties from in-memory cache and lazy-load if - * class has not been loaded. - * - * @param string|object $input - * @throws Exception\InvalidArgumentException - * @return \ReflectionProperty[] - */ - protected static function getReflProperties($input) - { - if (is_object($input)) { - $input = get_class($input); - } elseif (!is_string($input)) { - throw new Exception\InvalidArgumentException('Input must be a string or an object.'); - } - - if (isset(static::$reflProperties[$input])) { - return static::$reflProperties[$input]; - } - - static::$reflProperties[$input] = []; - $reflClass = new ReflectionClass($input); - $reflProperties = $reflClass->getProperties(); - - foreach ($reflProperties as $property) { - $property->setAccessible(true); - static::$reflProperties[$input][$property->getName()] = $property; - } - - return static::$reflProperties[$input]; - } } diff --git a/src/Hydrator/Strategy/BooleanStrategy.php b/src/Hydrator/Strategy/BooleanStrategy.php index 3c29231f3..d89f50711 100644 --- a/src/Hydrator/Strategy/BooleanStrategy.php +++ b/src/Hydrator/Strategy/BooleanStrategy.php @@ -9,98 +9,13 @@ namespace Zend\Stdlib\Hydrator\Strategy; -use Zend\Stdlib\Exception\InvalidArgumentException; +use Zend\Hydrator\Strategy\BooleanStrategy as BaseBooleanStrategy; /** * This Strategy extracts and hydrates int and string values to Boolean values * - * @package Zend\Stdlib\Hydrator\Strategy + * @deprecated Use Zend\Hydrator\Strategy\BooleanStrategy from zendframework/zend-hydrator instead. */ -final class BooleanStrategy implements StrategyInterface +class BooleanStrategy extends BaseBooleanStrategy implements StrategyInterface { - /** - * @var int|string - */ - private $trueValue; - - /** - * @var int|string - */ - private $falseValue; - - /** - * @param int|string $trueValue - * @param int|string $falseValue - * @throws InvalidArgumentException - */ - public function __construct($trueValue, $falseValue) - { - if (!is_int($trueValue) && !is_string($trueValue)) { - throw new InvalidArgumentException(sprintf( - 'Unable to instantiate BooleanStrategy. Expected int or string as $trueValue. %s was given', - is_object($trueValue) ? get_class($trueValue) : gettype($trueValue) - )); - } - - if (!is_int($falseValue) && !is_string($falseValue)) { - throw new InvalidArgumentException(sprintf( - 'Unable to instantiate BooleanStrategy. Expected int or string as $falseValue. %s was given', - is_object($falseValue) ? get_class($falseValue) : gettype($falseValue) - )); - } - - $this->trueValue = $trueValue; - $this->falseValue = $falseValue; - } - - /** - * Converts the given value so that it can be extracted by the hydrator. - * - * @param bool $value The original value. - * @throws InvalidArgumentException - * @return int|string Returns the value that should be extracted. - */ - public function extract($value) - { - if (!is_bool($value)) { - throw new InvalidArgumentException(sprintf( - 'Unable to extract. Expected bool. %s was given.', - is_object($value) ? get_class($value) : gettype($value) - )); - } - - return $value === true ? $this->trueValue : $this->falseValue; - } - - /** - * Converts the given value so that it can be hydrated by the hydrator. - * - * @param int|string $value The original value. - * @throws InvalidArgumentException - * @return bool Returns the value that should be hydrated. - */ - public function hydrate($value) - { - if (!is_string($value) && !is_int($value)) { - throw new InvalidArgumentException(sprintf( - 'Unable to hydrate. Expected string or int. %s was given.', - is_object($value) ? get_class($value) : gettype($value) - )); - } - - if ($value === $this->trueValue) { - return true; - } - - if ($value === $this->falseValue) { - return false; - } - - throw new InvalidArgumentException(sprintf( - 'Unexpected value %s can\'t be hydrated. Expect %s or %s as Value.', - $value, - $this->trueValue, - $this->falseValue - )); - } } diff --git a/src/Hydrator/Strategy/ClosureStrategy.php b/src/Hydrator/Strategy/ClosureStrategy.php index bf456a7fc..32572226f 100644 --- a/src/Hydrator/Strategy/ClosureStrategy.php +++ b/src/Hydrator/Strategy/ClosureStrategy.php @@ -9,94 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; -class ClosureStrategy implements StrategyInterface -{ - /** - * Function, used in extract method, default: - * function ($value) { - * return $value; - * }; - * @var callable - */ - protected $extractFunc = null; - - /** - * Function, used in hydrate method, default: - * function ($value) { - * return $value; - * }; - * @var callable - */ - protected $hydrateFunc = null; - - /** - * You can describe how your values will extract and hydrate, like this: - * $hydrator->addStrategy('category', new ClosureStrategy( - * function (Category $value) { - * return (int) $value->id; - * }, - * function ($value) { - * return new Category((int) $value); - * } - * )); - * - * @param callable $extractFunc - anonymous function, that extract values - * from object - * @param callable $hydrateFunc - anonymous function, that hydrate values - * into object - */ - public function __construct($extractFunc = null, $hydrateFunc = null) - { - if (isset($extractFunc)) { - if (!is_callable($extractFunc)) { - throw new \Exception('$extractFunc must be callable'); - } - - $this->extractFunc = $extractFunc; - } else { - $this->extractFunc = function ($value) { - return $value; - }; - } - - if (isset($hydrateFunc)) { - if (!is_callable($hydrateFunc)) { - throw new \Exception('$hydrateFunc must be callable'); - } - - $this->hydrateFunc = $hydrateFunc; - } else { - $this->hydrateFunc = function ($value) { - return $value; - }; - } - } +use Zend\Hydrator\Strategy\ClosureStrategy as BaseClosureStrategy; - /** - * Converts the given value so that it can be extracted by the hydrator. - * - * @param mixed $value The original value. - * @param array $object The object is optionally provided as context. - * @return mixed Returns the value that should be extracted. - */ - public function extract($value, $object = null) - { - $func = $this->extractFunc; - - return $func($value, $object); - } - - /** - * Converts the given value so that it can be hydrated by the hydrator. - * - * @param mixed $value The original value. - * @param array $data The whole data is optionally provided as context. - * @return mixed Returns the value that should be hydrated. - */ - public function hydrate($value, $data = null) - { - $func = $this->hydrateFunc; - - return $func($value, $data); - } +/** + * @deprecated Use Zend\Hydrator\Strategy\ClosureStrategy from zendframework/zend-hydrator instead. + */ +class ClosureStrategy extends BaseClosureStrategy implements StrategyInterface +{ } diff --git a/src/Hydrator/Strategy/DateTimeFormatterStrategy.php b/src/Hydrator/Strategy/DateTimeFormatterStrategy.php index 62d92c588..8e633f322 100644 --- a/src/Hydrator/Strategy/DateTimeFormatterStrategy.php +++ b/src/Hydrator/Strategy/DateTimeFormatterStrategy.php @@ -9,72 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; -use DateTime; -use DateTimeZone; +use Zend\Hydrator\Strategy\DateTimeFormatterStrategy as BaseDateTimeFormatterStrategy; -final class DateTimeFormatterStrategy implements StrategyInterface +/** + * @deprecated Use Zend\Hydrator\Strategy\DateTimeFormatterStrategy from zendframework/zend-hydrator instead. + */ +class DateTimeFormatterStrategy extends BaseDateTimeFormatterStrategy implements StrategyInterface { - /** - * @var string - */ - private $format; - - /** - * @var DateTimeZone|null - */ - private $timezone; - - /** - * Constructor - * - * @param string $format - * @param DateTimeZone|null $timezone - */ - public function __construct($format = DateTime::RFC3339, DateTimeZone $timezone = null) - { - $this->format = (string) $format; - $this->timezone = $timezone; - } - - /** - * {@inheritDoc} - * - * Converts to date time string - * - * @param mixed|DateTime $value - * - * @return mixed|string - */ - public function extract($value) - { - if ($value instanceof DateTime) { - return $value->format($this->format); - } - - return $value; - } - - /** - * Converts date time string to DateTime instance for injecting to object - * - * {@inheritDoc} - * - * @param mixed|string $value - * - * @return mixed|DateTime - */ - public function hydrate($value) - { - if ($value === '' || $value === null) { - return; - } - - if ($this->timezone) { - $hydrated = DateTime::createFromFormat($this->format, $value, $this->timezone); - } else { - $hydrated = DateTime::createFromFormat($this->format, $value); - } - - return $hydrated ?: $value; - } } diff --git a/src/Hydrator/Strategy/DefaultStrategy.php b/src/Hydrator/Strategy/DefaultStrategy.php index b2c5c29b4..155c82f0f 100644 --- a/src/Hydrator/Strategy/DefaultStrategy.php +++ b/src/Hydrator/Strategy/DefaultStrategy.php @@ -9,27 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; -class DefaultStrategy implements StrategyInterface -{ - /** - * Converts the given value so that it can be extracted by the hydrator. - * - * @param mixed $value The original value. - * @return mixed Returns the value that should be extracted. - */ - public function extract($value) - { - return $value; - } +use Zend\Hydrator\Strategy\DefaultStrategy as BaseDefaultStrategy; - /** - * Converts the given value so that it can be hydrated by the hydrator. - * - * @param mixed $value The original value. - * @return mixed Returns the value that should be hydrated. - */ - public function hydrate($value) - { - return $value; - } +/** + * @deprecated Use Zend\Hydrator\Strategy\DefaultStrategy from zendframework/zend-hydrator instead. + */ +class DefaultStrategy extends BaseDefaultStrategy implements StrategyInterface +{ } diff --git a/src/Hydrator/Strategy/Exception/ExceptionInterface.php b/src/Hydrator/Strategy/Exception/ExceptionInterface.php index c7b576ce3..16a921be1 100644 --- a/src/Hydrator/Strategy/Exception/ExceptionInterface.php +++ b/src/Hydrator/Strategy/Exception/ExceptionInterface.php @@ -9,6 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy\Exception; -interface ExceptionInterface +use Zend\Hydrator\Strategy\Exception; + +/** + * @deprecated Use Zend\Hydrator\Strategy\Exception\ExceptionInterface from zendframework/zend-hydrator instead. + */ +interface ExceptionInterface extends Exception\ExceptionInterface { } diff --git a/src/Hydrator/Strategy/Exception/InvalidArgumentException.php b/src/Hydrator/Strategy/Exception/InvalidArgumentException.php index 2e6510df4..10f69a244 100644 --- a/src/Hydrator/Strategy/Exception/InvalidArgumentException.php +++ b/src/Hydrator/Strategy/Exception/InvalidArgumentException.php @@ -9,6 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy\Exception; -class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface +use Zend\Hydrator\Strategy\Exception\InvalidArgumentException as BaseInvalidArgumentException; + +/** + * @deprecated Use Zend\Hydrator\Strategy\Exception\InvalidArgumentException from zendframework/zend-hydrator instead. + */ +class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface { } diff --git a/src/Hydrator/Strategy/ExplodeStrategy.php b/src/Hydrator/Strategy/ExplodeStrategy.php index 97c790b0c..ca7e5851c 100644 --- a/src/Hydrator/Strategy/ExplodeStrategy.php +++ b/src/Hydrator/Strategy/ExplodeStrategy.php @@ -9,105 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; -final class ExplodeStrategy implements StrategyInterface -{ - /** - * @var string - */ - private $valueDelimiter; - - /** - * @var int|null - */ - private $explodeLimit; - - /** - * Constructor - * - * @param string $delimiter String that the values will be split upon - * @param int|null $explodeLimit Explode limit - */ - public function __construct($delimiter = ',', $explodeLimit = null) - { - $this->setValueDelimiter($delimiter); - - $this->explodeLimit = ($explodeLimit === null) ? null : (int) $explodeLimit; - } - - /** - * Sets the delimiter string that the values will be split upon - * - * @param string $delimiter - * @return self - */ - private function setValueDelimiter($delimiter) - { - if (!is_string($delimiter)) { - throw new Exception\InvalidArgumentException(sprintf( - '%s expects Delimiter to be string, %s provided instead', - __METHOD__, - is_object($delimiter) ? get_class($delimiter) : gettype($delimiter) - )); - } - - if (empty($delimiter)) { - throw new Exception\InvalidArgumentException('Delimiter cannot be empty.'); - } - - $this->valueDelimiter = $delimiter; - } - - /** - * {@inheritDoc} - * - * Split a string by delimiter - * - * @param string|null $value - * - * @return string[] - * - * @throws Exception\InvalidArgumentException - */ - public function hydrate($value) - { - if (null === $value) { - return []; - } +use Zend\Hydrator\Strategy\ExplodeStrategy as BaseExplodeStrategy; - if (!(is_string($value) || is_numeric($value))) { - throw new Exception\InvalidArgumentException(sprintf( - '%s expects argument 1 to be string, %s provided instead', - __METHOD__, - is_object($value) ? get_class($value) : gettype($value) - )); - } - - if ($this->explodeLimit !== null) { - return explode($this->valueDelimiter, $value, $this->explodeLimit); - } - - return explode($this->valueDelimiter, $value); - } - - /** - * {@inheritDoc} - * - * Join array elements with delimiter - * - * @param string[] $value The original value. - * - * @return string|null - */ - public function extract($value) - { - if (!is_array($value)) { - throw new Exception\InvalidArgumentException(sprintf( - '%s expects argument 1 to be array, %s provided instead', - __METHOD__, - is_object($value) ? get_class($value) : gettype($value) - )); - } - - return empty($value) ? null : implode($this->valueDelimiter, $value); - } +/** + * @deprecated Use Zend\Hydrator\Strategy\ExplodeStrategy from zendframework/zend-hydrator instead. + */ +class ExplodeStrategy extends BaseExplodeStrategy implements StrategyInterface +{ } diff --git a/src/Hydrator/Strategy/SerializableStrategy.php b/src/Hydrator/Strategy/SerializableStrategy.php index 51549ddad..045279231 100644 --- a/src/Hydrator/Strategy/SerializableStrategy.php +++ b/src/Hydrator/Strategy/SerializableStrategy.php @@ -9,115 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; -use Zend\Stdlib\Exception\InvalidArgumentException; -use Zend\Serializer\Adapter\AdapterInterface as SerializerAdapter; -use Zend\Serializer\Serializer as SerializerFactory; +use Zend\Hydrator\Strategy\SerializableStrategy as BaseSerializableStrategy; -class SerializableStrategy implements StrategyInterface +/** + * @deprecated Use Zend\Hydrator\Strategy\SerializableStrategy from zendframework/zend-hydrator instead. + */ +class SerializableStrategy extends BaseSerializableStrategy implements StrategyInterface { - /** - * @var string|SerializerAdapter - */ - protected $serializer; - - /** - * @var array - */ - protected $serializerOptions = []; - - /** - * - * @param mixed $serializer string or SerializerAdapter - * @param mixed $serializerOptions - */ - public function __construct($serializer, $serializerOptions = null) - { - $this->setSerializer($serializer); - if ($serializerOptions) { - $this->setSerializerOptions($serializerOptions); - } - } - - /** - * Serialize the given value so that it can be extracted by the hydrator. - * - * @param mixed $value The original value. - * @return mixed Returns the value that should be extracted. - */ - public function extract($value) - { - $serializer = $this->getSerializer(); - return $serializer->serialize($value); - } - - /** - * Unserialize the given value so that it can be hydrated by the hydrator. - * - * @param mixed $value The original value. - * @return mixed Returns the value that should be hydrated. - */ - public function hydrate($value) - { - $serializer = $this->getSerializer(); - return $serializer->unserialize($value); - } - - /** - * Set serializer - * - * @param string|SerializerAdapter $serializer - * @return SerializableStrategy - */ - public function setSerializer($serializer) - { - if (!is_string($serializer) && !$serializer instanceof SerializerAdapter) { - throw new InvalidArgumentException(sprintf( - '%s expects either a string serializer name or Zend\Serializer\Adapter\AdapterInterface instance; ' - . 'received "%s"', - __METHOD__, - (is_object($serializer) ? get_class($serializer) : gettype($serializer)) - )); - } - $this->serializer = $serializer; - return $this; - } - - /** - * Get serializer - * - * @return SerializerAdapter - */ - public function getSerializer() - { - if (is_string($this->serializer)) { - $options = $this->getSerializerOptions(); - $this->setSerializer(SerializerFactory::factory($this->serializer, $options)); - } elseif (null === $this->serializer) { - $this->setSerializer(SerializerFactory::getDefaultAdapter()); - } - - return $this->serializer; - } - - /** - * Set configuration options for instantiating a serializer adapter - * - * @param mixed $serializerOptions - * @return SerializableStrategy - */ - public function setSerializerOptions($serializerOptions) - { - $this->serializerOptions = $serializerOptions; - return $this; - } - - /** - * Get configuration options for instantiating a serializer adapter - * - * @return mixed - */ - public function getSerializerOptions() - { - return $this->serializerOptions; - } } diff --git a/src/Hydrator/Strategy/StrategyChain.php b/src/Hydrator/Strategy/StrategyChain.php index a9316bbd7..33d2bc13e 100644 --- a/src/Hydrator/Strategy/StrategyChain.php +++ b/src/Hydrator/Strategy/StrategyChain.php @@ -9,65 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; -use Traversable; -use Zend\Stdlib\ArrayUtils; +use Zend\Hydrator\Strategy\StrategyChain as BaseStrategyChain; -final class StrategyChain implements StrategyInterface +/** + * @deprecated Use Zend\Hydrator\Strategy\StrategyChain from zendframework/zend-hydrator instead. + */ +class StrategyChain extends BaseStrategyChain implements StrategyInterface { - /** - * Strategy chain for extraction - * - * @var StrategyInterface[] - */ - private $extractionStrategies; - - /** - * Strategy chain for hydration - * - * @var StrategyInterface[] - */ - private $hydrationStrategies; - - /** - * Constructor - * - * @param array|Traversable $extractionStrategies - */ - public function __construct($extractionStrategies) - { - $extractionStrategies = ArrayUtils::iteratorToArray($extractionStrategies); - $this->extractionStrategies = array_map( - function (StrategyInterface $strategy) { - // this callback is here only to ensure type-safety - return $strategy; - }, - $extractionStrategies - ); - - $this->hydrationStrategies = array_reverse($extractionStrategies); - } - - /** - * {@inheritDoc} - */ - public function extract($value) - { - foreach ($this->extractionStrategies as $strategy) { - $value = $strategy->extract($value); - } - - return $value; - } - - /** - * {@inheritDoc} - */ - public function hydrate($value) - { - foreach ($this->hydrationStrategies as $strategy) { - $value = $strategy->hydrate($value); - } - - return $value; - } } diff --git a/src/Hydrator/Strategy/StrategyInterface.php b/src/Hydrator/Strategy/StrategyInterface.php index 562ec4bd6..a1eb8e906 100644 --- a/src/Hydrator/Strategy/StrategyInterface.php +++ b/src/Hydrator/Strategy/StrategyInterface.php @@ -9,26 +9,11 @@ namespace Zend\Stdlib\Hydrator\Strategy; +use Zend\Hydrator\Strategy\StrategyInterface as BaseStrategyInterface; + /** - * @todo v3.0, add optional object/data to extract/hydrate. + * @deprecated Use Zend\Hydrator\Strategy\StrategyInterface from zendframework/zend-hydrator instead. */ -interface StrategyInterface +interface StrategyInterface extends BaseStrategyInterface { - /** - * Converts the given value so that it can be extracted by the hydrator. - * - * @param mixed $value The original value. - * @param object $object (optional) The original object for context. - * @return mixed Returns the value that should be extracted. - */ - public function extract($value); - - /** - * Converts the given value so that it can be hydrated by the hydrator. - * - * @param mixed $value The original value. - * @param array $data (optional) The original data for context. - * @return mixed Returns the value that should be hydrated. - */ - public function hydrate($value); } diff --git a/src/Hydrator/StrategyEnabledInterface.php b/src/Hydrator/StrategyEnabledInterface.php index 6f29b16a6..9c058a712 100644 --- a/src/Hydrator/StrategyEnabledInterface.php +++ b/src/Hydrator/StrategyEnabledInterface.php @@ -9,40 +9,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; +use Zend\Hydrator\StrategyEnabledInterface as BaseStrategyEnabledInterface; -interface StrategyEnabledInterface +/** + * @deprecated Use Zend\Hydrator\Strategy\StrategyEnabledInterface from zendframework/zend-hydrator instead. + */ +interface StrategyEnabledInterface extends BaseStrategyEnabledInterface { - /** - * Adds the given strategy under the given name. - * - * @param string $name The name of the strategy to register. - * @param StrategyInterface $strategy The strategy to register. - * @return StrategyEnabledInterface - */ - public function addStrategy($name, StrategyInterface $strategy); - - /** - * Gets the strategy with the given name. - * - * @param string $name The name of the strategy to get. - * @return StrategyInterface - */ - public function getStrategy($name); - - /** - * Checks if the strategy with the given name exists. - * - * @param string $name The name of the strategy to check for. - * @return bool - */ - public function hasStrategy($name); - - /** - * Removes the strategy with the given name. - * - * @param string $name The name of the strategy to remove. - * @return StrategyEnabledInterface - */ - public function removeStrategy($name); } diff --git a/test/FilterCompositeTest.php b/test/FilterCompositeTest.php index 02c8e6d86..0e83b91d4 100644 --- a/test/FilterCompositeTest.php +++ b/test/FilterCompositeTest.php @@ -163,9 +163,9 @@ public function testWithComplexCompositeAdded() } /** - * @expectedException Zend\Stdlib\Exception\InvalidArgumentException + * @expectedException Zend\Hydrator\Exception\InvalidArgumentException * @expectedExceptionMessage The value of test should be either a callable - * or an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface + * or an instance of Zend\Hydrator\Filter\FilterInterface */ public function testInvalidParameterConstructorInjection() { @@ -176,9 +176,9 @@ public function testInvalidParameterConstructorInjection() } /** - * @expectedException Zend\Stdlib\Exception\InvalidArgumentException + * @expectedException Zend\Hydrator\Exception\InvalidArgumentException * @expectedExceptionMessage The value of foo should be either a callable - * or an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface + * or an instance of Zend\Hydrator\Filter\FilterInterface */ public function testInvalidFilterInjection() { diff --git a/test/Hydrator/Aggregate/AggregateHydratorFunctionalTest.php b/test/Hydrator/Aggregate/AggregateHydratorFunctionalTest.php index 07e9e3253..2a9521a26 100644 --- a/test/Hydrator/Aggregate/AggregateHydratorFunctionalTest.php +++ b/test/Hydrator/Aggregate/AggregateHydratorFunctionalTest.php @@ -12,8 +12,8 @@ use ArrayObject; use PHPUnit_Framework_TestCase; use Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator; -use Zend\Stdlib\Hydrator\Aggregate\ExtractEvent; -use Zend\Stdlib\Hydrator\Aggregate\HydrateEvent; +use Zend\Hydrator\Aggregate\ExtractEvent; +use Zend\Hydrator\Aggregate\HydrateEvent; use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Stdlib\Hydrator\ClassMethods; use Zend\Stdlib\Hydrator\HydratorInterface; diff --git a/test/Hydrator/Aggregate/AggregateHydratorTest.php b/test/Hydrator/Aggregate/AggregateHydratorTest.php index 1f5cc2484..c06958831 100644 --- a/test/Hydrator/Aggregate/AggregateHydratorTest.php +++ b/test/Hydrator/Aggregate/AggregateHydratorTest.php @@ -44,13 +44,13 @@ public function setUp() */ public function testAdd() { - $attached = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'); + $attached = $this->getMock('Zend\Hydrator\HydratorInterface'); $this ->eventManager ->expects($this->once()) ->method('attachAggregate') - ->with($this->isInstanceOf('Zend\Stdlib\Hydrator\Aggregate\HydratorListener'), 123); + ->with($this->isInstanceOf('Zend\Hydrator\Aggregate\HydratorListener'), 123); $this->hydrator->add($attached, 123); } @@ -66,7 +66,7 @@ public function testHydrate() ->eventManager ->expects($this->once()) ->method('trigger') - ->with($this->isInstanceOf('Zend\Stdlib\Hydrator\Aggregate\HydrateEvent')); + ->with($this->isInstanceOf('Zend\Hydrator\Aggregate\HydrateEvent')); $this->assertSame($object, $this->hydrator->hydrate(['foo' => 'bar'], $object)); } @@ -82,7 +82,7 @@ public function testExtract() ->eventManager ->expects($this->once()) ->method('trigger') - ->with($this->isInstanceOf('Zend\Stdlib\Hydrator\Aggregate\ExtractEvent')); + ->with($this->isInstanceOf('Zend\Hydrator\Aggregate\ExtractEvent')); $this->assertSame([], $this->hydrator->extract($object)); } @@ -103,7 +103,7 @@ public function testGetSetManager() ->method('setIdentifiers') ->with( [ - 'Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator', + 'Zend\Hydrator\Aggregate\AggregateHydrator', 'Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator', ] ); diff --git a/test/Hydrator/Aggregate/HydratorListenerTest.php b/test/Hydrator/Aggregate/HydratorListenerTest.php index 8a28fc96f..9e754de16 100644 --- a/test/Hydrator/Aggregate/HydratorListenerTest.php +++ b/test/Hydrator/Aggregate/HydratorListenerTest.php @@ -37,7 +37,7 @@ class HydratorListenerTest extends PHPUnit_Framework_TestCase */ public function setUp() { - $this->hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'); + $this->hydrator = $this->getMock('Zend\Hydrator\HydratorInterface'); $this->listener = new HydratorListener($this->hydrator); } diff --git a/test/Hydrator/DelegatingHydratorFactoryTest.php b/test/Hydrator/DelegatingHydratorFactoryTest.php index 21810d13c..e5a59ec57 100644 --- a/test/Hydrator/DelegatingHydratorFactoryTest.php +++ b/test/Hydrator/DelegatingHydratorFactoryTest.php @@ -18,7 +18,7 @@ public function testFactory() $hydratorManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); $factory = new DelegatingHydratorFactory(); $this->assertInstanceOf( - 'Zend\Stdlib\Hydrator\DelegatingHydrator', + 'Zend\Hydrator\DelegatingHydrator', $factory->createService($hydratorManager) ); } diff --git a/test/Hydrator/HydratorManagerTest.php b/test/Hydrator/HydratorManagerTest.php index 6376d4214..cf5035157 100644 --- a/test/Hydrator/HydratorManagerTest.php +++ b/test/Hydrator/HydratorManagerTest.php @@ -28,14 +28,14 @@ public function setUp() public function testRegisteringInvalidElementRaisesException() { - $this->setExpectedException('Zend\Stdlib\Exception\RuntimeException'); + $this->setExpectedException('Zend\Hydrator\Exception\RuntimeException'); $this->manager->setService('test', $this); } public function testLoadingInvalidElementRaisesException() { $this->manager->setInvokableClass('test', get_class($this)); - $this->setExpectedException('Zend\Stdlib\Exception\RuntimeException'); + $this->setExpectedException('Zend\Hydrator\Exception\RuntimeException'); $this->manager->get('test'); } } diff --git a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php index 214c82166..f84098244 100644 --- a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php +++ b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php @@ -52,7 +52,7 @@ public function testUsingStringForObjectName() public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass() { - $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException'); + $this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException'); $hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), [], 'not a real class'); } } diff --git a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php index 98bb469f0..58900f7e8 100644 --- a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php +++ b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php @@ -56,7 +56,7 @@ public function testUsingStringForObjectName() public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass() { - $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException'); + $this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException'); $hydratingIterator = new HydratingIteratorIterator( new ArraySerializable(), new ArrayIterator(), diff --git a/test/Hydrator/Strategy/BooleanStrategyTest.php b/test/Hydrator/Strategy/BooleanStrategyTest.php index c8c4f8318..6254a16cc 100644 --- a/test/Hydrator/Strategy/BooleanStrategyTest.php +++ b/test/Hydrator/Strategy/BooleanStrategyTest.php @@ -31,7 +31,7 @@ public function testConstructorWithValidString() public function testExceptionOnWrongTrueValueInConstructor() { $this->setExpectedException( - 'Zend\Stdlib\Exception\InvalidArgumentException', + 'Zend\Hydrator\Exception\InvalidArgumentException', 'Expected int or string as $trueValue.' ); @@ -41,7 +41,7 @@ public function testExceptionOnWrongTrueValueInConstructor() public function testExceptionOnWrongFalseValueInConstructor() { $this->setExpectedException( - 'Zend\Stdlib\Exception\InvalidArgumentException', + 'Zend\Hydrator\Exception\InvalidArgumentException', 'Expected int or string as $falseValue.' ); @@ -67,7 +67,7 @@ public function testExtractThrowsExceptionOnUnknownValue() { $hydrator = new BooleanStrategy(1, 0); - $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException', 'Unable to extract'); + $this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException', 'Unable to extract'); $hydrator->extract(5); } @@ -88,14 +88,14 @@ public function testHydrateInteger() public function testHydrateUnexpectedValueThrowsException() { - $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException', 'Unexpected value'); + $this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException', 'Unexpected value'); $hydrator = new BooleanStrategy(1, 0); $hydrator->hydrate(2); } public function testHydrateInvalidArgument() { - $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException', 'Unable to hydrate'); + $this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException', 'Unable to hydrate'); $hydrator = new BooleanStrategy(1, 0); $hydrator->hydrate(new \stdClass()); } diff --git a/test/Hydrator/Strategy/ExplodeStrategyTest.php b/test/Hydrator/Strategy/ExplodeStrategyTest.php index a7d76684b..a43fa2768 100644 --- a/test/Hydrator/Strategy/ExplodeStrategyTest.php +++ b/test/Hydrator/Strategy/ExplodeStrategyTest.php @@ -40,7 +40,7 @@ public function testGetExceptionWithInvalidArgumentOnExtraction() { $strategy = new ExplodeStrategy(); - $this->setExpectedException('Zend\Stdlib\Hydrator\Strategy\Exception\InvalidArgumentException'); + $this->setExpectedException('Zend\Hydrator\Strategy\Exception\InvalidArgumentException'); $strategy->extract(''); } @@ -54,14 +54,14 @@ public function testGetEmptyArrayWhenHydratingNullValue() public function testGetExceptionWithEmptyDelimiter() { - $this->setExpectedException('Zend\Stdlib\Hydrator\Strategy\Exception\InvalidArgumentException'); + $this->setExpectedException('Zend\Hydrator\Strategy\Exception\InvalidArgumentException'); new ExplodeStrategy(''); } public function testGetExceptionWithInvalidDelimiter() { - $this->setExpectedException('Zend\Stdlib\Hydrator\Strategy\Exception\InvalidArgumentException'); + $this->setExpectedException('Zend\Hydrator\Strategy\Exception\InvalidArgumentException'); new ExplodeStrategy([]); } @@ -80,8 +80,8 @@ public function testHydrateWithInvalidScalarType() $strategy = new ExplodeStrategy(); $this->setExpectedException( - 'Zend\Stdlib\Hydrator\Strategy\Exception\InvalidArgumentException', - 'Zend\Stdlib\Hydrator\Strategy\ExplodeStrategy::hydrate expects argument 1 to be string,' + 'Zend\Hydrator\Strategy\Exception\InvalidArgumentException', + 'Zend\Hydrator\Strategy\ExplodeStrategy::hydrate expects argument 1 to be string,' . ' array provided instead' ); @@ -93,8 +93,8 @@ public function testHydrateWithInvalidObjectType() $strategy = new ExplodeStrategy(); $this->setExpectedException( - 'Zend\Stdlib\Hydrator\Strategy\Exception\InvalidArgumentException', - 'Zend\Stdlib\Hydrator\Strategy\ExplodeStrategy::hydrate expects argument 1 to be string,' + 'Zend\Hydrator\Strategy\Exception\InvalidArgumentException', + 'Zend\Hydrator\Strategy\ExplodeStrategy::hydrate expects argument 1 to be string,' . ' stdClass provided instead' ); @@ -106,8 +106,8 @@ public function testExtractWithInvalidObjectType() $strategy = new ExplodeStrategy(); $this->setExpectedException( - 'Zend\Stdlib\Hydrator\Strategy\Exception\InvalidArgumentException', - 'Zend\Stdlib\Hydrator\Strategy\ExplodeStrategy::extract expects argument 1 to be array,' + 'Zend\Hydrator\Strategy\Exception\InvalidArgumentException', + 'Zend\Hydrator\Strategy\ExplodeStrategy::extract expects argument 1 to be array,' . ' stdClass provided instead' ); diff --git a/test/HydratorDeprecationTest.php b/test/HydratorDeprecationTest.php new file mode 100644 index 000000000..49675fcf8 --- /dev/null +++ b/test/HydratorDeprecationTest.php @@ -0,0 +1,26 @@ +fail('Catchable fatal error was triggered: ' . $errstr); + }, E_RECOVERABLE_ERROR); + $hydratorInjected->setHydrator($hydrator); + $this->assertSame($hydrator, $hydratorInjected->hydrator); + } +} diff --git a/test/Strategy/SerializableStrategyTest.php b/test/Strategy/SerializableStrategyTest.php index 96aba0187..b06be5d82 100644 --- a/test/Strategy/SerializableStrategyTest.php +++ b/test/Strategy/SerializableStrategyTest.php @@ -17,7 +17,7 @@ class SerializableStrategyTest extends TestCase { public function testCannotUseBadArgumentSerilizer() { - $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException'); + $this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException'); $serializerStrategy = new SerializableStrategy(false); } diff --git a/test/TestAsset/ClassMethodsExtendsHydrator.php b/test/TestAsset/ClassMethodsExtendsHydrator.php new file mode 100644 index 000000000..af79cefca --- /dev/null +++ b/test/TestAsset/ClassMethodsExtendsHydrator.php @@ -0,0 +1,228 @@ +setUnderscoreSeparatedKeys($underscoreSeparatedKeys); + $this->setExtractExtendedProperties($extractExtendedProperties); + + $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); + } + + /** + * @param array|Traversable $options + * @return ClassMethodsExtends + * @throws Exception\InvalidArgumentException + */ + 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']); + } + if (isset($options['extractExtendedProperties'])) { + $this->setExtractExtendedProperties($options['extractExtendedProperties']); + } + + return $this; + } + + /** + * @param bool $underscoreSeparatedKeys + * @return ClassMethodsExtends + */ + public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys) + { + $this->underscoreSeparatedKeys = (bool) $underscoreSeparatedKeys; + + if ($this->underscoreSeparatedKeys) { + $this->setNamingStrategy(new UnderscoreNamingStrategy); + } elseif ($this->getNamingStrategy() instanceof UnderscoreNamingStrategy) { + $this->removeNamingStrategy(); + } + + return $this; + } + + /** + * @return bool + */ + public function getUnderscoreSeparatedKeys() + { + return $this->underscoreSeparatedKeys; + } + + /** + * @param bool $extractExtendedProperties + * @return ClassMethodsExtends + */ + public function setExtractExtendedProperties($extractExtendedProperties) + { + $this->extractExtendedProperties = (bool) $extractExtendedProperties; + + if ($this->extractExtendedProperties) { + $this->filterComposite->removeFilter('skipExtended'); + } else { + $this->filterComposite->addFilter('skipExtended', new MethodMatchFilter('getExtended'), FilterComposite::CONDITION_AND); + } + + return $this; + } + + /** + * @return bool + */ + public function getExtractExtendedProperties() + { + return $this->extractExtendedProperties; + } + + /** + * Extract values from an object with class methods + * + * Extracts the getter/setter of the given $object. + * + * @param object $object + * @return array + * @throws Exception\BadMethodCallException for a non-object $object + */ + 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; + } + + /** + * Hydrate an object by populating getter/setter methods + * + * Hydrates an object by getter/setter methods of the object. + * + * @param array $data + * @param object $object + * @return object + * @throws Exception\BadMethodCallException for a non-object $object + */ + 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)); + $value = $this->hydrateValue($property, $value, $data); + if (is_callable(array($object, $method))) { + $object->$method($value); + } else if (is_callable(array($object, 'setExtended'))) { + $object->setExtended($property, $value); + } + } + + return $object; + } +} diff --git a/test/TestAsset/HydratorInjectedObject.php b/test/TestAsset/HydratorInjectedObject.php new file mode 100644 index 000000000..1b7509e8e --- /dev/null +++ b/test/TestAsset/HydratorInjectedObject.php @@ -0,0 +1,24 @@ +hydrator = $hydrator; + } +}