composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Storage / Capabilities.php
blobfd61f3e275e5c11629aaefa42cffc7afb8cc014e
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;
12 use ArrayObject;
13 use stdClass;
14 use Zend\Cache\Exception;
15 use Zend\EventManager\EventsCapableInterface;
17 class Capabilities
19 /**
20 * The storage instance
22 * @var StorageInterface
24 protected $storage;
26 /**
27 * A marker to set/change capabilities
29 * @var stdClass
31 protected $marker;
33 /**
34 * Base capabilities
36 * @var null|Capabilities
38 protected $baseCapabilities;
40 /**
41 * "lock-on-expire" support in seconds.
43 * 0 = Expired items will never be retrieved
44 * >0 = Time in seconds an expired item could be retrieved
45 * -1 = Expired items could be retrieved forever
47 * If it's NULL the capability isn't set and the getter
48 * returns the base capability or the default value.
50 * @var null|bool
52 protected $lockOnExpire;
54 /**
55 * Max. key length
57 * If it's NULL the capability isn't set and the getter
58 * returns the base capability or the default value.
60 * @var null|int
62 protected $maxKeyLength;
64 /**
65 * Min. TTL (0 means items never expire)
67 * If it's NULL the capability isn't set and the getter
68 * returns the base capability or the default value.
70 * @var null|int
72 protected $minTtl;
74 /**
75 * Max. TTL (0 means infinite)
77 * If it's NULL the capability isn't set and the getter
78 * returns the base capability or the default value.
80 * @var null|int
82 protected $maxTtl;
84 /**
85 * Namespace is prefix
87 * If it's NULL the capability isn't set and the getter
88 * returns the base capability or the default value.
90 * @var null|bool
92 protected $namespaceIsPrefix;
94 /**
95 * Namespace separator
97 * If it's NULL the capability isn't set and the getter
98 * returns the base capability or the default value.
100 * @var null|string
102 protected $namespaceSeparator;
105 * Static ttl
107 * If it's NULL the capability isn't set and the getter
108 * returns the base capability or the default value.
110 * @var null|bool
112 protected $staticTtl;
115 * Supported datatypes
117 * If it's NULL the capability isn't set and the getter
118 * returns the base capability or the default value.
120 * @var null|array
122 protected $supportedDatatypes;
125 * Supported metdata
127 * If it's NULL the capability isn't set and the getter
128 * returns the base capability or the default value.
130 * @var null|array
132 protected $supportedMetadata;
135 * TTL precision
137 * If it's NULL the capability isn't set and the getter
138 * returns the base capability or the default value.
140 * @var null|int
142 protected $ttlPrecision;
145 * Use request time
147 * If it's NULL the capability isn't set and the getter
148 * returns the base capability or the default value.
150 * @var null|bool
152 protected $useRequestTime;
155 * Constructor
157 * @param StorageInterface $storage
158 * @param stdClass $marker
159 * @param array $capabilities
160 * @param null|Capabilities $baseCapabilities
162 public function __construct(
163 StorageInterface $storage,
164 stdClass $marker,
165 array $capabilities = [],
166 Capabilities $baseCapabilities = null
168 $this->storage = $storage;
169 $this->marker = $marker;
170 $this->baseCapabilities = $baseCapabilities;
172 foreach ($capabilities as $name => $value) {
173 $this->setCapability($marker, $name, $value);
178 * Get the storage adapter
180 * @return StorageInterface
182 public function getAdapter()
184 return $this->storage;
188 * Get supported datatypes
190 * @return array
192 public function getSupportedDatatypes()
194 return $this->getCapability('supportedDatatypes', [
195 'NULL' => false,
196 'boolean' => false,
197 'integer' => false,
198 'double' => false,
199 'string' => true,
200 'array' => false,
201 'object' => false,
202 'resource' => false,
207 * Set supported datatypes
209 * @param stdClass $marker
210 * @param array $datatypes
211 * @throws Exception\InvalidArgumentException
212 * @return Capabilities Fluent interface
214 public function setSupportedDatatypes(stdClass $marker, array $datatypes)
216 $allTypes = [
217 'array',
218 'boolean',
219 'double',
220 'integer',
221 'NULL',
222 'object',
223 'resource',
224 'string',
227 // check/normalize datatype values
228 foreach ($datatypes as $type => &$toType) {
229 if (! in_array($type, $allTypes)) {
230 throw new Exception\InvalidArgumentException("Unknown datatype '{$type}'");
233 if (is_string($toType)) {
234 $toType = strtolower($toType);
235 if (! in_array($toType, $allTypes)) {
236 throw new Exception\InvalidArgumentException("Unknown datatype '{$toType}'");
238 } else {
239 $toType = (bool) $toType;
243 // add missing datatypes as not supported
244 $missingTypes = array_diff($allTypes, array_keys($datatypes));
245 foreach ($missingTypes as $type) {
246 $datatypes[$type] = false;
249 return $this->setCapability($marker, 'supportedDatatypes', $datatypes);
253 * Get supported metadata
255 * @return array
257 public function getSupportedMetadata()
259 return $this->getCapability('supportedMetadata', []);
263 * Set supported metadata
265 * @param stdClass $marker
266 * @param string[] $metadata
267 * @throws Exception\InvalidArgumentException
268 * @return Capabilities Fluent interface
270 public function setSupportedMetadata(stdClass $marker, array $metadata)
272 foreach ($metadata as $name) {
273 if (! is_string($name)) {
274 throw new Exception\InvalidArgumentException('$metadata must be an array of strings');
277 return $this->setCapability($marker, 'supportedMetadata', $metadata);
281 * Get minimum supported time-to-live
283 * @return int 0 means items never expire
285 public function getMinTtl()
287 return $this->getCapability('minTtl', 0);
291 * Set minimum supported time-to-live
293 * @param stdClass $marker
294 * @param int $minTtl
295 * @throws Exception\InvalidArgumentException
296 * @return Capabilities Fluent interface
298 public function setMinTtl(stdClass $marker, $minTtl)
300 $minTtl = (int) $minTtl;
301 if ($minTtl < 0) {
302 throw new Exception\InvalidArgumentException('$minTtl must be greater or equal 0');
304 return $this->setCapability($marker, 'minTtl', $minTtl);
308 * Get maximum supported time-to-live
310 * @return int 0 means infinite
312 public function getMaxTtl()
314 return $this->getCapability('maxTtl', 0);
318 * Set maximum supported time-to-live
320 * @param stdClass $marker
321 * @param int $maxTtl
322 * @throws Exception\InvalidArgumentException
323 * @return Capabilities Fluent interface
325 public function setMaxTtl(stdClass $marker, $maxTtl)
327 $maxTtl = (int) $maxTtl;
328 if ($maxTtl < 0) {
329 throw new Exception\InvalidArgumentException('$maxTtl must be greater or equal 0');
331 return $this->setCapability($marker, 'maxTtl', $maxTtl);
335 * Is the time-to-live handled static (on write)
336 * or dynamic (on read)
338 * @return bool
340 public function getStaticTtl()
342 return $this->getCapability('staticTtl', false);
346 * Set if the time-to-live handled static (on write) or dynamic (on read)
348 * @param stdClass $marker
349 * @param bool $flag
350 * @return Capabilities Fluent interface
352 public function setStaticTtl(stdClass $marker, $flag)
354 return $this->setCapability($marker, 'staticTtl', (bool) $flag);
358 * Get time-to-live precision
360 * @return float
362 public function getTtlPrecision()
364 return $this->getCapability('ttlPrecision', 1);
368 * Set time-to-live precision
370 * @param stdClass $marker
371 * @param float $ttlPrecision
372 * @throws Exception\InvalidArgumentException
373 * @return Capabilities Fluent interface
375 public function setTtlPrecision(stdClass $marker, $ttlPrecision)
377 $ttlPrecision = (float) $ttlPrecision;
378 if ($ttlPrecision <= 0) {
379 throw new Exception\InvalidArgumentException('$ttlPrecision must be greater than 0');
381 return $this->setCapability($marker, 'ttlPrecision', $ttlPrecision);
385 * Get use request time
387 * @return bool
389 public function getUseRequestTime()
391 return $this->getCapability('useRequestTime', false);
395 * Set use request time
397 * @param stdClass $marker
398 * @param bool $flag
399 * @return Capabilities Fluent interface
401 public function setUseRequestTime(stdClass $marker, $flag)
403 return $this->setCapability($marker, 'useRequestTime', (bool) $flag);
407 * Get if expired items are readable
409 * @return bool
410 * @deprecated This capability has been deprecated and will be removed in the future.
411 * Please use getStaticTtl() instead
413 public function getExpiredRead()
415 trigger_error(
416 'This capability has been deprecated and will be removed in the future. Please use static_ttl instead',
417 E_USER_DEPRECATED
419 return ! $this->getCapability('staticTtl', true);
423 * Set if expired items are readable
425 * @param stdClass $marker
426 * @param bool $flag
427 * @return Capabilities Fluent interface
428 * @deprecated This capability has been deprecated and will be removed in the future.
429 * Please use setStaticTtl() instead
431 public function setExpiredRead(stdClass $marker, $flag)
433 trigger_error(
434 'This capability has been deprecated and will be removed in the future. Please use static_ttl instead',
435 E_USER_DEPRECATED
437 return $this->setCapability($marker, 'staticTtl', (bool) $flag);
441 * Get "lock-on-expire" support in seconds.
443 * @return int 0 = Expired items will never be retrieved
444 * >0 = Time in seconds an expired item could be retrieved
445 * -1 = Expired items could be retrieved forever
447 public function getLockOnExpire()
449 return $this->getCapability('lockOnExpire', 0);
453 * Set "lock-on-expire" support in seconds.
455 * @param stdClass $marker
456 * @param int $timeout
457 * @return Capabilities Fluent interface
459 public function setLockOnExpire(stdClass $marker, $timeout)
461 return $this->setCapability($marker, 'lockOnExpire', (int) $timeout);
465 * Get maximum key length
467 * @return int -1 means unknown, 0 means infinite
469 public function getMaxKeyLength()
471 return $this->getCapability('maxKeyLength', -1);
475 * Set maximum key length
477 * @param stdClass $marker
478 * @param int $maxKeyLength
479 * @throws Exception\InvalidArgumentException
480 * @return Capabilities Fluent interface
482 public function setMaxKeyLength(stdClass $marker, $maxKeyLength)
484 $maxKeyLength = (int) $maxKeyLength;
485 if ($maxKeyLength < -1) {
486 throw new Exception\InvalidArgumentException('$maxKeyLength must be greater or equal than -1');
488 return $this->setCapability($marker, 'maxKeyLength', $maxKeyLength);
492 * Get if namespace support is implemented as prefix
494 * @return bool
496 public function getNamespaceIsPrefix()
498 return $this->getCapability('namespaceIsPrefix', true);
502 * Set if namespace support is implemented as prefix
504 * @param stdClass $marker
505 * @param bool $flag
506 * @return Capabilities Fluent interface
508 public function setNamespaceIsPrefix(stdClass $marker, $flag)
510 return $this->setCapability($marker, 'namespaceIsPrefix', (bool) $flag);
514 * Get namespace separator if namespace is implemented as prefix
516 * @return string
518 public function getNamespaceSeparator()
520 return $this->getCapability('namespaceSeparator', '');
524 * Set the namespace separator if namespace is implemented as prefix
526 * @param stdClass $marker
527 * @param string $separator
528 * @return Capabilities Fluent interface
530 public function setNamespaceSeparator(stdClass $marker, $separator)
532 return $this->setCapability($marker, 'namespaceSeparator', (string) $separator);
536 * Get a capability
538 * @param string $property
539 * @param mixed $default
540 * @return mixed
542 protected function getCapability($property, $default = null)
544 if ($this->$property !== null) {
545 return $this->$property;
546 } elseif ($this->baseCapabilities) {
547 $getMethod = 'get' . $property;
548 return $this->baseCapabilities->$getMethod();
550 return $default;
554 * Change a capability
556 * @param stdClass $marker
557 * @param string $property
558 * @param mixed $value
559 * @return Capabilities Fluent interface
560 * @throws Exception\InvalidArgumentException
562 protected function setCapability(stdClass $marker, $property, $value)
564 if ($this->marker !== $marker) {
565 throw new Exception\InvalidArgumentException('Invalid marker');
568 if ($this->$property !== $value) {
569 $this->$property = $value;
571 // trigger event
572 if ($this->storage instanceof EventsCapableInterface) {
573 $this->storage->getEventManager()->trigger('capability', $this->storage, new ArrayObject([
574 $property => $value
575 ]));
579 return $this;