* @license PHP License * @package wb * @subpackage Markup */ WBClass::load('WBMarkup_Handler'); /** * Markup Scanner Handler: Validator * * Validate against policy * * @version 0.1.0 * @package wb * @subpackage Markup */ class WBMarkup_Handler_Validator implements WBMarkup_Handler { /** * @var WBConfig */ private $policy; /** * List of errors * @var array */ private $errors = array(); /** * List of validators * @var array */ private $validators = array(); /** * Constructor * * Initialize configuration object */ public function __construct($parameters = array()) { $this->policy = WBClass::create('WBConfig'); } /** * Set Alternative Policy File * * Use this method to load a different policy file than __default. * @see onScanStart() * @param string */ public function setPolicy($policy = '__default') { $this->policy->load('wxml/policy/' . $policy); } /** * Handler to be called right before scan starts * * Load validator policy if not done already * * @param string $content * @return bool usually true, false to stop the scanner */ public function onScanStart(&$content) { if (!$this->policy->has()) { $this->policy->load('wxml/policy/__default'); } $rules = $this->policy->get('policy/rule'); foreach ($rules as $r) { $val = WBClass::create('WBMarkup_Validator_' . $r['validator']); if (isset($r['params']) && is_array($r['params'])) { $val->configure($r['params']); } $this->validators[] = $val; } return true; } /** * Handler to be called for each opening tag * * Gonna be callled if the Scanner finds a StartElement * * @param string $ns = The used Namespace, if set. Otherwise null * @param string $tag = The tag itself * @param array $attributes = the Attributes of the found Element * @param bool $isEmpty = true if it is a Start- and Closing-Element (e.g.
) * @return bool usually true, false to stop the scanner */ public function onStartElement($ns, $tag, $attributes, $isEmpty) { foreach ($this->validators as $i => $v) { if ($v->validateStartElement($ns, $tag, $attributes, $isEmpty)) { $code = $v->getErrorCode(); if (0 < $code) { $this->errors[] = array( 'code' => $i * 10000 + $code, 'msg' => $v->getErrorMsg() ); } break; } } return true; } /** * Handler to be called if character data comes in * * Gonna be called if the Scanner find cData * * @param string $cData = the cData * @return bool usually true, false to stop the scanner * @return bool usually true, false to stop the scanner */ public function onCharacterData($cData) { return true; } /** * Handler to be called for each closing tag * * Gonna be called if the Scanner finds a EndElement * * @param string $ns = The NameSpace * @param string $tag = The Tag itself * @param bool $empty = Defines if the tag is empty * @return bool usually true, false to stop the scanner */ public function onEndElement($ns, $tag, $empty) { return true; } /** * Handler to be called when scanner reaches the end of input stream * * This function gonna be called when the Scanner was'nt * distrubed by the client and has finished scanning the * whole content string. * * @return bool usually true, false to stop the scanner */ public function onScanComplete() { return true; } /** * Occurs if a entity element will be found inside cdata * * This function gonna be called if the scanner finds a entity element * like e.g.   The Client may then decided want he wants to do with it * * @param $entity = the entity without the surrounding & and ;, e.g. nbsp * @param $isUnicode = true if it is a unicode string, false if not * * @return bool usually true, false to stop the scanner */ public function onEntityElement($entity, $isUnicode) { return true; } /** * Receive result * * This function is meant to be called after scan has completed and * everything went alright. getParsedContent() gives you access to * whatever this handler build up. * * @return mixed contente */ public function getParsedContent() { return ''; } /** * Get list of validation errors * * @return array */ public function getErrors() { return $this->errors; } }