PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Filter/AbstractFilter.php

http://github.com/zendframework/zf2
PHP | 100 lines | 52 code | 9 blank | 39 comment | 5 complexity | ace8144207a819c6ae833469fd8a603b MD5 | raw file
Possible License(s): BSD-3-Clause
  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. namespace Zend\Filter;
  10. use Traversable;
  11. use Zend\Stdlib\StringUtils;
  12. abstract class AbstractFilter implements FilterInterface
  13. {
  14. /**
  15. * Filter options
  16. *
  17. * @var array
  18. */
  19. protected $options = array();
  20. /**
  21. * @return bool
  22. * @deprecated Since 2.1.0
  23. */
  24. public static function hasPcreUnicodeSupport()
  25. {
  26. return StringUtils::hasPcreUnicodeSupport();
  27. }
  28. /**
  29. * @param array|Traversable $options
  30. * @return self
  31. * @throws Exception\InvalidArgumentException
  32. */
  33. public function setOptions($options)
  34. {
  35. if (!is_array($options) && !$options instanceof Traversable) {
  36. throw new Exception\InvalidArgumentException(sprintf(
  37. '"%s" expects an array or Traversable; received "%s"',
  38. __METHOD__,
  39. (is_object($options) ? get_class($options) : gettype($options))
  40. ));
  41. }
  42. foreach ($options as $key => $value) {
  43. $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
  44. if (method_exists($this, $setter)) {
  45. $this->{$setter}($value);
  46. } elseif (array_key_exists($key, $this->options)) {
  47. $this->options[$key] = $value;
  48. } else {
  49. throw new Exception\InvalidArgumentException(
  50. sprintf(
  51. 'The option "%s" does not have a matching %s setter method or options[%s] array key',
  52. $key,
  53. $setter,
  54. $key
  55. )
  56. );
  57. }
  58. }
  59. return $this;
  60. }
  61. /**
  62. * Retrieve options representing object state
  63. *
  64. * @return array
  65. */
  66. public function getOptions()
  67. {
  68. return $this->options;
  69. }
  70. /**
  71. * Invoke filter as a command
  72. *
  73. * Proxies to {@link filter()}
  74. *
  75. * @param mixed $value
  76. * @throws Exception\ExceptionInterface If filtering $value is impossible
  77. * @return mixed
  78. */
  79. public function __invoke($value)
  80. {
  81. return $this->filter($value);
  82. }
  83. /**
  84. * @param mixed $options
  85. * @return bool
  86. */
  87. protected static function isOptions($options)
  88. {
  89. return (is_array($options) || $options instanceof Traversable);
  90. }
  91. }