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 Integer 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 Integer extends Type implements TypeInterface
 51: {
 52:     /**
 53:      * The value of the Integer.
 54:      *
 55:      * @var int
 56:      *
 57:      * @since 1.0
 58:      */
 59:     private $value;
 60: 
 61:     /**
 62:      * The validation rule (reg-ex) applied to Integer values.
 63:      *
 64:      * @var string
 65:      *
 66:      * @since 1.0
 67:      */
 68:     private $validationRule;
 69: 
 70:     /**
 71:      * The error message for the Integer type when validation fails.
 72:      *
 73:      * @var string
 74:      *
 75:      * @since 1.0
 76:      */
 77:     protected $helper = 'Not a valid integer value!';
 78: 
 79:     /**
 80:      * The size of the value for the Integer.
 81:      *
 82:      * @var int
 83:      *
 84:      * @since 1.0
 85:      */
 86:     private $size = 11;
 87: 
 88:     /**
 89:      * The absolute maximum size of the value for the this Integer.
 90:      *
 91:      * @var int
 92:      *
 93:      * @since 1.0
 94:      */
 95:     const MAX_SIZE = 11;
 96: 
 97:     /**
 98:      * Constructor.
 99:      *
100:      * @param int $val
101:      *
102:      * @since 1.0
103:      *
104:      * @throws Alpha\Exception\IllegalArguementException
105:      */
106:     public function __construct($val = 0)
107:     {
108:         $this->validationRule = Validator::REQUIRED_INTEGER;
109: 
110:         if (!Validator::isInteger($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 Integer value.
123:      *
124:      * @param int $val
125:      *
126:      * @since 1.0
127:      *
128:      * @throws Alpha\Exception\IllegalArguementException
129:      */
130:     public function setValue($val)
131:     {
132:         if (!Validator::isInteger($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 Integer value.
145:      *
146:      * @return int
147:      *
148:      * @since 1.0
149:      */
150:     public function getValue()
151:     {
152:         return intval($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 Integer 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:             $this->helper = 'Not a valid integer value!  A maximum of '.$this->size.' characters is allowed';
181:         } else {
182:             throw new IllegalArguementException('Error: the value '.$size.' provided by set_size is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
183:         }
184:     }
185: 
186:     /**
187:      * Get the allowable size of the Integer in the database field.
188:      *
189:      * @return int
190:      *
191:      * @since 1.0
192:      */
193:     public function getSize()
194:     {
195:         return $this->size;
196:     }
197: 
198:     /**
199:      * Returns the integer value provided but padded with zeros to MAX_SIZE.
200:      *
201:      * @param int $val
202:      *
203:      * @since 1.0
204:      */
205:     public static function zeroPad($val)
206:     {
207:         return str_pad($val, self::MAX_SIZE, '0', STR_PAD_LEFT);
208:     }
209: }
210: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0