3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib\Hydrator
;
13 use Zend\Stdlib\Exception
;
14 use Zend\Stdlib\Hydrator\Filter\FilterComposite
;
15 use Zend\Stdlib\Hydrator\StrategyEnabledInterface
;
16 use Zend\Stdlib\Hydrator\Strategy\StrategyInterface
;
18 abstract class AbstractHydrator
implements HydratorInterface
, StrategyEnabledInterface
21 * The list with strategies that this hydrator has.
25 protected $strategies;
28 * Composite to filter the methods, that need to be hydrated
29 * @var Filter\FilterComposite
31 protected $filterComposite;
34 * Initializes a new instance of this class.
36 public function __construct()
38 $this->strategies
= new ArrayObject();
39 $this->filterComposite
= new FilterComposite();
43 * Gets the strategy with the given name.
45 * @param string $name The name of the strategy to get.
46 * @return StrategyInterface
48 public function getStrategy($name)
50 if (isset($this->strategies
[$name])) {
51 return $this->strategies
[$name];
54 if (!isset($this->strategies
['*'])) {
55 throw new Exception\
InvalidArgumentException(sprintf(
56 '%s: no strategy by name of "%s", and no wildcard strategy present',
62 return $this->strategies
['*'];
66 * Checks if the strategy with the given name exists.
68 * @param string $name The name of the strategy to check for.
71 public function hasStrategy($name)
73 return array_key_exists($name, $this->strategies
)
74 ||
array_key_exists('*', $this->strategies
);
78 * Adds the given strategy under the given name.
80 * @param string $name The name of the strategy to register.
81 * @param StrategyInterface $strategy The strategy to register.
82 * @return HydratorInterface
84 public function addStrategy($name, StrategyInterface
$strategy)
86 $this->strategies
[$name] = $strategy;
91 * Removes the strategy with the given name.
93 * @param string $name The name of the strategy to remove.
94 * @return HydratorInterface
96 public function removeStrategy($name)
98 unset($this->strategies
[$name]);
103 * Converts a value for extraction. If no strategy exists the plain value is returned.
105 * @param string $name The name of the strategy to use.
106 * @param mixed $value The value that should be converted.
107 * @param array $object The object is optionally provided as context.
110 public function extractValue($name, $value, $object = null)
112 if ($this->hasStrategy($name)) {
113 $strategy = $this->getStrategy($name);
114 $value = $strategy->extract($value, $object);
120 * Converts a value for hydration. If no strategy exists the plain value is returned.
122 * @param string $name The name of the strategy to use.
123 * @param mixed $value The value that should be converted.
124 * @param array $data The whole data is optionally provided as context.
127 public function hydrateValue($name, $value, $data = null)
129 if ($this->hasStrategy($name)) {
130 $strategy = $this->getStrategy($name);
131 $value = $strategy->hydrate($value, $data);
137 * Get the filter instance
139 * @return Filter\FilterComposite
141 public function getFilter()
143 return $this->filterComposite
;
147 * Add a new filter to take care of what needs to be hydrated.
148 * To exclude e.g. the method getServiceLocator:
151 * $composite->addFilter("servicelocator",
152 * function ($property) {
153 * list($class, $method) = explode('::', $property);
154 * if ($method === 'getServiceLocator') {
158 * }, FilterComposite::CONDITION_AND
162 * @param string $name Index in the composite
163 * @param callable|Filter\FilterInterface $filter
164 * @param int $condition
165 * @return Filter\FilterComposite
167 public function addFilter($name, $filter, $condition = FilterComposite
::CONDITION_OR
)
169 return $this->filterComposite
->addFilter($name, $filter, $condition);
173 * Check whether a specific filter exists at key $name or not
175 * @param string $name Index in the composite
178 public function hasFilter($name)
180 return $this->filterComposite
->hasFilter($name);
184 * Remove a filter from the composition.
185 * To not extract "has" methods, you simply need to unregister it
188 * $filterComposite->removeFilter('has');
192 * @return Filter\FilterComposite
194 public function removeFilter($name)
196 return $this->filterComposite
->removeFilter($name);