[Solar-svn] Revision 2635
pmjones at solarphp.com
pmjones at solarphp.com
Fri Jul 27 15:49:04 CDT 2007
Branch: removed Solar_Filter_Chain in preparation for rolling the functions into Solar_Filter
Deleted: branches/orm/Solar/Filter/Chain.php
===================================================================
--- branches/orm/Solar/Filter/Chain.php 2007-07-27 01:18:42 UTC (rev 2634)
+++ branches/orm/Solar/Filter/Chain.php 2007-07-27 20:49:04 UTC (rev 2635)
@@ -1,374 +0,0 @@
-<?php
-/**
- *
- * Applies a chain (or stack) of filters to an array of data: validates and
- * sanitizes, and retains messages about invalid elements.
- *
- * @category Solar
- *
- * @package Solar_Filter
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Applies a chain (or stack) of filters to an array of data: validates and
- * sanitizes, and retains messages about invalid elements.
- *
- * @category Solar
- *
- * @package Solar_Filter
- *
- */
-
-class Solar_Filter_Chain extends Solar_Base {
-
- /**
- *
- * User-defined configuration values.
- *
- * Keys are ...
- *
- * `filter`
- * : (dependency) A Solar_Filter dependency object. Default is null,
- * which creates a new Solar_Filter object.
- *
- * @var array
- *
- */
- protected $_Solar_Filter_Chain = array(
- 'filter' => null,
- );
-
- /**
- *
- * The object used for filtering data with validate*() and sanitize*()
- * methods.
- *
- * @var object
- *
- */
- protected $_filter_object = null;
-
- /**
- *
- * The object used for generating "invalid" messages.
- *
- * Defaults to $this.
- *
- * @var Solar_Base
- *
- */
- protected $_locale_object = null;
-
- /**
- *
- * The chain of filters to be applied to the data array.
- *
- * Format is 'data_key' => array(), where the sub-array is a sequential
- * array of callbacks.
- *
- * For example, this will filter the $data['rank'] value to validate as
- * an integer in the range 0-9.
- *
- * $this->_filters = array(
- * 'rank' => array(
- * 'validateInt',
- * array('validateRange', 0, 9),
- * )
- * );
- *
- * @var array
- *
- * @see addFilter()
- *
- * @see addFilters()
- *
- * @see process()
- *
- */
- protected $_filters = array();
-
- /**
- *
- * Tells the filter chain if a particular data key is required.
- *
- * The key is the data key name, the value is a boolean (true if required,
- * false if not).
- *
- * @var array
- *
- * @see setRequire()
- *
- */
- protected $_require = array();
-
- /**
- *
- * After processing, this contains the list of validation failure messages
- * for each data key.
- *
- * @var array
- *
- */
- protected $_invalid = array();
-
- /**
- *
- * The data array to be filtered by the chain.
- *
- * @var array
- *
- * @see process()
- *
- */
- protected $_data;
-
- /**
- *
- * The name of the data key currently being processed.
- *
- * @var string
- *
- * @see process()
- *
- */
- protected $_key;
-
- /**
- *
- * Sets the filter object used for processing.
- *
- * @param Solar_Filter $obj The filter object. When empty, uses a standard
- * Solar_Filter object.
- *
- * @return void
- *
- */
- public function setFilterObject($obj)
- {
- $this->_filter_object = $obj;
- }
-
- /**
- *
- * Sets the object used for getting locale() translations.
- *
- * @param Solar_Base $obj Any Solar_Base object with a locale() method.
- * When empty, uses $this for locale().
- *
- * @return void
- *
- */
- public function setLocaleObject($obj)
- {
- $this->_locale_object = $obj;
- }
-
- /**
- *
- * Sets whether or not a particular data key is required to be present and
- * non-blank in the data.
- *
- * @param string $key The data key.
- *
- * @param bool $flag True if required, false if not.
- *
- * @return void
- *
- */
- public function setRequire($key, $flag)
- {
- $this->_require[$key] = (bool) $flag;
- }
-
- /**
- *
- * Adds one filter for a data key.
- *
- * @param string $key The data key.
- *
- * @param string|array $spec The filter specification. If a string, it's
- * just a method name. If an array, the first element is a method name,
- * and all remaining elements are parameters to that method.
- *
- * @return void
- *
- */
- public function addFilter($key, $spec)
- {
- $this->_filters[$key][] = (array) $spec;
- }
-
- /**
- *
- * Adds many filters for one data key.
- *
- * @param array $list An array of data keys and filter specifications.
- *
- * @return void
- *
- */
- public function addFilters($key, $list)
- {
- foreach ((array) $list as $spec) {
- $this->addFilter($key, $spec);
- }
- }
-
- /**
- *
- * Resets the filters and required keys.
- *
- * @param string $key Reset only this key. If empty, resets all keys.
- *
- * @return void
- *
- */
- public function reset($key = null)
- {
- if ($key === null) {
- $this->_filters = array();
- $this->_require = array();
- $this->_invalid = array();
- } else {
- unset($this->_filters[$key]);
- unset($this->_require[$key]);
- unset($this->_invalid[$key]);
- }
- }
-
- /**
- *
- * Gets the list of invalid keys and feedback messages.
- *
- * @param string $key Get messages for only this key. If empty, returns
- * messages for all keys.
- *
- * @return array
- *
- */
- public function getInvalid($key = null)
- {
- if ($key === null) {
- return $this->_invalid;
- } elseif (! empty($this->_invalid[$key])) {
- return $this->_invalid[$key];
- }
- }
-
- /**
- *
- * Processes an array of data in-place with the chain of filters.
- *
- * @param array &$data A reference to the data to be filtered; sanitizing
- * methods will be applied to the data directly, so the data is modified
- * in-place.
- *
- * @return bool True if all elements were validated, and all required keys
- * were present and non-blank; false if not validated or a key was missing
- * or blank.
- *
- */
- public function process(&$data)
- {
- // keep the data as a property, because some extended Filter classes
- // may need to refer to various pieces of data for validation.
- $this->_data =& $data;
-
- // reset the list of invalid keys
- $this->_invalid = array();
-
- // if we don't have a filter object already, get one as a dependency
- if (! $this->_filter_object) {
- $this->_filter_object = Solar::dependency(
- 'Solar_Filter',
- $this->_config['filter']
- );
- }
-
- // if we don't have a locale object, use self
- if (! $this->_locale_object) {
- $this->_locale_object = $this;
- }
-
- // see if we actually have all the required data keys
- foreach ($this->_required as $key => $flag) {
-
- if (! $flag) {
- // not required
- continue;
- }
-
- // "blank" means the key does not exist in the data, or that it
- // validates as a blank value
- $blank = ! array_key_exists($key, $this->_data) ||
- $this->_filter_object->validateBlank($this->_data[$key]);
-
- // is it blank?
- if ($blank) {
- $this->_invalid[$key][] = $this->_locale_object->locale(
- 'VALIDATE_NOTBLANK'
- );
- }
- }
-
- // check the filters
- foreach ($this->_data as $key => &$val) {
-
- // keep the key name
- $this->_key = $key;
-
- // are there filters on this key?
- if (empty($this->_filters[$key])) {
- continue;
- }
-
- // is this key required?
- if (! empty($this->_require[$key])) {
- $this->_filter_object->setRequire(true);
- } else {
- $this->_filter_object->setRequire(false);
- }
-
- // run the filter series for the key
- foreach ($this->_filters[$key] as $params) {
-
- // take the method name off the top of the params ...
- $method = array_shift($params);
-
- // ... and put the value in its place.
- array_unshift($params, $val);
-
- // call the filtering method
- $result = call_user_func_array(
- array($this, $method),
- $params
- );
-
- // what to do with the result?
- $type = strtolower(substr($method, 0, 8));
- if ($type == 'sanitize') {
- // retain the sanitized value
- $val = $result;
- } elseif ($type == 'validate' && ! $result) {
- // a validation method failed; use the method name as
- // the locale translation key, converting from camelCase
- // to camel_Case, then to CAMEL_CASE.
- $tmp = preg_replace('/([a-z])([A-Z])/', '$1_$2', $method);
- $tmp = strtoupper($tmp);
- $invalid[$key] = $this->_locale_object->locale($tmp);
- }
- }
- }
-
- // return the validation status; if not empty, it's not valid.
- return empty($this->_invalid);
- }
-}
\ No newline at end of file
More information about the Solar-svn
mailing list