This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
AbstractHydrator.php
195 lines (176 loc) · 5.53 KB
/
AbstractHydrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Stdlib\Hydrator;
use ArrayObject;
use Zend\Stdlib\Exception;
use Zend\Stdlib\Hydrator\Filter\FilterComposite;
use Zend\Stdlib\Hydrator\StrategyEnabledInterface;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
use Zend\Stdlib\Hydrator\Strategy\DefaultStrategy;
abstract class AbstractHydrator implements HydratorInterface, StrategyEnabledInterface, FilterEnabledInterface
{
/**
* The list with strategies that this hydrator has.
*
* @var ArrayObject
*/
protected $strategies;
/**
* 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.
* @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;
}
/**
* 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);
}
/**
* 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 array $object The object is optionally provided as context.
* @return mixed
*/
public function extractValue($name, $value, $object = null)
{
$strategy = $this->hasStrategy($name) ? $this->getStrategy($name) : new DefaultStrategy();
return $strategy->extract($value, $object);
}
/**
* 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)
{
$strategy = $this->hasStrategy($name) ? $this->getStrategy($name) : new DefaultStrategy();
return $strategy->hydrate($value, $data);
}
}