Rule
[ class tree: Rule ] [ index: Rule ] [ all elements ]

Source for file Length.php

Documentation is available at Length.php

  1. <?php
  2. /**
  3.  * Formagic
  4.  *
  5.  * LICENCE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at
  10.  * http://formagic.weasle.de/licence.txt
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to licence@weasle.de so we can send you a copy immediately.
  14.  *
  15.  * @category    Formagic
  16.  * @package     Rule
  17.  * @author      Marc Schrader
  18.  * @author      Florian Sonnenburg
  19.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  20.  * @license     http://formagic.weasle.de/licence.txt     New BSD License
  21.  * @revision    $Revision: 17 $
  22.  */
  23.  
  24. require_once('Rule.php');
  25.  
  26. /**
  27.  * Checks if value is between min and max length
  28.  *
  29.  * If only one numeric value is passed as argument, this value is interpreted
  30.  * as min length value.
  31.  *
  32.  * Supported arguments are:
  33.  *  - (integer)min: Minimum length of submitted value
  34.  *  - (integer)max: Maximum length of submitted value
  35.  *
  36.  * Usage examples:
  37.  *  - addRule('mandatory')
  38.  *  // any length will do
  39.  *
  40.  *  - addRule('mandatory', 'errorMessage', 10);
  41.  *  // at least 10 characters
  42.  *
  43.  *  - addRule('mandatory', 'errorMessage', array('min' => 1));
  44.  *  // same as addRule('mandatory')
  45.  *
  46.  *  - addRule('mandatory', 'errorMessage', array('max' => 10));
  47.  *  // 1 to 10 characters
  48.  *
  49.  *  - addRule('mandatory', 'errorMessage', array('min' => 5, 'max' => 10));
  50.  *  // 5 to 10 characters
  51.  *
  52.  * @category    Formagic
  53.  * @package     Rule
  54.  * @author      Marc Schrader
  55.  * @author      Florian Sonnenburg
  56.  * @copyright   Copyright (c) 2007 Marc Schrader
  57.  * @version     $Id: Length.php 17 2007-08-19 20:40:10Z meweasle $
  58.  ***/
  59. {
  60.  
  61.     /**
  62.      * Default error messages
  63.      * @var array 
  64.      ***/
  65.     private $_errorMessages array(
  66.                                 'min' => 'Bitte mindestens %MIN% Zeichen eingeben',
  67.                                 'max' => 'Bitte maximal %MAX% Zeichen eingeben',
  68.                                 'both' => 'Eingabe muss zwischen %MIN% und %MAX% Zeichen haben',
  69.                                 'mandatory' => 'Bitte Wert eingeben'
  70.                                );
  71.  
  72.     /**
  73.      * Default error code
  74.      * @var integer 
  75.      ***/
  76.     protected $_errorCode = 1;
  77.  
  78.     /**
  79.      * Tells what kind of violation occured
  80.      * @var integer 
  81.      ***/
  82.     private $_violation;
  83.  
  84.     /**
  85.      * Extends parent constructor
  86.      *
  87.      * @param string $type 
  88.      * @param array $args 
  89.      * @return void 
  90.      ***/
  91.     public function __construct($type$errorMessage=null$args=null)
  92.     {
  93.         parent::__construct($type$errorMessage$args);
  94.  
  95.         if (is_array($errorMessage)) {
  96.             if (   !isset($errorMessage['min'])
  97.                 || !isset($errorMessage['max'])
  98.                 || !isset($errorMessage['both'])
  99.                 || !isset($errorMessage['mandatory'])
  100.  
  101.             {
  102.                 throw new Formagic_Exception('Error message has to be array with ' .
  103.                     'keys "min", "max", "both" and "mandatory"');
  104.             }
  105.             $this->_errorMessages $errorMessage;
  106.         elseif($errorMessage{
  107.             $this->_errorMessages['min'$errorMessage;
  108.             $this->_errorMessages['max'$errorMessage;
  109.             $this->_errorMessages['both'$errorMessage;
  110.             $this->_errorMessages['mandatory'$errorMessage;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Performs rule check
  116.      *
  117.      * Length rule checks if value is between min and max length.
  118.      * (integer)_args['min']: Min length
  119.      * (integer)_args['max']: Max length
  120.      *
  121.      * @param Formagic_Item $itemObj 
  122.      * @return boolean 
  123.      ***/
  124.     public function check($item)
  125.     {
  126.         $this->_violatedType null;
  127.         $value $item->getValue();
  128.  
  129.         // check if value has to be entered for the rule to be valid
  130.         if (   !$value
  131.             && !$item->hasRule('mandatory')
  132.             && !isset($this->_args['mandatory'])
  133.         {
  134.             return true;
  135.         }
  136.  
  137.         // no arguments --> any length will do
  138.         if (is_null($this->_args)) {
  139.             $this->_args['min'1;
  140.         }
  141.  
  142.         if (!is_array($this->_args)) {
  143.             if (is_numeric($this->_args)) {
  144.                 $this->_args = array('min' => $this->_args);
  145.             else {
  146.                 throw new Formagic_Exception('Argument has to be array or numeric');
  147.             }
  148.         }
  149.  
  150.         $length strlen($value);
  151.  
  152.         // Check min and max length
  153.         if (   isset($this->_args['min'])
  154.             && isset($this->_args['max'])
  155.         {
  156.             if (   $length >= (int)$this->_args['min']
  157.                 && $length <= (int)$this->_args['max']
  158.             {
  159.                 return true;
  160.             else {
  161.                 $this->_errorMessage = str_replace(
  162.                             array('%MIN%''%MAX%'),
  163.                             array((int)$this->_args['min'](int)$this->_args['max']),
  164.                             $this->_errorMessages['both']);
  165.                 return false;
  166.             }
  167.         }
  168.  
  169.         // only max length given
  170.         if (isset($this->_args['max'])) {
  171.             if ($length <= (int)$this->_args['max']{
  172.                 return true;
  173.             else {
  174.                 $this->_errorMessage = str_replace(
  175.                             '%MAX%',
  176.                             (int)$this->_args['max'],
  177.                             $this->_errorMessages['max']);
  178.                 return false;
  179.             }
  180.         }
  181.  
  182.         // only min length given
  183.         if (isset($this->_args['min'])) {
  184.             if ($length >= (int)$this->_args['min']{
  185.                 return true;
  186.             else {
  187.                 $this->_errorMessage = str_replace(
  188.                             '%MIN%',
  189.                             (int)$this->_args['min'],
  190.                             $this->_errorMessages['min']);
  191.                 return false;
  192.             }
  193.         }
  194.  
  195.         // no min, no max --> mandatory rule applies
  196.         $this->_errorMessage = $this->_errorMessages['mandatory'];
  197.         return false;
  198.     }
  199.  
  200. }

Documentation generated on Thu, 23 Aug 2007 00:29:43 +0200 by phpDocumentor 1.4.0