1: <?php
2:
3: /**
4: * The Integer complex data type
5: *
6: * @package alpha::model::types
7: * @since 1.0
8: * @author John Collins <dev@alphaframework.org>
9: * @version $Id: Integer.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 Integer extends AlphaType implements AlphaTypeInterface {
48: /**
49: * The value of the Integer
50: *
51: * @var integer
52: * @since 1.0
53: */
54: private $value;
55:
56: /**
57: * The validation rule (reg-ex) applied to Integer values
58: *
59: * @var string
60: * @since 1.0
61: */
62: private $validationRule;
63:
64: /**
65: * The error message for the Integer type when validation fails
66: *
67: * @var string
68: * @since 1.0
69: */
70: protected $helper = 'Not a valid integer value!';
71:
72: /**
73: * The size of the value for the Integer
74: *
75: * @var integer
76: * @since 1.0
77: */
78: private $size = 11;
79:
80: /**
81: * The absolute maximum size of the value for the this Integer
82: *
83: * @var integer
84: * @since 1.0
85: */
86: const MAX_SIZE = 11;
87:
88: /**
89: * Constructor
90: *
91: * @param integer $val
92: * @since 1.0
93: * @throws IllegalArguementException
94: */
95: public function __construct($val=0) {
96: $this->validationRule = AlphaValidator::REQUIRED_INTEGER;
97:
98: if(!AlphaValidator::isInteger($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 Integer value
110: *
111: * @param integer $val
112: * @since 1.0
113: * @throws IllegalArguementException
114: */
115: public function setValue($val) {
116: if(!AlphaValidator::isInteger($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 Integer value
128: *
129: * @return integer
130: * @since 1.0
131: */
132: public function getValue() {
133: return intval($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 Integer 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: $this->helper = 'Not a valid integer value! A maximum of '.$this->size.' characters is allowed';
157: }else{
158: throw new IllegalArguementException('Error: the value '.$size.' provided by set_size is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
159: }
160: }
161:
162: /**
163: * Get the allowable size of the Integer in the database field
164: *
165: * @return integer
166: * @since 1.0
167: */
168: public function getSize(){
169: return $this->size;
170: }
171:
172: /**
173: * Returns the integer value provided but padded with zeros to MAX_SIZE
174: *
175: * @param integer $val
176: * @since 1.0
177: */
178: public static function zeroPad($val) {
179: return str_pad($val, Integer::MAX_SIZE, '0', STR_PAD_LEFT);
180: }
181: }
182:
183: ?>