/** * 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."); } }
/** * 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); }
/** * Watches the target fields and turns the fields requirements * on/off. * * @throws PIECE_RIGHT_ERROR_NOT_FOUND * @since Method available since Release 0.3.0 */ function _watch() { $fieldNames = $this->_config->getFieldNames(); foreach ($fieldNames as $fieldName) { $watcher = $this->_config->getWatcher($fieldName); if (!is_array($watcher)) { continue; } $found = false; foreach ($watcher['target'] as $target) { if ($target['name'] == $fieldName) { $found = true; break; } } if (!$found) { $watcher['target'][] = array('name' => $fieldName); } if (!array_key_exists('turnOn', $watcher)) { $watcher['turnOn'] = array(); foreach ($watcher['target'] as $target) { if (!in_array($target['name'], $watcher['turnOn'])) { $watcher['turnOn'][] = $target['name']; } } } if (!in_array($fieldName, $watcher['turnOn'])) { $watcher['turnOn'][] = $fieldName; } if (!array_key_exists('turnOff', $watcher)) { $watcher['turnOff'] = array(); } $turnOnFields = array(); $turnOffFields = array(); foreach ($watcher['target'] as $target) { $targetValue = $this->_results->getFieldValue($target['name']); if (Piece_Right::isEmpty($targetValue)) { continue; } if ($this->_shouldBeTurnedOn($target, $targetValue)) { foreach ($watcher['turnOn'] as $turnOnFieldName) { $turnOnFields[] = $turnOnFieldName; } foreach ($watcher['turnOff'] as $turnOffFieldName) { $turnOffFields[] = $turnOffFieldName; } } } foreach ($turnOnFields as $turnOnFieldName) { if (!in_array($turnOnFieldName, $fieldNames)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The field [ {$turnOnFieldName} ] which is a target of turn on is not found in the configuration."); return; } $this->_config->setRequired($turnOnFieldName, array('enabled' => true)); } foreach ($turnOffFields as $turnOffFieldName) { if (!in_array($turnOffFieldName, $fieldNames)) { Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_FOUND, "The field [ {$turnOffFieldName} ] which is a target of turn off is not found in the configuration."); return; } $this->_config->setRequired($turnOffFieldName, array('enabled' => false)); } if (array_key_exists('turnOnForceValidation', $watcher) && $watcher['turnOnForceValidation'] && count($turnOnFields)) { $this->_config->setForceValidation($fieldName); } } }
/** * 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; }
/** * 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 a field value by the given validations. * * @param string $fieldName * @param string $value * @param boolean $isFinals * @throws PIECE_RIGHT_ERROR_NOT_FOUND * @throws PIECE_RIGHT_ERROR_INVALID_VALIDATOR * @throws PIECE_RIGHT_ERROR_CANNOT_READ * @throws PIECE_RIGHT_ERROR_NOT_READABLE * @since Method available since Release 0.3.0 */ function _validateField($fieldName, $value, $isFinals) { foreach ($this->_config->getValidations($fieldName) as $validation) { if ($validation['useInFinals'] != $isFinals) { continue; } $validator =& Piece_Right_Validator_Factory::factory($validation['validator']); if (Piece_Right_Error::hasErrors('exception')) { return; } if (is_array($value) && !$validator->isArrayable()) { Piece_Right_Error::pushCallback(create_function('$error', 'return ' . PEAR_ERRORSTACK_PUSHANDLOG . ';')); Piece_Right_Error::push(PIECE_RIGHT_ERROR_NOT_ARRAYABLE, "The value of the field [ {$fieldName} ] is an array, but the validator [ {$validation['validator']} ] is not arrayable. This validation is skipped.", 'warning'); Piece_Right_Error::popCallback(); $this->_results->addError($fieldName, $validation['validator'], $validator->getMessage()); continue; } $validator->setResults($this->_results); $validator->setRules($validation['rules']); $validator->setMessage($validation['message']); $validator->setPayload($this->_payload); if (!$validator->validate($value)) { $this->_results->addError($fieldName, $validation['validator'], $validator->getMessage()); } } }
/** * 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]; }