Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Stdlib / AbstractOptions.php
blob0e68c29e11c7c39ac5fa340d66d5fe0250c879dd
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-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Stdlib;
12 use Traversable;
14 abstract class AbstractOptions implements ParameterObjectInterface
16 /**
17 * We use the __ prefix to avoid collisions with properties in
18 * user-implementations.
20 * @var bool
22 protected $__strictMode__ = true;
24 /**
25 * Constructor
27 * @param array|Traversable|null $options
29 public function __construct($options = null)
31 if (null !== $options) {
32 $this->setFromArray($options);
36 /**
37 * Set one or more configuration properties
39 * @param array|Traversable|AbstractOptions $options
40 * @throws Exception\InvalidArgumentException
41 * @return AbstractOptions Provides fluent interface
43 public function setFromArray($options)
45 if ($options instanceof self) {
46 $options = $options->toArray();
49 if (!is_array($options) && !$options instanceof Traversable) {
50 throw new Exception\InvalidArgumentException(sprintf(
51 'Parameter provided to %s must be an %s, %s or %s',
52 __METHOD__, 'array', 'Traversable', 'Zend\Stdlib\AbstractOptions'
53 ));
56 foreach ($options as $key => $value) {
57 $this->__set($key, $value);
60 return $this;
63 /**
64 * Cast to array
66 * @return array
68 public function toArray()
70 $array = array();
71 $transform = function ($letters) {
72 $letter = array_shift($letters);
73 return '_' . strtolower($letter);
75 foreach ($this as $key => $value) {
76 if ($key === '__strictMode__') continue;
77 $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
78 $array[$normalizedKey] = $value;
80 return $array;
83 /**
84 * Set a configuration property
86 * @see ParameterObject::__set()
87 * @param string $key
88 * @param mixed $value
89 * @throws Exception\BadMethodCallException
90 * @return void
92 public function __set($key, $value)
94 $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
95 if ($this->__strictMode__ && !method_exists($this, $setter)) {
96 throw new Exception\BadMethodCallException(
97 'The option "' . $key . '" does not '
98 . 'have a matching ' . $setter . ' setter method '
99 . 'which must be defined'
101 } elseif (!$this->__strictMode__ && !method_exists($this, $setter)) {
102 return;
104 $this->{$setter}($value);
108 * Get a configuration property
110 * @see ParameterObject::__get()
111 * @param string $key
112 * @throws Exception\BadMethodCallException
113 * @return mixed
115 public function __get($key)
117 $getter = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
118 if (!method_exists($this, $getter)) {
119 throw new Exception\BadMethodCallException(
120 'The option "' . $key . '" does not '
121 . 'have a matching ' . $getter . ' getter method '
122 . 'which must be defined'
126 return $this->{$getter}();
130 * Test if a configuration property is null
131 * @see ParameterObject::__isset()
132 * @param string $key
133 * @return bool
135 public function __isset($key)
137 return null !== $this->__get($key);
141 * Set a configuration property to NULL
143 * @see ParameterObject::__unset()
144 * @param string $key
145 * @throws Exception\InvalidArgumentException
146 * @return void
148 public function __unset($key)
150 try {
151 $this->__set($key, null);
152 } catch (Exception\BadMethodCallException $e) {
153 throw new Exception\InvalidArgumentException(
154 'The class property $' . $key . ' cannot be unset as'
155 . ' NULL is an invalid value for it',