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 / Filter / Boolean.php
blob9de8b9363d43e44d54bda76287b10f65a3ad6fdb
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\Filter;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
15 class Boolean extends AbstractFilter
17 const TYPE_BOOLEAN = 1;
18 const TYPE_INTEGER = 2;
19 const TYPE_FLOAT = 4;
20 const TYPE_STRING = 8;
21 const TYPE_ZERO_STRING = 16;
22 const TYPE_EMPTY_ARRAY = 32;
23 const TYPE_NULL = 64;
24 const TYPE_PHP = 127;
25 const TYPE_FALSE_STRING = 128;
26 const TYPE_LOCALIZED = 256;
27 const TYPE_ALL = 511;
29 /**
30 * @var array
32 protected $constants = array(
33 self::TYPE_BOOLEAN => 'boolean',
34 self::TYPE_INTEGER => 'integer',
35 self::TYPE_FLOAT => 'float',
36 self::TYPE_STRING => 'string',
37 self::TYPE_ZERO_STRING => 'zero',
38 self::TYPE_EMPTY_ARRAY => 'array',
39 self::TYPE_NULL => 'null',
40 self::TYPE_PHP => 'php',
41 self::TYPE_FALSE_STRING => 'false',
42 self::TYPE_LOCALIZED => 'localized',
43 self::TYPE_ALL => 'all',
46 /**
47 * @var array
49 protected $options = array(
50 'type' => self::TYPE_PHP,
51 'casting' => true,
52 'translations' => array(),
55 /**
56 * Constructor
58 * @param array|Traversable|int|null $typeOrOptions
59 * @param bool $casting
60 * @param array $translations
62 public function __construct($typeOrOptions = null, $casting = true, $translations = array())
64 if ($typeOrOptions !== null) {
65 if ($typeOrOptions instanceof Traversable) {
66 $typeOrOptions = ArrayUtils::iteratorToArray($typeOrOptions);
69 if (is_array($typeOrOptions)) {
70 if (isset($typeOrOptions['type'])
71 || isset($typeOrOptions['casting'])
72 || isset($typeOrOptions['translations']))
74 $this->setOptions($typeOrOptions);
75 } else {
76 $this->setType($typeOrOptions);
77 $this->setCasting($casting);
78 $this->setTranslations($translations);
80 } else {
81 $this->setType($typeOrOptions);
82 $this->setCasting($casting);
83 $this->setTranslations($translations);
88 /**
89 * Set boolean types
91 * @param int|array $type
92 * @throws Exception\InvalidArgumentException
93 * @return self
95 public function setType($type = null)
97 if (is_array($type)) {
98 $detected = 0;
99 foreach ($type as $value) {
100 if (is_int($value)) {
101 $detected += $value;
102 } elseif (in_array($value, $this->constants)) {
103 $detected += array_search($value, $this->constants);
107 $type = $detected;
108 } elseif (is_string($type) && in_array($type, $this->constants)) {
109 $type = array_search($type, $this->constants);
112 if (!is_int($type) || ($type < 0) || ($type > self::TYPE_ALL)) {
113 throw new Exception\InvalidArgumentException(sprintf(
114 'Unknown type value "%s" (%s)',
115 $type,
116 gettype($type)
120 $this->options['type'] = $type;
121 return $this;
125 * Returns defined boolean types
127 * @return int
129 public function getType()
131 return $this->options['type'];
135 * Set the working mode
137 * @param bool $flag When true this filter works like cast
138 * When false it recognises only true and false
139 * and all other values are returned as is
140 * @return self
142 public function setCasting($flag = true)
144 $this->options['casting'] = (bool) $flag;
145 return $this;
149 * Returns the casting option
151 * @return bool
153 public function getCasting()
155 return $this->options['casting'];
159 * @param array|Traversable $translations
160 * @throws Exception\InvalidArgumentException
161 * @return self
163 public function setTranslations($translations)
165 if (!is_array($translations) && !$translations instanceof Traversable) {
166 throw new Exception\InvalidArgumentException(sprintf(
167 '"%s" expects an array or Traversable; received "%s"',
168 __METHOD__,
169 (is_object($translations) ? get_class($translations) : gettype($translations))
173 foreach ($translations as $message => $flag) {
174 $this->options['translations'][$message] = (bool) $flag;
177 return $this;
181 * @return array
183 public function getTranslations()
185 return $this->options['translations'];
189 * Defined by Zend\Filter\FilterInterface
191 * Returns a boolean representation of $value
193 * @param string $value
194 * @return string
196 public function filter($value)
198 $type = $this->getType();
199 $casting = $this->getCasting();
201 // LOCALIZED
202 if ($type >= self::TYPE_LOCALIZED) {
203 $type -= self::TYPE_LOCALIZED;
204 if (is_string($value)) {
205 if (isset($this->options['translations'][$value])) {
206 return (bool) $this->options['translations'][$value];
211 // FALSE_STRING ('false')
212 if ($type >= self::TYPE_FALSE_STRING) {
213 $type -= self::TYPE_FALSE_STRING;
214 if (is_string($value) && (strtolower($value) == 'false')) {
215 return false;
218 if (!$casting && is_string($value) && (strtolower($value) == 'true')) {
219 return true;
223 // NULL (null)
224 if ($type >= self::TYPE_NULL) {
225 $type -= self::TYPE_NULL;
226 if ($value === null) {
227 return false;
231 // EMPTY_ARRAY (array())
232 if ($type >= self::TYPE_EMPTY_ARRAY) {
233 $type -= self::TYPE_EMPTY_ARRAY;
234 if (is_array($value) && ($value == array())) {
235 return false;
239 // ZERO_STRING ('0')
240 if ($type >= self::TYPE_ZERO_STRING) {
241 $type -= self::TYPE_ZERO_STRING;
242 if (is_string($value) && ($value == '0')) {
243 return false;
246 if (!$casting && (is_string($value)) && ($value == '1')) {
247 return true;
251 // STRING ('')
252 if ($type >= self::TYPE_STRING) {
253 $type -= self::TYPE_STRING;
254 if (is_string($value) && ($value == '')) {
255 return false;
259 // FLOAT (0.0)
260 if ($type >= self::TYPE_FLOAT) {
261 $type -= self::TYPE_FLOAT;
262 if (is_float($value) && ($value == 0.0)) {
263 return false;
266 if (!$casting && is_float($value) && ($value == 1.0)) {
267 return true;
271 // INTEGER (0)
272 if ($type >= self::TYPE_INTEGER) {
273 $type -= self::TYPE_INTEGER;
274 if (is_int($value) && ($value == 0)) {
275 return false;
278 if (!$casting && is_int($value) && ($value == 1)) {
279 return true;
283 // BOOLEAN (false)
284 if ($type >= self::TYPE_BOOLEAN) {
285 $type -= self::TYPE_BOOLEAN;
286 if (is_bool($value)) {
287 return $value;
291 if ($casting) {
292 return true;
295 return $value;