/** * 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; }
/** * Gets all field names corresponding to the given validation set and * a Piece_Right_Config object. * * @param string $validationSetName * @param Piece_Right_Config $config * @return array * @throws PIECE_RIGHT_ERROR_INVALID_CONFIGURATION * @throws PIECE_RIGHT_ERROR_NOT_FOUND */ function getFieldNames($validationSetName, $config) { $right =& new Piece_Right($this->_configDirectory, $this->_cacheDirectory, $this->_fieldValuesCallback); $fieldNames = $right->getFieldNames($validationSetName, $config); if (Piece_Right_Error::hasErrors('exception')) { return; } return $fieldNames; }
/** * Validates value of all fields. * * @param boolean $isFinals * @since Method available since Release 1.6.0 */ function _validateFields($isFinals) { foreach ($this->_config->getFieldNames() as $fieldName) { $value = $this->_results->getFieldValue($fieldName); if (!$this->_config->forceValidation($fieldName)) { if (!$this->_checkValidationRequirement($fieldName, $value)) { continue; } } $this->_validateField($fieldName, $value, $isFinals); if (Piece_Right_Error::hasErrors()) { return; } } }
/** * Extends a given field using the field in the template. * * @param string $fieldName * @param string $basedOn * @param Piece_Right_Config &$template * @throws PIECE_RIGHT_ERROR_NOT_FOUND * @since Method available since Release 1.8.0 */ function inherit($fieldName, $basedOn, &$template) { if (!$this->_hasField($fieldName)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The field [ {$fieldName} ] is not found."); return; } if (version_compare(phpversion(), '5.0.0', '>=')) { $field = clone $this->_fields[$fieldName]; $this->_fields[$fieldName] = clone $template->getField($basedOn); } else { $field = $this->_fields[$fieldName]; $this->_fields[$fieldName] = $template->getField($basedOn); } if (Piece_Right_Error::hasErrors()) { return; } $this->_fields[$fieldName]->merge($field); }
/** * Gets a Piece_Right_Config object from a configuration file or a cache. * * @param string $masterFile * @param string $cacheDirectory * @return Piece_Right_Config */ function &_getConfiguration($masterFile, $cacheDirectory) { $masterFile = realpath($masterFile); $cache =& new Cache_Lite_File(array('cacheDir' => "{$cacheDirectory}/", 'masterFile' => $masterFile, 'automaticSerialization' => true, 'errorHandlingAPIBreak' => true)); if (!Piece_Right_Env::isProduction()) { $cache->remove($masterFile); } /* * The Cache_Lite class always specifies PEAR_ERROR_RETURN when * calling PEAR::raiseError in default. */ $config = $cache->get($masterFile); if (PEAR::isError($config)) { trigger_error("Cannot read the cache file in the directory [ {$cacheDirectory} ].", E_USER_WARNING); return Piece_Right_Config_Factory::_getConfigurationFromFile($masterFile); } if (!$config) { $config =& Piece_Right_Config_Factory::_getConfigurationFromFile($masterFile); if (Piece_Right_Error::hasErrors()) { $return = null; return $return; } $result = $cache->save($config); if (PEAR::isError($result)) { trigger_error("Cannot write the Piece_Right_Config object to the cache file in the directory [ {$cacheDirectory} ].", E_USER_WARNING); } } return $config; }
/** * 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]; }
/** * Merges the given validation set into the Piece_Right_Config object for * the current validation. * * @param string $validationSetName * @param string $configDirectory * @param string $cacheDirectory * @throws PIECE_UNITY_ERROR_INVOCATION_FAILED * @since Method available since Release 1.3.0 */ function mergeValidationSet($validationSetName, $configDirectory = null, $cacheDirectory = null) { if (is_null($configDirectory)) { $configDirectory = $this->_configDirectory; } if (is_null($cacheDirectory)) { $cacheDirectory = $this->_cacheDirectory; } Piece_Right_Error::disableCallback(); $config =& Piece_Right_Config_Factory::factory($validationSetName, $configDirectory, $cacheDirectory, $this->_template); Piece_Right_Error::enableCallback(); if (Piece_Right_Error::hasErrors()) { Piece_Unity_Error::push(PIECE_UNITY_ERROR_INVOCATION_FAILED, 'Failed to invoke Piece_Right_Validation_Script::mergeValidationSet() for any reasons.', 'exception', array(), Piece_Right_Error::pop()); return; } if (is_null($this->_config)) { $this->_config =& new Piece_Right_Config(); } $this->_config->merge($config); }
/** * 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]; }