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

Source for file Integer.inc

Documentation is available at Integer.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 Integer complex data type
  9.  * 
  10.  * @package alpha::model::types
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: Integer.inc 1453 2011-12-04 15:12:54Z 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 Integer extends AlphaType implements AlphaTypeInterface {
  52.     /**
  53.      * The value of the Integer
  54.      *
  55.      * @var integer 
  56.      * @since 1.0
  57.      */
  58.      private $value;
  59.      
  60.     /**
  61.      * The validation rule (reg-ex) applied to Integer values
  62.      *
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.      private $validationRule;
  67.      
  68.     /**
  69.      * The error message for the Integer type when validation fails
  70.      * 
  71.      * @var string 
  72.      * @since 1.0
  73.      */
  74.     protected $helper = 'Not a valid integer value!';
  75.     
  76.     /**
  77.      * The size of the value for the Integer
  78.      * 
  79.      * @var integer 
  80.      * @since 1.0
  81.      */
  82.     private $size 11;
  83.     
  84.     /**
  85.      * The absolute maximum size of the value for the this Integer
  86.      * 
  87.      * @var integer 
  88.      * @since 1.0
  89.      */
  90.     const MAX_SIZE = 11;
  91.     
  92.     /**
  93.      * Constructor
  94.      *
  95.      * @param integer $val 
  96.      * @since 1.0
  97.      * @throws IllegalArguementException
  98.      */
  99.     public function __construct($val=0{
  100.         $this->validationRule AlphaValidator::REQUIRED_INTEGER;
  101.         
  102.         if(!AlphaValidator::isInteger($val))
  103.             throw new IllegalArguementException($this->helper);
  104.         
  105.         if (strlen($val<= $this->size{            
  106.             $this->value $val;
  107.         }else{
  108.             throw new IllegalArguementException($this->helper);
  109.         }
  110.     }
  111.     
  112.     /**
  113.      * Setter for the Integer value
  114.      *
  115.      * @param integer $val 
  116.      * @since 1.0
  117.      * @throws IllegalArguementException
  118.      */
  119.     public function setValue($val{        
  120.         if(!AlphaValidator::isInteger($val))
  121.             throw new IllegalArguementException($this->helper);
  122.         
  123.         if (strlen($val<= $this->size{            
  124.             $this->value $val;
  125.         }else{
  126.             throw new IllegalArguementException($this->helper);
  127.         }
  128.     }
  129.     
  130.     /**
  131.      * Getter for the Integer value
  132.      *
  133.      * @return integer 
  134.      * @since 1.0
  135.      */
  136.     public function getValue({
  137.         return intval($this->value);
  138.     }    
  139.     
  140.     /**
  141.       * Get the validation rule
  142.       *
  143.       * @return string 
  144.       * @since 1.0
  145.       */
  146.      public function getRule({
  147.         return $this->validationRule;
  148.     }    
  149.     
  150.     /**
  151.      * Used to set the allowable size of the Integer in the database field
  152.      *
  153.      * @param integer $size 
  154.      * @since 1.0
  155.      * @throws IllegalArguementException
  156.      */
  157.     public function setSize($size{
  158.         if ($size <= self::MAX_SIZE{
  159.             $this->size $size;
  160.             $this->helper = 'Not a valid integer value!  A maximum of '.$this->size.' characters is allowed';
  161.         }else{
  162.             throw new IllegalArguementException('Error: the value '.$size.' provided by set_size is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
  163.         }    
  164.     }
  165.     
  166.     /**
  167.      * Get the allowable size of the Integer in the database field
  168.      *    
  169.      * @return integer 
  170.      * @since 1.0
  171.      */
  172.     public function getSize(){
  173.         return $this->size;
  174.     }
  175.     
  176.     /**
  177.      * Returns the integer value provided but padded with zeros to MAX_SIZE
  178.      * 
  179.      * @param integer $val 
  180.      * @since 1.0
  181.      */
  182.     public static function zeroPad($val{
  183.         return str_pad($valInteger::MAX_SIZE'0'STR_PAD_LEFT);
  184.     }
  185. }
  186.  
  187. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:05 +0000 by phpDocumentor 1.4.3