Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • Boolean
  • Date
  • DEnum
  • DEnumItem
  • Double
  • Enum
  • Integer
  • Relation
  • RelationLookup
  • Sequence
  • String
  • Text
  • Timestamp
  • Type

Interfaces

  • TypeInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alpha\Model\Type;
  4: 
  5: use Alpha\Util\Helper\Validator;
  6: use Alpha\Exception\IllegalArguementException;
  7: 
  8: /**
  9:  * The Double complex data type.
 10:  *
 11:  * @since 1.0
 12:  *
 13:  * @author John Collins <dev@alphaframework.org>
 14:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 15:  * @copyright Copyright (c) 2015, 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: class Double extends Type implements TypeInterface
 51: {
 52:     /**
 53:      * The value of the Double.
 54:      *
 55:      * @var float
 56:      *
 57:      * @since 1.0
 58:      */
 59:     private $value;
 60: 
 61:     /**
 62:      * The validation rule (reg-ex) applied to Double values.
 63:      *
 64:      * @var string
 65:      *
 66:      * @since 1.0
 67:      */
 68:     private $validationRule;
 69: 
 70:     /**
 71:      * The error message for the Double type when validation fails.
 72:      *
 73:      * @var string
 74:      *
 75:      * @since 1.0
 76:      */
 77:     protected $helper = 'Not a valid double value!';
 78: 
 79:     /**
 80:      * The size of the value for the Double.
 81:      *
 82:      * @var int
 83:      *
 84:      * @since 1.0
 85:      */
 86:     private $size = 13;
 87: 
 88:     /**
 89:      * The absolute maximum size of the value for the this double.
 90:      *
 91:      * @var int
 92:      *
 93:      * @since 1.0
 94:      */
 95:     const MAX_SIZE = 13;
 96: 
 97:     /**
 98:      * Constructor.
 99:      *
100:      * @param float $val
101:      *
102:      * @since 1.0
103:      *
104:      * @throws Alpha\Exception\IllegalArguementException
105:      */
106:     public function __construct($val = 0.0)
107:     {
108:         $this->validationRule = Validator::REQUIRED_DOUBLE;
109: 
110:         if (!Validator::isDouble($val)) {
111:             throw new IllegalArguementException($this->helper);
112:         }
113: 
114:         if (mb_strlen($val) <= $this->size) {
115:             $this->value = $val;
116:         } else {
117:             throw new IllegalArguementException($this->helper);
118:         }
119:     }
120: 
121:     /**
122:      * Setter for the Double value.
123:      *
124:      * @param float $val
125:      *
126:      * @since 1.0
127:      *
128:      * @throws Alpha\Exception\IllegalArguementException
129:      */
130:     public function setValue($val)
131:     {
132:         if (!Validator::isDouble($val)) {
133:             throw new IllegalArguementException($this->helper);
134:         }
135: 
136:         if (mb_strlen($val) <= $this->size) {
137:             $this->value = $val;
138:         } else {
139:             throw new IllegalArguementException($this->helper);
140:         }
141:     }
142: 
143:     /**
144:      * Getter for the Double value.
145:      *
146:      * @return float
147:      *
148:      * @since 1.0
149:      */
150:     public function getValue()
151:     {
152:         return $this->value;
153:     }
154: 
155:     /**
156:      * Get the validation rule.
157:      *
158:      * @return string
159:      *
160:      * @since 1.0
161:      */
162:     public function getRule()
163:     {
164:         return $this->validationRule;
165:     }
166: 
167:     /**
168:      * Used to set the allowable size of the Double in the database field.
169:      *
170:      * @param int $size
171:      *
172:      * @since 1.0
173:      *
174:      * @throws Alpha\Exception\IllegalArguementException
175:      */
176:     public function setSize($size)
177:     {
178:         if ($size <= self::MAX_SIZE) {
179:             $this->size = $size;
180:         } else {
181:             throw new IllegalArguementException('The value '.$size.' provided by setSize is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
182:         }
183:     }
184: 
185:     /**
186:      * Get the allowable size of the Double in the database field.
187:      *
188:      * @param bool $databaseDimension
189:      *
190:      * @return mixed
191:      *
192:      * @since 1.0
193:      */
194:     public function getSize($databaseDimension = false)
195:     {
196:         if ($databaseDimension) {
197:             return $this->size.',2';
198:         } else {
199:             return $this->size;
200:         }
201:     }
202: 
203:     /**
204:      * Used to convert the object to a printable string.
205:      *
206:      * @return string
207:      *
208:      * @since 1.0
209:      */
210:     public function __toString()
211:     {
212:         return strval(sprintf('%01.2f', $this->value));
213:     }
214: }
215: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0