phpDocumentor SForm
Rules
[ class tree: SForm ] [ index: SForm ] [ all elements ]

Source for file Boolean.php

Documentation is available at Boolean.php

  1. <?php
  2. /**
  3.  * <b>License</b>:
  4.  * 
  5.  * Copyright (c) 2006, Seth Price <{@link mailto:seth@pricepages.org seth@pricepages.org}> All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  13.  * - The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
  14.  *
  15.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  *
  27.  * @copyright    Copyright (c) 2006, Seth Price
  28.  * @author        Seth Price <seth@pricepages.org>
  29.  * @license        http://opensource.org/licenses/bsd-license.php New BSD License
  30.  * @access        public
  31.  * @package        SForm
  32.  * @see            SForm::__construct()
  33.  * @version        0.1
  34.  * @subpackage    Rules
  35.  */
  36.  
  37.  
  38. /**
  39.  * Used to generate on-the-fly boolean rules
  40.  * 
  41.  * This is a completely free form boolean statement, with an arbitrary number of
  42.  * elements used. It automatically uses the boolean statement to generate code
  43.  * for both client side and server side validation.
  44.  * 
  45.  * This means that you can easily create complex rules, such as validating a
  46.  * credit card number depnding on which type of credit card is selected. You can
  47.  * require a user to fill in identifying information if they don't have a user
  48.  * name. You can better control filling out billing vs. shipping information.
  49.  * You can also do any of this in JavaScript.
  50.  * 
  51.  * <b>Example</b>:
  52.  * 
  53.  * If the text input is blank <i>and</i> the value -2 is selected in the pull
  54.  * down menu, throw an error:
  55.  * 
  56.  * <code>
  57.  * $newNameElm->addRule( 'boolean', '%s == "" && %s == -2',
  58.  *                 array($newNameElm, $areaElm),
  59.  *                 'If you are creating a new area, you must fill in it\'s name.' );
  60.  * </code>
  61.  * 
  62.  * @package        SForm
  63.  * @subpackage    Rules
  64.  */
  65. class SForm_Rule_Boolean extends SForm_Rule {
  66.     
  67.     /**
  68.      * Define this rule
  69.      * 
  70.      * @var        string 
  71.      */
  72.     protected $freeFormBool;
  73.     
  74.     /**
  75.      * Create a free form boolean rule
  76.      * 
  77.      * The first arg is the messsage that will be returned when an error ocours.
  78.      * 
  79.      * The second arg are the elements who's values will be fed into the boolean
  80.      * statement.
  81.      * 
  82.      * Last argument is a boolean string, with vars like {@link sprintf()} would
  83.      * accept. The placeholders in the string should be '%s' so we can insert
  84.      * the required text into it. The boolean string should evaluate to true iff
  85.      * there is an error.
  86.      * 
  87.      * @param    string    Error message
  88.      * @param    array    The elements used by the statement
  89.      * @param    string    Boolean statement
  90.      */
  91.     function __construct($message$elements$freeFormBool){
  92.         /*
  93.          * Basic test intended to make sure that we aren't actually executing
  94.          * code in our "boolean statement". 
  95.          */
  96.         if(strpos($freeFormBool';'!== false){
  97.             throw new Exception('It looks like you are trying to execute code in ' .
  98.                     'your rule. This rule is intended to be limited to boolean ' .
  99.                     'statements.');
  100.         elseif(!is_string($freeFormBool)) {
  101.             throw new Exception('Your boolean statement must be in the form of a ' .
  102.                     'string.');
  103.         elseif(!is_array($elements)){
  104.             $elements array($elements);
  105.         }
  106.         
  107.         foreach($elements as $elm){
  108.             if(!$elm instanceof SForm_Element){
  109.                 throw new Exception('The elements should be instances of ' .
  110.                         'SForm_Element.');
  111.             }
  112.         }
  113.         
  114.         $this->message = $message;
  115.         $this->elements = $elements;
  116.         $this->freeFormBool = $freeFormBool;
  117.     }
  118.     
  119.     /**
  120.      * Does the statement evaluate to true?
  121.      * 
  122.      * Values are found for each of the input elements, php code is created
  123.      * using those values, and the whole thing is fed through {@link eval()} to
  124.      * produce a boolean value.
  125.      * 
  126.      * @return    boolean    Evaluation of the boolean statement
  127.      */
  128.     public function isError(){
  129.         $vars array($this->freeFormBool);
  130.         foreach($this->elements as $elm){
  131.             $vars['"'.$elm->getValue().'"';
  132.         }
  133.         
  134.         /*
  135.          * If anyone knows how to call a function with a variable number of
  136.          * args, let me know.
  137.          */
  138.         $ret true;
  139.         $valid = eval('$ret = '.call_user_func_array('sprintf'$vars).';');
  140.         
  141.         if($valid !== null){
  142.             throw new Exception('eval() of the boolean statement returned a ' .
  143.                     'non-null value. Please check it for validity.');
  144.         }
  145.         
  146.         return $ret;
  147.     }
  148.     
  149.     /**
  150.      * Generates JS that will execute the boolean statement
  151.      * 
  152.      * The code to locate the value of each element is created with the
  153.      * necessary local variables and the local variables are parsed into the
  154.      * boolean string. The strings are then inserted in the appropriate points
  155.      * in JS to throw an error if the boolean statement evaluated as true.
  156.      * 
  157.      * @return    string    JavaScript
  158.      */
  159.     public function toJavaScript(){
  160.         if(!$this->validateOnClient){
  161.             return '';
  162.         }
  163.         
  164.         $pre '';
  165.         
  166.         $varArgs array($this->freeFormBool);
  167.         $i 0;
  168.         foreach($this->elements as $elm){
  169.             $id $elm->getGlobalId();
  170.             $pre .= self::genJSValue($elm);
  171.             $varArgs[self::$jsElmVar[$id];
  172.         }
  173.         
  174.         /*
  175.          * If anyone knows how to call a function with a variable number of
  176.          * args, let me know.
  177.          */
  178.         $bool call_user_func_array('sprintf'$varArgs);
  179.         
  180.         return $this->createJSRule($pre$bool);
  181.     }
  182. }
  183. ?>

Documentation generated on Mon, 09 Apr 2007 19:06:36 -0500 by phpDocumentor 1.3.0