Alpha Framework alpha--model--types
[ class tree: alpha--model--types ] [ index: alpha--model--types ] [ all elements ]

Source for file String.inc

Documentation is available at String.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/AlphaException.inc';
  4. require_once $config->get('sysRoot').'alpha/model/types/AlphaType.inc';
  5. require_once $config->get('sysRoot').'alpha/model/types/AlphaTypeInterface.inc';
  6.  
  7. /**
  8.  * The String complex data type
  9.  * 
  10.  * @package alpha::model::types
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: String.inc 1341 2011-03-17 15:02:02Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class String extends AlphaType implements AlphaTypeInterface {
  52.     /**
  53.      * The value of the string
  54.      * 
  55.      * @var string 
  56.      * @since 1.0
  57.      */
  58.     private $value;
  59.     
  60.     /**
  61.      * The validation rule for the string type
  62.      * 
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.     private $validationRule;
  67.     
  68.     /**
  69.      * The error message for the string type when validation fails
  70.      * 
  71.      * @var string 
  72.      * @since 1.0
  73.      */
  74.     protected $helper = 'Not a valid string value!';
  75.     
  76.     /**
  77.      * The size of the value for the this String
  78.      * 
  79.      * @var integer 
  80.      * @since 1.0
  81.      */
  82.     private $size 255;
  83.     
  84.     /**
  85.      * The absolute maximum size of the value for the this String
  86.      * 
  87.      * @var integer 
  88.      * @since 1.0
  89.      */
  90.     const MAX_SIZE = 255;
  91.     
  92.     /**
  93.      * Simple boolean to determine if the string is a password or not
  94.      * 
  95.      * @var boolean 
  96.      * @since 1.0
  97.      */
  98.     private $password false;
  99.     
  100.     /**
  101.      * Constructor
  102.      *
  103.      * @param string $val 
  104.      * @since 1.0
  105.      * @throws IllegalArguementException
  106.      */
  107.     public function __construct($val=''{        
  108.         
  109.         $this->validationRule AlphaValidator::ALLOW_ALL;
  110.         
  111.         if (strlen($val<= $this->size{            
  112.             if (preg_match($this->validationRule$val)) {                
  113.                 $this->value $val;
  114.             }else{
  115.                 throw new IllegalArguementException($this->helper);
  116.             }
  117.         }else{
  118.             throw new IllegalArguementException($this->helper);
  119.         }
  120.     }
  121.     
  122.     /**
  123.      * Setter for the value
  124.      *
  125.      * @param string $val 
  126.      * @since 1.0
  127.      * @throws IllegalArguementException
  128.      */
  129.     public function setValue($val{    
  130.         
  131.         if (strlen($val<= $this->size{            
  132.             if (preg_match($this->validationRule$val)) {                
  133.                 $this->value $val;
  134.             }else{
  135.                 throw new IllegalArguementException($this->helper);
  136.             }
  137.         }else{
  138.             throw new IllegalArguementException($this->helper);
  139.         }
  140.     }
  141.     
  142.     /**
  143.      * Getter for the value
  144.      *
  145.      * @return string 
  146.      * @since 1.0
  147.      */
  148.     public function getValue({
  149.         return $this->value;
  150.     }
  151.  
  152.     /**
  153.      * Setter to override the default validation rule
  154.      *
  155.      * @param string $rule 
  156.      * @since 1.0
  157.      */
  158.     public function setRule($rule{
  159.         $this->validationRule $rule;
  160.     }
  161.     
  162.     /**
  163.       * Get the validation rule
  164.       *
  165.       * @return string 
  166.       * @since 1.0
  167.       */
  168.      public function getRule({
  169.         return $this->validationRule;
  170.     }    
  171.     
  172.     /**
  173.      * Used to set the allowable size of the String in the database field
  174.      *
  175.      * @param integer $size 
  176.      * @since 1.0
  177.      * @throws IllegalArguementException
  178.      */
  179.     public function setSize($size{
  180.         if ($size <= self::MAX_SIZE{
  181.             $this->size $size;
  182.         }else{
  183.             throw new IllegalArguementException('Error: the value '.$size.' provided by setSize is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
  184.         }    
  185.     }
  186.     
  187.     /**
  188.      * Get the allowable size of the Double in the database field
  189.      *    
  190.      * @return integer 
  191.      * @since 1.0
  192.      */
  193.     public function getSize(){
  194.         return $this->size;
  195.     }
  196.     
  197.     /**
  198.      * Sets up an appropriate validation rule for a required field
  199.      * 
  200.      * @param bool $req 
  201.      * @since 1.0
  202.      */
  203.     public function isRequired($req=true{
  204.         if ($req{
  205.             $this->validationRule REQUIRED_STRING;
  206.             $this->helper = 'This string requires a value!';
  207.         }else{
  208.             $this->validationRule DEFAULT_STRING;
  209.         }
  210.     }
  211.     
  212.     /**
  213.      * Define the string as a password (making it required by validation rule)
  214.      * 
  215.      * @param boolean $pass 
  216.      * @since 1.0
  217.      */
  218.     public function isPassword($pass=true{
  219.         $this->password $pass;
  220.         
  221.         if($pass{
  222.             $this->validationRule '/\w+/';
  223.             $this->helper = 'Password is required!';
  224.         }
  225.     }
  226.     
  227.     /**
  228.      * Checks to see if the string is a password or not
  229.      * 
  230.      * @return boolean 
  231.      * @since 1.0
  232.      */
  233.     public function checkIsPassword({
  234.         return $this->password;
  235.     }
  236. }
  237.  
  238. ?>

Documentation generated on Thu, 17 Mar 2011 16:44:52 +0000 by phpDocumentor 1.4.3