      1: <?php
      2:
      3: /**
      4:  * The Double complex data type
      5:  *
      6:  * @package alpha::model::types
      7:  * @since 1.0
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: Double.inc 1496 2012-02-12 20:32:21Z alphadev $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class Double extends AlphaType implements AlphaTypeInterface {
     48:     /**
     49:      * The value of the Double
     50:      *
     51:      * @var double
     52:      * @since 1.0
     53:      */
     54:     private $value;
     55:
     56:     /**
     57:      * The validation rule (reg-ex) applied to Double values
     58:      *
     59:      * @var string
     60:      * @since 1.0
     61:      */
     62:     private $validationRule;
     63:
     64:     /**
     65:      * The error message for the Double type when validation fails
     66:      *
     67:      * @var string
     68:      * @since 1.0
     69:      */
     70:     protected $helper = 'Not a valid double value!';
     71:
     72:     /**
     73:      * The size of the value for the Double
     74:      *
     75:      * @var integer
     76:      * @since 1.0
     77:      */
     78:     private $size = 13;
     79:
     80:     /**
     81:      * The absolute maximum size of the value for the this double
     82:      *
     83:      * @var integer
     84:      * @since 1.0
     85:      */
     86:     const MAX_SIZE = 13;
     87:
     88:     /**
     89:      * Constructor
     90:      *
     91:      * @param double $val
     92:      * @since 1.0
     93:      * @throws IllegalArguementException
     94:      */
     95:     public function __construct($val=0.0) {
     96:         $this->validationRule = AlphaValidator::REQUIRED_DOUBLE;
     97:
     98:         if(!AlphaValidator::isDouble($val))
     99:             throw new IllegalArguementException($this->helper);
    100:
    101:         if (strlen($val) <= $this->size) {
    102:             $this->value = $val;
    103:         }else{
    104:             throw new IllegalArguementException($this->helper);
    105:         }
    106:     }
    107:
    108:     /**
    109:      * Setter for the Double value
    110:      *
    111:      * @param double $val
    112:      * @since 1.0
    113:      * @throws IllegalArguementException
    114:      */
    115:     public function setValue($val) {
    116:         if(!AlphaValidator::isDouble($val))
    117:             throw new IllegalArguementException($this->helper);
    118:
    119:         if (strlen($val) <= $this->size) {
    120:             $this->value = $val;
    121:         }else{
    122:             throw new IllegalArguementException($this->helper);
    123:         }
    124:     }
    125:
    126:     /**
    127:      * Getter for the Double value
    128:      *
    129:      * @return double
    130:      * @since 1.0
    131:      */
    132:     public function getValue() {
    133:         return $this->value;
    134:     }
    135:
    136:     /**
    137:      * Get the validation rule
    138:      *
    139:      * @return string
    140:      * @since 1.0
    141:      */
    142:     public function getRule() {
    143:         return $this->validationRule;
    144:     }
    145:
    146:     /**
    147:      * Used to set the allowable size of the Double in the database field
    148:      *
    149:      * @param integer $size
    150:      * @since 1.0
    151:      * @throws IllegalArguementException
    152:      */
    153:     public function setSize($size) {
    154:         if ($size <= self::MAX_SIZE) {
    155:             $this->size = $size;
    156:         }else{
    157:             throw new IllegalArguementException('The value '.$size.' provided by setSize is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
    158:         }
    159:     }
    160:
    161:     /**
    162:      * Get the allowable size of the Double in the database field
    163:      *
    164:      * @param boolean $databaseDimension
    165:      * @return mixed
    166:      * @since 1.0
    167:      */
    168:     public function getSize($databaseDimension=false){
    169:         if($databaseDimension)
    170:             return $this->size.',2';
    171:         else
    172:             return $this->size;
    173:     }
    174:
    175:     /**
    176:      * Used to convert the object to a printable string
    177:      *
    178:      * @return string
    179:      * @since 1.0
    180:      */
    181:     public function __toString() {
    182:         return strval(sprintf("%01.2f", $this->value));
    183:     }
    184: }
    185:
    186: ?>