<?php
namespace Moto\Filter; use Traversable; use Zend\Filter; use Moto; abstract class AbstractFilter implements Filter\FilterInterface { static protected $_zendFilters = array(); static protected $_filters = array(); static protected $_inputFilters = array(); protected $_options = array(); public function __construct($options = array()) { $this->setOptions($options); } public function setOption($key, $value) { $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); if (method_exists($this, $setter)) { $this->{$setter}($value); } elseif (array_key_exists($key, $this->_options)) { $this->_options[$key] = $value; } return $this; } public function getOption($key, $default = null) { return (array_key_exists($key, $this->_options) ? $this->_options[$key] : $default); } public function getOptions() { return $this->_options; } public function setOptions($options) { if (!is_array($options) && !$options instanceof Traversable) { return $this; } foreach ($options as $key => $value) { $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); if (method_exists($this, $setter)) { $this->{$setter}($value); } elseif (array_key_exists($key, $this->_options)) { $this->_options[$key] = $value; } } return $this; } public function __invoke($value) { return $this->filter($value); } protected function _getFilter($name) { if (!array_key_exists($name, self::$_filters)) { $class = '\Moto\\Filter\\' . $name; self::$_filters[$name] = new $class(); } return self::$_filters[$name]; } protected function _getInputFilter($name) { if (!array_key_exists($name, self::$_inputFilters)) { $class = '\Moto\\InputFilter\\' . $name; self::$_inputFilters[$name] = new $class(); } return self::$_inputFilters[$name]; } protected function _filterId($value) { $value = (int)(string)$value; return $value; } protected function _filterNoFollow($value) { $value = (boolean)(int)(string)$value; return $value; } protected function _filterUrl($value) { $value = (string)$value; $filter = $this->_getZendFilter('StripTags'); $value = $filter->filter($value); $filter = $this->_getZendFilter('StringTrim'); $value = $filter->filter($value); return $value; } protected function _getZendFilter($name) { if (!array_key_exists($name, self::$_zendFilters)) { $class = '\\Zend\\Filter\\' . $name; self::$_zendFilters[$name] = new $class(); } return self::$_zendFilters[$name]; } protected function _filterEmail($value) { return $this->_filterString($value); } protected function _filterString($value) { $value = (string)$value; $filter = $this->_getZendFilter('StripTags'); $value = $filter->filter($value); $filter = $this->_getZendFilter('StringTrim'); $value = $filter->filter($value); return $value; } } 