1: <?php
2:
3: namespace Alpha\Model\Type;
4:
5: use Alpha\Util\Helper\Validator;
6: use Alpha\Exception\IllegalArguementException;
7:
8: /**
9: * The Boolean 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 Boolean extends Type implements TypeInterface
51: {
52: /**
53: * The value of the Boolean.
54: *
55: * @var bool
56: *
57: * @since 1.0
58: */
59: private $booleanValue;
60:
61: /**
62: * The binary (1/0) value of the Boolean. This is the value stored in the database.
63: *
64: * @var int
65: *
66: * @since 1.0
67: */
68: private $value;
69:
70: /**
71: * The error message returned for invalid values.
72: *
73: * @var string
74: *
75: * @since 1.0
76: */
77: protected $helper = 'Not a valid Boolean value!';
78:
79: /**
80: * Constructor.
81: *
82: * @param bool $val
83: *
84: * @since 1.0
85: *
86: * @throws Alpha\Exception\IllegalArguementException
87: */
88: public function __construct($val = true)
89: {
90: if (!Validator::isBoolean($val)) {
91: throw new IllegalArguementException($this->helper);
92: }
93:
94: if (Validator::isBooleanTrue($val)) {
95: $this->value = 1;
96: $this->booleanValue = true;
97: } else {
98: $this->value = 0;
99: $this->booleanValue = false;
100: }
101: }
102:
103: /**
104: * Used to set the Boolean value.
105: *
106: * @param mixed $val Will accept a boolean true/false or integer 1/0.
107: *
108: * @since 1.0
109: *
110: * @throws Alpha\Exception\IllegalArguementException
111: */
112: public function setValue($val)
113: {
114: if (!Validator::isBoolean($val)) {
115: throw new IllegalArguementException($this->helper);
116: }
117:
118: if (Validator::isBooleanTrue($val)) {
119: $this->value = 1;
120: $this->booleanValue = true;
121: } else {
122: $this->value = 0;
123: $this->booleanValue = false;
124: }
125: }
126:
127: /**
128: * Used to get the binary (1/0) value of the Boolean. This is the value stored in the database.
129: *
130: * @return int
131: *
132: * @since 1.0
133: */
134: public function getValue()
135: {
136: return $this->value;
137: }
138:
139: /**
140: * Used to get the boolean value of the Boolean.
141: *
142: * @return bool
143: *
144: * @since 1.0
145: */
146: public function getBooleanValue()
147: {
148: return $this->booleanValue;
149: }
150:
151: /**
152: * Used to convert the object to a printable string.
153: *
154: * @return string
155: *
156: * @since 1.0
157: */
158: public function __toString()
159: {
160: return $this->value ? 'true' : 'false';
161: }
162: }
163: