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

Source for file Double.inc

Documentation is available at Double.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 Double complex data type
  9.  * 
  10.  * @package alpha::model::types
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: Double.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 Double extends AlphaType implements AlphaTypeInterface {
  52.     /**
  53.      * The value of the Double
  54.      * 
  55.      * @var double 
  56.      * @since 1.0
  57.      */
  58.      private $value;
  59.     
  60.     /**
  61.      * The validation rule (reg-ex) applied to Double values
  62.      *
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.      private $validationRule;
  67.     
  68.     /**
  69.      * The error message for the Double type when validation fails
  70.      * 
  71.      * @var string 
  72.      * @since 1.0
  73.      */
  74.     protected $helper = 'Not a valid double value!';
  75.     
  76.     /**
  77.      * The size of the value for the Double
  78.      * 
  79.      * @var integer 
  80.      * @since 1.0
  81.      */
  82.     private $size 13;
  83.     
  84.     /**
  85.      * The absolute maximum size of the value for the this double
  86.      * 
  87.      * @var integer 
  88.      * @since 1.0
  89.      */
  90.     const MAX_SIZE = 13;
  91.     
  92.     /**
  93.      * Constructor
  94.      *
  95.      * @param double $val 
  96.      * @since 1.0
  97.      * @throws IllegalArguementException
  98.      */
  99.     public function __construct($val=0.0{    
  100.         $this->validationRule AlphaValidator::REQUIRED_DOUBLE;
  101.         
  102.         if(!AlphaValidator::isDouble($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 Double value
  114.      *
  115.      * @param double $val 
  116.      * @since 1.0
  117.      * @throws IllegalArguementException
  118.      */
  119.     public function setValue($val{                
  120.         if(!AlphaValidator::isDouble($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 Double value
  132.      *
  133.      * @return double 
  134.      * @since 1.0
  135.      */
  136.     public function getValue({
  137.         return $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 Double 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.         }else{
  161.             throw new IllegalArguementException('The value '.$size.' provided by setSize is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
  162.         }    
  163.     }
  164.     
  165.     /**
  166.      * Get the allowable size of the Double in the database field
  167.      *
  168.      * @param boolean $databaseDimension 
  169.      * @return mixed 
  170.      * @since 1.0
  171.      */
  172.     public function getSize($databaseDimension=false){
  173.         if($databaseDimension)
  174.             return $this->size.',2';
  175.         else
  176.             return $this->size;
  177.     }
  178.  
  179.     /**
  180.      * Used to convert the object to a printable string
  181.      *
  182.      * @return string 
  183.      * @since 1.0
  184.      */
  185.     public function __toString({        
  186.         return strval(sprintf("%01.2f"$this->value));
  187.     }
  188. }
  189.  
  190. ?>

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