Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Stdlib / Hydrator / AbstractHydrator.php
blob729260f910f1a5384f1e0425a928f44d15dd3960
1 <?php
2 /**
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
8 */
10 namespace Zend\Stdlib\Hydrator;
12 use ArrayObject;
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
20 /**
21 * The list with strategies that this hydrator has.
23 * @var ArrayObject
25 protected $strategies;
27 /**
28 * Composite to filter the methods, that need to be hydrated
29 * @var Filter\FilterComposite
31 protected $filterComposite;
33 /**
34 * Initializes a new instance of this class.
36 public function __construct()
38 $this->strategies = new ArrayObject();
39 $this->filterComposite = new FilterComposite();
42 /**
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',
57 __METHOD__,
58 $name
59 ));
62 return $this->strategies['*'];
65 /**
66 * Checks if the strategy with the given name exists.
68 * @param string $name The name of the strategy to check for.
69 * @return bool
71 public function hasStrategy($name)
73 return array_key_exists($name, $this->strategies)
74 || array_key_exists('*', $this->strategies);
77 /**
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;
87 return $this;
90 /**
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]);
99 return $this;
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.
108 * @return mixed
110 public function extractValue($name, $value, $object = null)
112 if ($this->hasStrategy($name)) {
113 $strategy = $this->getStrategy($name);
114 $value = $strategy->extract($value, $object);
116 return $value;
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.
125 * @return mixed
127 public function hydrateValue($name, $value, $data = null)
129 if ($this->hasStrategy($name)) {
130 $strategy = $this->getStrategy($name);
131 $value = $strategy->hydrate($value, $data);
133 return $value;
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:
150 * <code>
151 * $composite->addFilter("servicelocator",
152 * function ($property) {
153 * list($class, $method) = explode('::', $property);
154 * if ($method === 'getServiceLocator') {
155 * return false;
157 * return true;
158 * }, FilterComposite::CONDITION_AND
159 * );
160 * </code>
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
176 * @return bool
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
187 * <code>
188 * $filterComposite->removeFilter('has');
189 * </code>
191 * @param $name
192 * @return Filter\FilterComposite
194 public function removeFilter($name)
196 return $this->filterComposite->removeFilter($name);