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
10 namespace Zend\Filter
;
13 use Zend\Stdlib\ArrayUtils
;
15 class Boolean
extends AbstractFilter
17 const TYPE_BOOLEAN
= 1;
18 const TYPE_INTEGER
= 2;
20 const TYPE_STRING
= 8;
21 const TYPE_ZERO_STRING
= 16;
22 const TYPE_EMPTY_ARRAY
= 32;
25 const TYPE_FALSE_STRING
= 128;
26 const TYPE_LOCALIZED
= 256;
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',
49 protected $options = array(
50 'type' => self
::TYPE_PHP
,
52 'translations' => array(),
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);
76 $this->setType($typeOrOptions);
77 $this->setCasting($casting);
78 $this->setTranslations($translations);
81 $this->setType($typeOrOptions);
82 $this->setCasting($casting);
83 $this->setTranslations($translations);
91 * @param int|array $type
92 * @throws Exception\InvalidArgumentException
95 public function setType($type = null)
97 if (is_array($type)) {
99 foreach ($type as $value) {
100 if (is_int($value)) {
102 } elseif (in_array($value, $this->constants
)) {
103 $detected +
= array_search($value, $this->constants
);
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)',
120 $this->options
['type'] = $type;
125 * Returns defined boolean types
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
142 public function setCasting($flag = true)
144 $this->options
['casting'] = (bool) $flag;
149 * Returns the casting option
153 public function getCasting()
155 return $this->options
['casting'];
159 * @param array|Traversable $translations
160 * @throws Exception\InvalidArgumentException
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"',
169 (is_object($translations) ?
get_class($translations) : gettype($translations))
173 foreach ($translations as $message => $flag) {
174 $this->options
['translations'][$message] = (bool) $flag;
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
196 public function filter($value)
198 $type = $this->getType();
199 $casting = $this->getCasting();
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')) {
218 if (!$casting && is_string($value) && (strtolower($value) == 'true')) {
224 if ($type >= self
::TYPE_NULL
) {
225 $type -= self
::TYPE_NULL
;
226 if ($value === null) {
231 // EMPTY_ARRAY (array())
232 if ($type >= self
::TYPE_EMPTY_ARRAY
) {
233 $type -= self
::TYPE_EMPTY_ARRAY
;
234 if (is_array($value) && ($value == array())) {
240 if ($type >= self
::TYPE_ZERO_STRING
) {
241 $type -= self
::TYPE_ZERO_STRING
;
242 if (is_string($value) && ($value == '0')) {
246 if (!$casting && (is_string($value)) && ($value == '1')) {
252 if ($type >= self
::TYPE_STRING
) {
253 $type -= self
::TYPE_STRING
;
254 if (is_string($value) && ($value == '')) {
260 if ($type >= self
::TYPE_FLOAT
) {
261 $type -= self
::TYPE_FLOAT
;
262 if (is_float($value) && ($value == 0.0)) {
266 if (!$casting && is_float($value) && ($value == 1.0)) {
272 if ($type >= self
::TYPE_INTEGER
) {
273 $type -= self
::TYPE_INTEGER
;
274 if (is_int($value) && ($value == 0)) {
278 if (!$casting && is_int($value) && ($value == 1)) {
284 if ($type >= self
::TYPE_BOOLEAN
) {
285 $type -= self
::TYPE_BOOLEAN
;
286 if (is_bool($value)) {