/** * Validates the value of a field with an arbitrary method. * * @param mixed $value * @return boolean * @throws PIECE_RIGHT_ERROR_NOT_FOUND */ function validate($value) { $class = $this->_getRule('class'); $method = $this->_getRule('method'); $isStatic = $this->_getRule('isStatic'); if (is_null($class) || is_null($method)) { return false; } if (!Piece_Right_ClassLoader::loaded($class)) { Piece_Right_ClassLoader::load($class, $this->_getRule('directory')); if (Piece_Right_Error::hasErrors()) { return; } if (!Piece_Right_ClassLoader::loaded($class)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The class [ {$class} ] is not found in the loaded file.", 'exception', array('validator' => __CLASS__)); return; } } if ($isStatic) { $callback = array($class, $method); } else { $instance =& new $class(); $callback = array(&$instance, $method); } if (!is_array($value)) { return call_user_func_array($callback, array($value, &$this->_payload, &$this->_results)); } foreach ($value as $target) { if (!call_user_func_array($callback, array($target, &$this->_payload, &$this->_results))) { return false; } } return true; }
/** * Creates a filter object from the filter directories. * * @param string $filterName * @return mixed * @throws PIECE_RIGHT_ERROR_NOT_FOUND * @throws PIECE_RIGHT_ERROR_CANNOT_READ */ function &factory($filterName) { if (!array_key_exists($filterName, $GLOBALS['PIECE_RIGHT_Filter_Instances'])) { $found = false; foreach ($GLOBALS['PIECE_RIGHT_Filter_Prefixes'] as $prefixAlias) { $filterClass = Piece_Right_Filter_Factory::_getFilterClass($filterName, $prefixAlias); if (Piece_Right_ClassLoader::loaded($filterClass)) { $found = true; break; } } if (!$found) { foreach ($GLOBALS['PIECE_RIGHT_Filter_Directories'] as $filterDirectory) { foreach ($GLOBALS['PIECE_RIGHT_Filter_Prefixes'] as $prefixAlias) { $filterClass = Piece_Right_Filter_Factory::_getFilterClass($filterName, $prefixAlias); Piece_Right_Error::disableCallback(); Piece_Right_ClassLoader::load($filterClass, $filterDirectory); Piece_Right_Error::enableCallback(); if (Piece_Right_Error::hasErrors()) { $error = Piece_Right_Error::pop(); if ($error['code'] == PIECE_RIGHT_ERROR_NOT_FOUND) { continue; } Piece_Right_Error::push(PIECE_RIGHT_ERROR_CANNOT_READ, "Failed to read the filter [ {$filterName} ] for any reasons.", 'exception', array(), $error); $return = null; return $return; } if (Piece_Right_ClassLoader::loaded($filterClass)) { $found = true; break 2; } } } if (!$found) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The filter [ {$filterName} ] is not found in the following directories:\n" . implode("\n", $GLOBALS['PIECE_RIGHT_Filter_Directories'])); $return = null; return $return; } } $GLOBALS['PIECE_RIGHT_Filter_Instances'][$filterName] =& new $filterClass(); } return $GLOBALS['PIECE_RIGHT_Filter_Instances'][$filterName]; }
/** * Creates a validator object from the validator directories. * * @param string $validatorName * @return mixed * @throws PIECE_RIGHT_ERROR_NOT_FOUND * @throws PIECE_RIGHT_ERROR_INVALID_VALIDATOR * @throws PIECE_RIGHT_ERROR_CANNOT_READ */ function &factory($validatorName) { if (!array_key_exists($validatorName, $GLOBALS['PIECE_RIGHT_Validator_Instances'])) { $found = false; foreach ($GLOBALS['PIECE_RIGHT_Validator_Prefixes'] as $prefixAlias) { $validatorClass = Piece_Right_Validator_Factory::_getValidatorClass($validatorName, $prefixAlias); if (Piece_Right_ClassLoader::loaded($validatorClass)) { $found = true; break; } } if (!$found) { foreach ($GLOBALS['PIECE_RIGHT_Validator_Directories'] as $validatorDirectory) { foreach ($GLOBALS['PIECE_RIGHT_Validator_Prefixes'] as $prefixAlias) { $validatorClass = Piece_Right_Validator_Factory::_getValidatorClass($validatorName, $prefixAlias); Piece_Right_Error::disableCallback(); Piece_Right_ClassLoader::load($validatorClass, $validatorDirectory); Piece_Right_Error::enableCallback(); if (Piece_Right_Error::hasErrors()) { $error = Piece_Right_Error::pop(); if ($error['code'] == PIECE_RIGHT_ERROR_NOT_FOUND) { continue; } Piece_Right_Error::push(PIECE_RIGHT_ERROR_CANNOT_READ, "Failed to read the validator [ {$validatorName} ] for any reasons.", 'exception', array(), $error); $return = null; return $return; } if (Piece_Right_ClassLoader::loaded($validatorClass)) { $found = true; break 2; } } } if (!$found) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The validator [ {$validatorName} ] is not found in the following directories:\n" . implode("\n", $GLOBALS['PIECE_RIGHT_Validator_Directories'])); $return = null; return $return; } } $validator =& new $validatorClass($prefixAlias); if (!is_subclass_of($validator, 'Piece_Right_Validator_Common')) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_INVALID_VALIDATOR, "The validator [ {$validatorName} ] is invalid."); $return = null; return $return; } $GLOBALS['PIECE_RIGHT_Validator_Instances'][$validatorName] =& $validator; } else { $GLOBALS['PIECE_RIGHT_Validator_Instances'][$validatorName]->clear(); } return $GLOBALS['PIECE_RIGHT_Validator_Instances'][$validatorName]; }