1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace Predis\Option;
- class CustomOption implements OptionInterface
- {
- private $filter;
- private $default;
-
- public function __construct(Array $options = array())
- {
- $this->filter = $this->ensureCallable($options, 'filter');
- $this->default = $this->ensureCallable($options, 'default');
- }
-
- private function ensureCallable($options, $key)
- {
- if (!isset($options[$key])) {
- return;
- }
- if (is_callable($callable = $options[$key])) {
- return $callable;
- }
- throw new \InvalidArgumentException("The parameter $key must be callable");
- }
-
- public function filter(ClientOptionsInterface $options, $value)
- {
- if (isset($value)) {
- if ($this->filter === null) {
- return $value;
- }
- return call_user_func($this->filter, $options, $value);
- }
- }
-
- public function getDefault(ClientOptionsInterface $options)
- {
- if (!isset($this->default)) {
- return;
- }
- return call_user_func($this->default, $options);
- }
-
- public function __invoke(ClientOptionsInterface $options, $value)
- {
- if (isset($value)) {
- return $this->filter($options, $value);
- }
- return $this->getDefault($options);
- }
- }
|