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

Deprecate hydrator classes #20

Merged
merged 16 commits into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
}
},
"require": {
"php": ">=5.5"
"php": ">=5.5",
"zendframework/zend-hydrator": "~1.0"
},
"require-dev": {
"zendframework/zend-config": "~2.5",
Expand Down
14 changes: 6 additions & 8 deletions src/Extractor/ExtractionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
278 changes: 5 additions & 273 deletions src/Hydrator/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
* <code>
* $composite->addFilter("servicelocator",
* function ($property) {
* list($class, $method) = explode('::', $property);
* if ($method === 'getServiceLocator') {
* return false;
* }
* return true;
* }, FilterComposite::CONDITION_AND
* );
* </code>
*
* @param string $name Index in the composite
* @param callable|Filter\FilterInterface $filter
* @param int $condition
* @return Filter\FilterComposite
*/
public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
{
return $this->filterComposite->addFilter($name, $filter, $condition);
}

/**
* Check whether a specific filter exists at key $name or not
*
* @param string $name Index in the composite
* @return bool
*/
public function hasFilter($name)
{
return $this->filterComposite->hasFilter($name);
}

/**
* Remove a filter from the composition.
* To not extract "has" methods, you simply need to unregister it
*
* <code>
* $filterComposite->removeFilter('has');
* </code>
*
* @param $name
* @return Filter\FilterComposite
*/
public function removeFilter($name)
{
return $this->filterComposite->removeFilter($name);
}

/**
* 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;
}
}
Loading