/** * 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; }
/** * Loads a class. * * @param string $class * @param string $directory * @throws PIECE_RIGHT_ERROR_NOT_READABLE * @throws PIECE_RIGHT_ERROR_NOT_FOUND * @throws PIECE_RIGHT_ERROR_CANNOT_READ * @static */ function load($class, $directory = null) { $file = str_replace('_', '/', $class) . '.php'; if (!is_null($directory)) { $file = "{$directory}/{$file}"; if (!file_exists($file)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The class file [ {$file} ] for the class [ {$class} ] not found."); return; } if (!is_readable($file)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_READABLE, "The class file [ {$file} ] is not readable."); return; } } if (!(include_once $file)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_CANNOT_READ, "The class file [ {$file} ] not found or is not readable."); } }
function tearDown() { foreach (array_keys($_POST) as $field) { unset($_POST[$field]); } unset($_SERVER['REQUEST_METHOD']); $this->_postRunCallbackCalled = false; $this->_resultsViaCallback = null; $cache =& new Cache_Lite_File(array('cacheDir' => "{$this->_cacheDirectory}/", 'masterFile' => '', 'automaticSerialization' => true, 'errorHandlingAPIBreak' => true)); $cache->clean(); Piece_Right_Error::clearErrors(); }
/** * 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); }
function tearDown() { $this->_config = null; Piece_Right_Error::clearErrors(); Piece_Right_Error::popCallback(); }
/** * Parses the given file and returns a Piece_Right_Config object. * * @param string $file * @return Piece_Right_Config * @throws PIECE_RIGHT_ERROR_INVALID_CONFIGURATION */ function &_getConfigurationFromFile($file) { $config =& new Piece_Right_Config(); $yaml = Spyc::YAMLLoad($file); foreach ($yaml as $validation) { if (!array_key_exists('name', $validation)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_INVALID_CONFIGURATION, "A configuration in the configuration file [ {$file} ] has no 'name' element."); $return = null; return $return; } $config->addField($validation['name']); if (array_key_exists('required', $validation)) { $config->setRequired($validation['name'], (array) $validation['required']); } if (array_key_exists('filter', $validation)) { foreach ((array) $validation['filter'] as $filter) { $config->addFilter($validation['name'], $filter); } } if (array_key_exists('validator', $validation)) { foreach ((array) $validation['validator'] as $validator) { $config->addValidation($validation['name'], $validator['name'], (array) @$validator['rule'], @$validator['message']); } } if (array_key_exists('watcher', $validation)) { $config->setWatcher($validation['name'], (array) $validation['watcher']); } if (array_key_exists('pseudo', $validation)) { $config->setPseudo($validation['name'], (array) $validation['pseudo']); } if (array_key_exists('description', $validation)) { $config->setDescription($validation['name'], $validation['description']); } if (array_key_exists('forceValidation', $validation)) { $config->setForceValidation($validation['name'], $validation['forceValidation']); } if (array_key_exists('finals', $validation)) { foreach ((array) $validation['finals'] as $validator) { $config->addValidation($validation['name'], $validator['name'], (array) @$validator['rule'], @$validator['message'], true); } } if (array_key_exists('basedOn', $validation)) { $config->setBasedOn($validation['name'], $validation['basedOn']); } } return $config; }
/** * 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; }
/** * Enables the last callback. * * @since Method available since Release 1.10.0 */ function enableCallback() { Piece_Right_Error::popCallback(); }
/** * 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]; }
function tearDown() { foreach (array_keys($_POST) as $field) { unset($_POST[$field]); } unset($_SERVER['REQUEST_METHOD']); Piece_Right_Validator_Factory::clearInstances(); $GLOBALS['PIECE_RIGHT_Validator_Directories'] = $this->_oldValidatorDirectories; Piece_Right_Filter_Factory::clearInstances(); $GLOBALS['PIECE_RIGHT_Filter_Directories'] = $this->_oldFilterDirectories; $cache =& new Cache_Lite_File(array('cacheDir' => "{$this->_cacheDirectory}/", 'masterFile' => '', 'automaticSerialization' => true, 'errorHandlingAPIBreak' => true)); $cache->clean(); Piece_Right_Error::clearErrors(); Piece_Right_Error::popCallback(); }