composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Storage / Adapter / AdapterOptions.php
blob1ea4abe0624d7928bd4160713ac2ecb1a92f83a5
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-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Cache\Storage\Adapter;
12 use ArrayObject;
13 use Traversable;
14 use Zend\Cache\Exception;
15 use Zend\Cache\Storage\Event;
16 use Zend\Cache\Storage\StorageInterface;
17 use Zend\EventManager\EventsCapableInterface;
18 use Zend\Stdlib\AbstractOptions;
19 use Zend\Stdlib\ErrorHandler;
21 /**
22 * Unless otherwise marked, all options in this class affect all adapters.
24 class AdapterOptions extends AbstractOptions
26 // @codingStandardsIgnoreStart
27 /**
28 * Prioritized properties ordered by prio to be set first
29 * in case a bulk of options sets set at once
31 * @var string[]
33 protected $__prioritizedProperties__ = [];
34 // @codingStandardsIgnoreEnd
36 /**
37 * The adapter using these options
39 * @var null|StorageInterface
41 protected $adapter;
43 /**
44 * Validate key against pattern
46 * @var string
48 protected $keyPattern = '';
50 /**
51 * Namespace option
53 * @var string
55 protected $namespace = 'zfcache';
57 /**
58 * Readable option
60 * @var bool
62 protected $readable = true;
64 /**
65 * TTL option
67 * @var int|float 0 means infinite or maximum of adapter
69 protected $ttl = 0;
71 /**
72 * Writable option
74 * @var bool
76 protected $writable = true;
78 /**
79 * Adapter using this instance
81 * @param StorageInterface|null $adapter
82 * @return AdapterOptions Provides a fluent interface
84 public function setAdapter(StorageInterface $adapter = null)
86 $this->adapter = $adapter;
87 return $this;
90 /**
91 * Set key pattern
93 * @param string $keyPattern
94 * @throws Exception\InvalidArgumentException
95 * @return AdapterOptions Provides a fluent interface
97 public function setKeyPattern($keyPattern)
99 $keyPattern = (string) $keyPattern;
100 if ($this->keyPattern !== $keyPattern) {
101 // validate pattern
102 if ($keyPattern !== '') {
103 ErrorHandler::start(E_WARNING);
104 $result = preg_match($keyPattern, '');
105 $error = ErrorHandler::stop();
106 if ($result === false) {
107 throw new Exception\InvalidArgumentException(sprintf(
108 'Invalid pattern "%s"%s',
109 $keyPattern,
110 ($error ? ': ' . $error->getMessage() : '')
111 ), 0, $error);
115 $this->triggerOptionEvent('key_pattern', $keyPattern);
116 $this->keyPattern = $keyPattern;
119 return $this;
123 * Get key pattern
125 * @return string
127 public function getKeyPattern()
129 return $this->keyPattern;
133 * Set namespace.
135 * @param string $namespace
136 * @return AdapterOptions Provides a fluent interface
138 public function setNamespace($namespace)
140 $namespace = (string) $namespace;
141 if ($this->namespace !== $namespace) {
142 $this->triggerOptionEvent('namespace', $namespace);
143 $this->namespace = $namespace;
146 return $this;
150 * Get namespace
152 * @return string
154 public function getNamespace()
156 return $this->namespace;
160 * Enable/Disable reading data from cache.
162 * @param bool $readable
163 * @return AdapterOptions Provides a fluent interface
165 public function setReadable($readable)
167 $readable = (bool) $readable;
168 if ($this->readable !== $readable) {
169 $this->triggerOptionEvent('readable', $readable);
170 $this->readable = $readable;
172 return $this;
176 * If reading data from cache enabled.
178 * @return bool
180 public function getReadable()
182 return $this->readable;
186 * Set time to live.
188 * @param int|float $ttl
189 * @return AdapterOptions Provides a fluent interface
191 public function setTtl($ttl)
193 $this->normalizeTtl($ttl);
194 if ($this->ttl !== $ttl) {
195 $this->triggerOptionEvent('ttl', $ttl);
196 $this->ttl = $ttl;
198 return $this;
202 * Get time to live.
204 * @return float
206 public function getTtl()
208 return $this->ttl;
212 * Enable/Disable writing data to cache.
214 * @param bool $writable
215 * @return AdapterOptions Provides a fluent interface
217 public function setWritable($writable)
219 $writable = (bool) $writable;
220 if ($this->writable !== $writable) {
221 $this->triggerOptionEvent('writable', $writable);
222 $this->writable = $writable;
224 return $this;
228 * If writing data to cache enabled.
230 * @return bool
232 public function getWritable()
234 return $this->writable;
238 * Triggers an option event if this options instance has a connection to
239 * an adapter implements EventsCapableInterface.
241 * @param string $optionName
242 * @param mixed $optionValue
243 * @return void
245 protected function triggerOptionEvent($optionName, $optionValue)
247 if ($this->adapter instanceof EventsCapableInterface) {
248 $event = new Event('option', $this->adapter, new ArrayObject([$optionName => $optionValue]));
249 $this->adapter->getEventManager()->triggerEvent($event);
254 * Validates and normalize a TTL.
256 * @param int|float $ttl
257 * @throws Exception\InvalidArgumentException
258 * @return void
260 protected function normalizeTtl(&$ttl)
262 if (! is_int($ttl)) {
263 $ttl = (float) $ttl;
265 // convert to int if possible
266 if ($ttl === (float) (int) $ttl) {
267 $ttl = (int) $ttl;
271 if ($ttl < 0) {
272 throw new Exception\InvalidArgumentException("TTL can't be negative");
277 * Cast to array
279 * @return array
281 public function toArray()
283 $array = [];
284 $transform = function ($letters) {
285 $letter = array_shift($letters);
286 return '_' . strtolower($letter);
288 foreach ($this as $key => $value) {
289 if ($key === '__strictMode__' || $key === '__prioritizedProperties__') {
290 continue;
292 $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
293 $array[$normalizedKey] = $value;
295 return $array;
299 * {@inheritdoc}
301 * NOTE: This method was overwritten just to support prioritized properties
302 * {@link https://github.com/zendframework/zf2/issues/6381}
304 * @param array|Traversable|AbstractOptions $options
305 * @throws Exception\InvalidArgumentException
306 * @return AbstractOptions Provides fluent interface
308 public function setFromArray($options)
310 if ($this->__prioritizedProperties__) {
311 if ($options instanceof AbstractOptions) {
312 $options = $options->toArray();
315 if ($options instanceof Traversable) {
316 $options = iterator_to_array($options);
317 } elseif (! is_array($options)) {
318 throw new Exception\InvalidArgumentException(
319 sprintf(
320 'Parameter provided to %s must be an %s, %s or %s',
321 __METHOD__,
322 'array',
323 'Traversable',
324 'Zend\Stdlib\AbstractOptions'
329 // Sort prioritized options to top
330 $options = array_change_key_case($options, CASE_LOWER);
331 foreach (array_reverse($this->__prioritizedProperties__) as $key) {
332 if (isset($options[$key])) {
333 $options = [$key => $options[$key]] + $options;
334 } elseif (isset($options[($key = str_replace('_', '', $key))])) {
335 $options = [$key => $options[$key]] + $options;
340 return parent::setFromArray($options);