PageRenderTime 71ms CodeModel.GetById 40ms RepoModel.GetById 2ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Filter/Null.php

https://bitbucket.org/pcelta/zf2
PHP | 182 lines | 112 code | 20 blank | 50 comment | 36 complexity | d4edaf74e24402faf879ccee77c2dca3 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Filter
  9. */
  10. namespace Zend\Filter;
  11. use Traversable;
  12. /**
  13. * @category Zend
  14. * @package Zend_Filter
  15. */
  16. class Null extends AbstractFilter
  17. {
  18. const TYPE_BOOLEAN = 1;
  19. const TYPE_INTEGER = 2;
  20. const TYPE_EMPTY_ARRAY = 4;
  21. const TYPE_STRING = 8;
  22. const TYPE_ZERO_STRING = 16;
  23. const TYPE_FLOAT = 32;
  24. const TYPE_ALL = 63;
  25. /**
  26. * @var array
  27. */
  28. protected $constants = array(
  29. self::TYPE_BOOLEAN => 'boolean',
  30. self::TYPE_INTEGER => 'integer',
  31. self::TYPE_EMPTY_ARRAY => 'array',
  32. self::TYPE_STRING => 'string',
  33. self::TYPE_ZERO_STRING => 'zero',
  34. self::TYPE_FLOAT => 'float',
  35. self::TYPE_ALL => 'all',
  36. );
  37. /**
  38. * @var array
  39. */
  40. protected $options = array(
  41. 'type' => self::TYPE_ALL,
  42. );
  43. /**
  44. * Constructor
  45. *
  46. * @param string|array|Traversable $typeOrOptions OPTIONAL
  47. */
  48. public function __construct($typeOrOptions = null)
  49. {
  50. if ($typeOrOptions !== null) {
  51. if ($typeOrOptions instanceof Traversable) {
  52. $typeOrOptions = iterator_to_array($typeOrOptions);
  53. }
  54. if (is_array($typeOrOptions)) {
  55. if (isset($typeOrOptions['type'])) {
  56. $this->setOptions($typeOrOptions);
  57. } else {
  58. $this->setType($typeOrOptions);
  59. }
  60. } else {
  61. $this->setType($typeOrOptions);
  62. }
  63. }
  64. }
  65. /**
  66. * Set boolean types
  67. *
  68. * @param integer|array $type
  69. * @throws Exception\InvalidArgumentException
  70. * @return Boolean
  71. */
  72. public function setType($type = null)
  73. {
  74. if (is_array($type)) {
  75. $detected = 0;
  76. foreach ($type as $value) {
  77. if (is_int($value)) {
  78. $detected += $value;
  79. } elseif (in_array($value, $this->constants)) {
  80. $detected += array_search($value, $this->constants);
  81. }
  82. }
  83. $type = $detected;
  84. } elseif (is_string($type) && in_array($type, $this->constants)) {
  85. $type = array_search($type, $this->constants);
  86. }
  87. if (!is_int($type) || ($type < 0) || ($type > self::TYPE_ALL)) {
  88. throw new Exception\InvalidArgumentException(sprintf(
  89. 'Unknown type value "%s" (%s)',
  90. $type,
  91. gettype($type)
  92. ));
  93. }
  94. $this->options['type'] = $type;
  95. return $this;
  96. }
  97. /**
  98. * Returns defined boolean types
  99. *
  100. * @return int
  101. */
  102. public function getType()
  103. {
  104. return $this->options['type'];
  105. }
  106. /**
  107. * Defined by Zend\Filter\FilterInterface
  108. *
  109. * Returns null representation of $value, if value is empty and matches
  110. * types that should be considered null.
  111. *
  112. * @param string $value
  113. * @return string
  114. */
  115. public function filter($value)
  116. {
  117. $type = $this->getType();
  118. // FLOAT (0.0)
  119. if ($type >= self::TYPE_FLOAT) {
  120. $type -= self::TYPE_FLOAT;
  121. if (is_float($value) && ($value == 0.0)) {
  122. return null;
  123. }
  124. }
  125. // STRING ZERO ('0')
  126. if ($type >= self::TYPE_ZERO_STRING) {
  127. $type -= self::TYPE_ZERO_STRING;
  128. if (is_string($value) && ($value == '0')) {
  129. return null;
  130. }
  131. }
  132. // STRING ('')
  133. if ($type >= self::TYPE_STRING) {
  134. $type -= self::TYPE_STRING;
  135. if (is_string($value) && ($value == '')) {
  136. return null;
  137. }
  138. }
  139. // EMPTY_ARRAY (array())
  140. if ($type >= self::TYPE_EMPTY_ARRAY) {
  141. $type -= self::TYPE_EMPTY_ARRAY;
  142. if (is_array($value) && ($value == array())) {
  143. return null;
  144. }
  145. }
  146. // INTEGER (0)
  147. if ($type >= self::TYPE_INTEGER) {
  148. $type -= self::TYPE_INTEGER;
  149. if (is_int($value) && ($value == 0)) {
  150. return null;
  151. }
  152. }
  153. // BOOLEAN (false)
  154. if ($type >= self::TYPE_BOOLEAN) {
  155. $type -= self::TYPE_BOOLEAN;
  156. if (is_bool($value) && ($value == false)) {
  157. return null;
  158. }
  159. }
  160. return $value;
  161. }
  162. }