172f5d363f86213b05f6715b8e722962cb884e1c
[yaffs-website] / vendor / zendframework / zend-stdlib / src / AbstractOptions.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
5  * @link      http://github.com/zendframework/zf2 for the canonical source repository
6  * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7  * @license   http://framework.zend.com/license/new-bsd New BSD License
8  */
9
10 namespace Zend\Stdlib;
11
12 use Traversable;
13
14 abstract class AbstractOptions implements ParameterObjectInterface
15 {
16     // @codingStandardsIgnoreStart
17     /**
18      * We use the __ prefix to avoid collisions with properties in
19      * user-implementations.
20      *
21      * @var bool
22      */
23     protected $__strictMode__ = true;
24     // @codingStandardsIgnoreEnd
25
26     /**
27      * Constructor
28      *
29      * @param  array|Traversable|null $options
30      */
31     public function __construct($options = null)
32     {
33         if (null !== $options) {
34             $this->setFromArray($options);
35         }
36     }
37
38     /**
39      * Set one or more configuration properties
40      *
41      * @param  array|Traversable|AbstractOptions $options
42      * @throws Exception\InvalidArgumentException
43      * @return AbstractOptions Provides fluent interface
44      */
45     public function setFromArray($options)
46     {
47         if ($options instanceof self) {
48             $options = $options->toArray();
49         }
50
51         if (! is_array($options) && ! $options instanceof Traversable) {
52             throw new Exception\InvalidArgumentException(
53                 sprintf(
54                     'Parameter provided to %s must be an %s, %s or %s',
55                     __METHOD__,
56                     'array',
57                     'Traversable',
58                     'Zend\Stdlib\AbstractOptions'
59                 )
60             );
61         }
62
63         foreach ($options as $key => $value) {
64             $this->__set($key, $value);
65         }
66
67         return $this;
68     }
69
70     /**
71      * Cast to array
72      *
73      * @return array
74      */
75     public function toArray()
76     {
77         $array = [];
78         $transform = function ($letters) {
79             $letter = array_shift($letters);
80             return '_' . strtolower($letter);
81         };
82         foreach ($this as $key => $value) {
83             if ($key === '__strictMode__') {
84                 continue;
85             }
86             $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
87             $array[$normalizedKey] = $value;
88         }
89         return $array;
90     }
91
92     /**
93      * Set a configuration property
94      *
95      * @see ParameterObject::__set()
96      * @param string $key
97      * @param mixed $value
98      * @throws Exception\BadMethodCallException
99      * @return void
100      */
101     public function __set($key, $value)
102     {
103         $setter = 'set' . str_replace('_', '', $key);
104
105         if (is_callable([$this, $setter])) {
106             $this->{$setter}($value);
107
108             return;
109         }
110
111         if ($this->__strictMode__) {
112             throw new Exception\BadMethodCallException(sprintf(
113                 'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined',
114                 $key,
115                 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))),
116                 $setter
117             ));
118         }
119     }
120
121     /**
122      * Get a configuration property
123      *
124      * @see ParameterObject::__get()
125      * @param string $key
126      * @throws Exception\BadMethodCallException
127      * @return mixed
128      */
129     public function __get($key)
130     {
131         $getter = 'get' . str_replace('_', '', $key);
132
133         if (is_callable([$this, $getter])) {
134             return $this->{$getter}();
135         }
136
137         throw new Exception\BadMethodCallException(sprintf(
138             'The option "%s" does not have a callable "%s" getter method which must be defined',
139             $key,
140             'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))
141         ));
142     }
143
144     /**
145      * Test if a configuration property is null
146      * @see ParameterObject::__isset()
147      * @param string $key
148      * @return bool
149      */
150     public function __isset($key)
151     {
152         $getter = 'get' . str_replace('_', '', $key);
153
154         return method_exists($this, $getter) && null !== $this->__get($key);
155     }
156
157     /**
158      * Set a configuration property to NULL
159      *
160      * @see ParameterObject::__unset()
161      * @param string $key
162      * @throws Exception\InvalidArgumentException
163      * @return void
164      */
165     public function __unset($key)
166     {
167         try {
168             $this->__set($key, null);
169         } catch (Exception\BadMethodCallException $e) {
170             throw new Exception\InvalidArgumentException(
171                 'The class property $' . $key . ' cannot be unset as'
172                 . ' NULL is an invalid value for it',
173                 0,
174                 $e
175             );
176         }
177     }
178 }