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
10 namespace Zend\Cache\Storage\Adapter
;
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
;
22 * Unless otherwise marked, all options in this class affect all adapters.
24 class AdapterOptions
extends AbstractOptions
26 // @codingStandardsIgnoreStart
28 * Prioritized properties ordered by prio to be set first
29 * in case a bulk of options sets set at once
33 protected $__prioritizedProperties__ = [];
34 // @codingStandardsIgnoreEnd
37 * The adapter using these options
39 * @var null|StorageInterface
44 * Validate key against pattern
48 protected $keyPattern = '';
55 protected $namespace = 'zfcache';
62 protected $readable = true;
67 * @var int|float 0 means infinite or maximum of adapter
76 protected $writable = true;
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;
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) {
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',
110 ($error ?
': ' . $error->getMessage() : '')
115 $this->triggerOptionEvent('key_pattern', $keyPattern);
116 $this->keyPattern
= $keyPattern;
127 public function getKeyPattern()
129 return $this->keyPattern
;
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;
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;
176 * If reading data from cache enabled.
180 public function getReadable()
182 return $this->readable
;
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);
206 public function getTtl()
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;
228 * If writing data to cache enabled.
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
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
260 protected function normalizeTtl(&$ttl)
262 if (! is_int($ttl)) {
265 // convert to int if possible
266 if ($ttl === (float) (int) $ttl) {
272 throw new Exception\
InvalidArgumentException("TTL can't be negative");
281 public function toArray()
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__') {
292 $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
293 $array[$normalizedKey] = $value;
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(
320 'Parameter provided to %s must be an %s, %s or %s',
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);