home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm2 / Rule.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  8.7 KB  |  264 lines

  1. <?php
  2. /**
  3.  * Base class for HTML_QuickForm2 rules
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE:
  8.  *
  9.  * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
  10.  *                           Bertrand Mansion <golgote@mamasam.com>
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  *
  17.  *    * Redistributions of source code must retain the above copyright
  18.  *      notice, this list of conditions and the following disclaimer.
  19.  *    * Redistributions in binary form must reproduce the above copyright
  20.  *      notice, this list of conditions and the following disclaimer in the
  21.  *      documentation and/or other materials provided with the distribution.
  22.  *    * The names of the authors may not be used to endorse or promote products
  23.  *      derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * @category   HTML
  38.  * @package    HTML_QuickForm2
  39.  * @author     Alexey Borzov <avb@php.net>
  40.  * @author     Bertrand Mansion <golgote@mamasam.com>
  41.  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  42.  * @version    CVS: $Id: Rule.php,v 1.3 2007/07/05 18:27:16 avb Exp $
  43.  * @link       http://pear.php.net/package/HTML_QuickForm2
  44.  */
  45.  
  46. /**
  47.  * Abstract base class for HTML_QuickForm2 rules
  48.  *
  49.  * This class provides methods that allow chaining several rules together.
  50.  * Its validate() method executes the whole rule chain starting from this rule.
  51.  *
  52.  * @category   HTML
  53.  * @package    HTML_QuickForm2
  54.  * @author     Alexey Borzov <avb@php.net>
  55.  * @author     Bertrand Mansion <golgote@mamasam.com>
  56.  * @version    Release: 0.2.0
  57.  */
  58. abstract class HTML_QuickForm2_Rule
  59. {
  60.    /**
  61.     * An element whose value will be validated by this rule
  62.     * @var  HTML_QuickForm2_Node
  63.     */
  64.     protected $owner;
  65.  
  66.    /**
  67.     * An error message to display if validation fails
  68.     * @var  string
  69.     */
  70.     protected $message;
  71.  
  72.    /**
  73.     * Additional data for the rule
  74.     * @var  mixed
  75.     */
  76.     protected $options;
  77.  
  78.    /**
  79.     * Rules chained to this via "and" and "or" operators
  80.     *
  81.     * The contents can be described as "disjunctive normal form", where an outer
  82.     * array represents a disjunction of conjunctive clauses represented by inner
  83.     * arrays.
  84.     *
  85.     * @var  array
  86.     */
  87.     protected $chainedRules = array(array());
  88.  
  89.    /**
  90.     * Type that was provided to Factory when creating this Rule instance
  91.     *
  92.     * Used to get the common configuration data for the Rules of that type from
  93.     * Factory.
  94.     *
  95.     * @var  string
  96.     */
  97.     protected $registeredType = null;
  98.  
  99.  
  100.    /**
  101.     * Class constructor
  102.     *
  103.     * @param    HTML_QuickForm2_Node    Element to validate
  104.     * @param    string                  Error message to display if validation fails
  105.     * @param    mixed                   Additional data for the rule
  106.     * @param    string                  Type that was provided to Factory when
  107.     *                                   creating this Rule instance, shouldn't
  108.     *                                   be set if instantiating the Rule object
  109.     *                                   manually.
  110.     */
  111.     public function __construct(HTML_QuickForm2_Node $owner, $message = '',
  112.                                 $options = null, $registeredType = null)
  113.     {
  114.         $this->setOwner($owner);
  115.         $this->setMessage($message);
  116.         $this->setOptions($options);
  117.         $this->registeredType = $registeredType;
  118.     }
  119.  
  120.    /**
  121.     * Sets additional configuration data for the rule
  122.     *
  123.     * @param    mixed                   Rule configuration data (rule-dependent)
  124.     * @return   HTML_QuickForm2_Rule
  125.     */
  126.     public function setOptions($options)
  127.     {
  128.         $this->options = $options;
  129.         return $this;
  130.     }
  131.  
  132.    /**
  133.     * Returns the rule's configuration data
  134.     *
  135.     * @return   mixed
  136.     */
  137.     public function getOptions()
  138.     {
  139.         return $this->options;
  140.     }
  141.  
  142.    /**
  143.     * Sets the error message output by the rule
  144.     *
  145.     * @param    string                  Error message to display if validation fails
  146.     * @return   HTML_QuickForm2_Rule
  147.     */
  148.     public function setMessage($message)
  149.     {
  150.         $this->message = (string)$message;
  151.         return $this;
  152.     }
  153.  
  154.    /**
  155.     * Returns the error message output by the rule
  156.     *
  157.     * @return   string  Error message
  158.     */
  159.     public function getMessage()
  160.     {
  161.         return $this->message;
  162.     }
  163.  
  164.    /**
  165.     * Sets the element that will be validated by this rule
  166.     *
  167.     * @param    HTML_QuickForm2_Node    Element to validate
  168.     * @todo     We should consider removing the rule from previous owner
  169.     */
  170.     public function setOwner(HTML_QuickForm2_Node $owner)
  171.     {
  172.         $this->owner = $owner;
  173.     }
  174.  
  175.    /**
  176.     * Adds a rule to the chain with an "and" operator
  177.     *
  178.     * Evaluation is short-circuited, next rule will not be evaluated if the
  179.     * previous one returns false. The method is named this way because "and" is
  180.     * a reserved word in PHP.
  181.     *
  182.     * @param    HTML_QuickForm2_Rule
  183.     * @return   HTML_QuickForm2_Rule    first rule in the chain (i.e. $this)
  184.     * @throws   HTML_QuickForm2_InvalidArgumentException    when trying to add
  185.     *           a "required" rule to the chain
  186.     */
  187.     public function and_(HTML_QuickForm2_Rule $next)
  188.     {
  189.         if ($next instanceof HTML_QuickForm2_Rule_Required) {
  190.             throw new HTML_QuickForm2_InvalidArgumentException(
  191.                 'and_(): Cannot add a "required" rule'
  192.             );
  193.         }
  194.         $this->chainedRules[count($this->chainedRules) - 1][] = $next;
  195.         return $this;
  196.     }
  197.  
  198.    /**
  199.     * Adds a rule to the chain with an "or" operator
  200.     *
  201.     * Evaluation is short-circuited, next rule will not be evaluated if the
  202.     * previous one returns true. The method is named this way because "or" is
  203.     * a reserved word in PHP.
  204.     *
  205.     * @param    HTML_QuickForm2_Rule
  206.     * @return   HTML_QuickForm2_Rule    first rule in the chain (i.e. $this)
  207.     * @throws   HTML_QuickForm2_InvalidArgumentException    when trying to add
  208.     *           a "required" rule to the chain
  209.     */
  210.     public function or_(HTML_QuickForm2_Rule $next)
  211.     {
  212.         if ($next instanceof HTML_QuickForm2_Rule_Required) {
  213.             throw new HTML_QuickForm2_InvalidArgumentException(
  214.                 'or_(): Cannot add a "required" rule'
  215.             );
  216.         }
  217.         $this->chainedRules[] = array($next);
  218.         return $this;
  219.     }
  220.  
  221.    /**
  222.     * Performs validation
  223.     *
  224.     * The whole rule chain is executed. Note that the side effect of this
  225.     * method is setting the error message on element if validation fails
  226.     *
  227.     * @return   boolean     Whether the element is valid
  228.     */
  229.     public function validate()
  230.     {
  231.         $globalValid = false;
  232.         $localValid  = $this->checkValue($this->owner->getValue());
  233.         foreach ($this->chainedRules as $item) {
  234.             foreach ($item as $multiplier) {
  235.                 if (!$localValid) {
  236.                     break;
  237.                 }
  238.                 $localValid = $localValid && $multiplier->validate();
  239.             }
  240.             $globalValid = $globalValid || $localValid;
  241.             if ($globalValid) {
  242.                 break;
  243.             }
  244.             $localValid = true;
  245.         }
  246.         if (!$globalValid && strlen($this->message) && !$this->owner->getError()) {
  247.             $this->owner->setError($this->message);
  248.         }
  249.         return $globalValid;
  250.     }
  251.  
  252.    /**
  253.     * Validates the element's value
  254.     *
  255.     * Note that the error message will be set for an element if such message
  256.     * exists in the rule and that method returns false
  257.     *
  258.     * @param    mixed   Form element's value
  259.     * @return   boolean Whether the value is valid according to the rule
  260.     */
  261.     abstract protected function checkValue($value);
  262. }
  263. ?>
  264.