Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
AbstractHydrator.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009
010 namespace Zend\Stdlib\Hydrator;
011
012 use ArrayObject;
013 use Zend\Stdlib\Exception;
014 use Zend\Stdlib\Hydrator\Filter\FilterComposite;
015 use Zend\Stdlib\Hydrator\NamingStrategy\NamingStrategyInterface;
016 use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
017
018 abstract class AbstractHydrator implements
019 HydratorInterface,
020 StrategyEnabledInterface,
021 FilterEnabledInterface,
022 NamingStrategyEnabledInterface
023 {
024 /**
025 * The list with strategies that this hydrator has.
026 *
027 * @var ArrayObject
028 */
029 protected $strategies;
030
031 /**
032 * An instance of NamingStrategyInterface
033 *
034 * @var NamingStrategyInterface
035 */
036 protected $namingStrategy;
037
038 /**
039 * Composite to filter the methods, that need to be hydrated
040 *
041 * @var Filter\FilterComposite
042 */
043 protected $filterComposite;
044
045 /**
046 * Initializes a new instance of this class.
047 */
048 public function __construct()
049 {
050 $this->strategies = new ArrayObject();
051 $this->filterComposite = new FilterComposite();
052 }
053
054 /**
055 * Gets the strategy with the given name.
056 *
057 * @param string $name The name of the strategy to get.
058 *
059 * @throws \Zend\Stdlib\Exception\InvalidArgumentException
060 * @return StrategyInterface
061 */
062 public function getStrategy($name)
063 {
064 if (isset($this->strategies[$name])) {
065 return $this->strategies[$name];
066 }
067
068 if (!isset($this->strategies['*'])) {
069 throw new Exception\InvalidArgumentException(sprintf(
070 '%s: no strategy by name of "%s", and no wildcard strategy present',
071 __METHOD__,
072 $name
073 ));
074 }
075
076 return $this->strategies['*'];
077 }
078
079 /**
080 * Checks if the strategy with the given name exists.
081 *
082 * @param string $name The name of the strategy to check for.
083 * @return bool
084 */
085 public function hasStrategy($name)
086 {
087 return array_key_exists($name, $this->strategies)
088 || array_key_exists('*', $this->strategies);
089 }
090
091 /**
092 * Adds the given strategy under the given name.
093 *
094 * @param string $name The name of the strategy to register.
095 * @param StrategyInterface $strategy The strategy to register.
096 * @return HydratorInterface
097 */
098 public function addStrategy($name, StrategyInterface $strategy)
099 {
100 $this->strategies[$name] = $strategy;
101 return $this;
102 }
103
104 /**
105 * Removes the strategy with the given name.
106 *
107 * @param string $name The name of the strategy to remove.
108 * @return HydratorInterface
109 */
110 public function removeStrategy($name)
111 {
112 unset($this->strategies[$name]);
113 return $this;
114 }
115
116 /**
117 * Converts a value for extraction. If no strategy exists the plain value is returned.
118 *
119 * @param string $name The name of the strategy to use.
120 * @param mixed $value The value that should be converted.
121 * @param mixed $object The object is optionally provided as context.
122 * @return mixed
123 */
124 public function extractValue($name, $value, $object = null)
125 {
126 if ($this->hasStrategy($name)) {
127 $strategy = $this->getStrategy($name);
128 $value = $strategy->extract($value, $object);
129 }
130 return $value;
131 }
132
133 /**
134 * Converts a value for hydration. If no strategy exists the plain value is returned.
135 *
136 * @param string $name The name of the strategy to use.
137 * @param mixed $value The value that should be converted.
138 * @param array $data The whole data is optionally provided as context.
139 * @return mixed
140 */
141 public function hydrateValue($name, $value, $data = null)
142 {
143 if ($this->hasStrategy($name)) {
144 $strategy = $this->getStrategy($name);
145 $value = $strategy->hydrate($value, $data);
146 }
147 return $value;
148 }
149
150 /**
151 * Convert a name for extraction. If no naming strategy exists, the plain value is returned.
152 *
153 * @param string $name The name to convert.
154 * @param null $object The object is optionally provided as context.
155 * @return mixed
156 */
157 public function extractName($name, $object = null)
158 {
159 if ($this->hasNamingStrategy()) {
160 $name = $this->getNamingStrategy()->extract($name, $object);
161 }
162 return $name;
163 }
164
165 /**
166 * Converts a value for hydration. If no naming strategy exists, the plain value is returned.
167 *
168 * @param string $name The name to convert.
169 * @param array $data The whole data is optionally provided as context.
170 * @return mixed
171 */
172 public function hydrateName($name, $data = null)
173 {
174 if ($this->hasNamingStrategy()) {
175 $name = $this->getNamingStrategy()->hydrate($name, $data);
176 }
177 return $name;
178 }
179
180 /**
181 * Get the filter instance
182 *
183 * @return Filter\FilterComposite
184 */
185 public function getFilter()
186 {
187 return $this->filterComposite;
188 }
189
190 /**
191 * Add a new filter to take care of what needs to be hydrated.
192 * To exclude e.g. the method getServiceLocator:
193 *
194 * <code>
195 * $composite->addFilter("servicelocator",
196 * function ($property) {
197 * list($class, $method) = explode('::', $property);
198 * if ($method === 'getServiceLocator') {
199 * return false;
200 * }
201 * return true;
202 * }, FilterComposite::CONDITION_AND
203 * );
204 * </code>
205 *
206 * @param string $name Index in the composite
207 * @param callable|Filter\FilterInterface $filter
208 * @param int $condition
209 * @return Filter\FilterComposite
210 */
211 public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
212 {
213 return $this->filterComposite->addFilter($name, $filter, $condition);
214 }
215
216 /**
217 * Check whether a specific filter exists at key $name or not
218 *
219 * @param string $name Index in the composite
220 * @return bool
221 */
222 public function hasFilter($name)
223 {
224 return $this->filterComposite->hasFilter($name);
225 }
226
227 /**
228 * Remove a filter from the composition.
229 * To not extract "has" methods, you simply need to unregister it
230 *
231 * <code>
232 * $filterComposite->removeFilter('has');
233 * </code>
234 *
235 * @param $name
236 * @return Filter\FilterComposite
237 */
238 public function removeFilter($name)
239 {
240 return $this->filterComposite->removeFilter($name);
241 }
242
243 /**
244 * Adds the given naming strategy
245 *
246 * @param NamingStrategyInterface $strategy The naming to register.
247 * @return self
248 */
249 public function setNamingStrategy(NamingStrategyInterface $strategy)
250 {
251 $this->namingStrategy = $strategy;
252
253 return $this;
254 }
255
256 /**
257 * Gets the naming strategy.
258 *
259 * @return NamingStrategyInterface
260 */
261 public function getNamingStrategy()
262 {
263 return $this->namingStrategy;
264 }
265
266 /**
267 * Checks if a naming strategy exists.
268 *
269 * @return bool
270 */
271 public function hasNamingStrategy()
272 {
273 return isset($this->namingStrategy);
274 }
275
276 /**
277 * Removes the naming strategy
278 *
279 * @return self
280 */
281 public function removeNamingStrategy()
282 {
283 $this->namingStrategy = null;
284
285 return $this;
286 }
287 }
288