ValidateProxy.php

Go to the documentation of this file.
00001 <?php
00002 
00031 class Robo47_Log_Filter_ValidateProxy extends Zend_Log_Filter_Abstract
00032 {
00033 
00038     protected $_validator = null;
00043     protected $_key = null;
00048     protected $_not = false;
00049 
00056     public function __construct($validator, $key = null, $not = false)
00057     {
00058         $this->setValidator($validator);
00059         $this->setKey($key);
00060         $this->setNot($not);
00061     }
00062 
00068     public function getValidator()
00069     {
00070         return $this->_validator;
00071     }
00072 
00077     public function getNot()
00078     {
00079         return $this->_not;
00080     }
00081 
00087     public function getKey()
00088     {
00089         return $this->_key;
00090     }
00091 
00098     public function setNot($not)
00099     {
00100         $not = (bool) $not;
00101         $this->_not = $not;
00102         return $this;
00103     }
00104 
00111     public function setKey($key = null)
00112     {
00113         $this->_key = $key;
00114         return $this;
00115     }
00116 
00123     public function setValidator($validator)
00124     {
00125         if (is_string($validator)) {
00126             $validator = new $validator;
00127         }
00128         if (!$validator instanceof Zend_Validate_Interface) {
00129             $message = 'Validator is not instance of Zend_Validate_Interface';
00130             throw new Robo47_Log_Filter_Exception($message);
00131         }
00132         $this->_validator = $validator;
00133         return $this;
00134     }
00135 
00141     public function getOptions()
00142     {
00143         return array(
00144             'not' => $this->getNot(),
00145             'validator' => $this->getValidator(),
00146             'key' => $this->getKey(),
00147         );
00148     }
00149 
00155     public function accept($event)
00156     {
00157         if (null === $this->_key) {
00158             $validationData = $event;
00159         } elseif (isset($event[$this->_key])) {
00160             $validationData = $event[$this->_key];
00161         } else {
00162             $message = 'key "' . $this->_key . '" not found in event';
00163             throw new Robo47_Log_Filter_Exception($message);
00164         }
00165 
00166         if (true === $this->_not) {
00167             return !$this->_validator->isValid($validationData);
00168         } else {
00169             return $this->_validator->isValid($validationData);
00170         }
00171     }
00172 
00179     static public function factory($config)
00180     {
00181         $config = self::_parseConfig($config);
00182         $config = array_merge(
00183             array(
00184                 'validator' => null,
00185                 'key' => null,
00186                 'not' => false,
00187             ),
00188             $config
00189         );
00190 
00191         return new Robo47_Log_Filter_ValidateProxy(
00192             $config['validator'],
00193             $config['key'],
00194             $config['not']
00195         );
00196     }
00197 }