1: <?php
2:
3: namespace Alpha\Model\Type;
4:
5: use Alpha\Util\Helper\Validator;
6: use Alpha\Exception\IllegalArguementException;
7:
8: /**
9: * The Text 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 Text extends Type implements TypeInterface
51: {
52: /**
53: * The value of the Text object.
54: *
55: * @var string
56: *
57: * @since 1.0
58: */
59: private $value;
60:
61: /**
62: * The validation rule for the Text type.
63: *
64: * @var string
65: *
66: * @since 1.0
67: */
68: private $validationRule;
69:
70: /**
71: * Used to determine if the Text object can support HTML content or not. Defaults to true, if set to false
72: * then HTML content should be filtered.
73: *
74: * @var bool
75: *
76: * @since 1.0
77: */
78: private $allowHTML = true;
79:
80: /**
81: * The error message for the string type when validation fails.
82: *
83: * @var string
84: *
85: * @since 1.0
86: */
87: protected $helper = 'Not a valid text value!';
88:
89: /**
90: * The size of the value for the this Text.
91: *
92: * @var int
93: *
94: * @since 1.0
95: */
96: private $size = 65535;
97:
98: /**
99: * The absolute maximum size of the value for the this Text.
100: *
101: * @var int
102: *
103: * @since 1.0
104: */
105: const MAX_SIZE = 65535;
106:
107: /**
108: * Constructor.
109: *
110: * @param string $val
111: *
112: * @since 1.0
113: *
114: * @throws Alpha\Exception\IllegalArguementException
115: */
116: public function __construct($val = '')
117: {
118: $this->validationRule = Validator::ALLOW_ALL;
119:
120: if (mb_strlen($val) <= $this->size) {
121: if (preg_match($this->validationRule, $val)) {
122: $this->value = $val;
123: } else {
124: throw new IllegalArguementException($this->helper);
125: }
126: } else {
127: throw new IllegalArguementException($this->helper);
128: }
129: }
130:
131: /**
132: * Setter for the value.
133: *
134: * @param string $val
135: *
136: * @since 1.0
137: *
138: * @throws Alpha\Exception\IllegalArguementException
139: */
140: public function setValue($val)
141: {
142: if (mb_strlen($val) <= $this->size) {
143: if (preg_match($this->validationRule, $val)) {
144: $this->value = $val;
145: } else {
146: throw new IllegalArguementException($this->helper);
147: }
148: } else {
149: throw new IllegalArguementException($this->helper);
150: }
151: }
152:
153: /**
154: * Getter for the value.
155: *
156: * @return string
157: *
158: * @since 1.0
159: */
160: public function getValue()
161: {
162: return $this->value;
163: }
164:
165: /**
166: * Setter to override the default validation rule.
167: *
168: * @param string $rule
169: *
170: * @since 1.0
171: */
172: public function setRule($rule)
173: {
174: $this->validationRule = $rule;
175: }
176:
177: /**
178: * Get the validation rule.
179: *
180: * @return string
181: *
182: * @since 1.0
183: */
184: public function getRule()
185: {
186: return $this->validationRule;
187: }
188:
189: /**
190: * Used to set the allowable size of the Text in the database field.
191: *
192: * @param int $size
193: *
194: * @since 1.0
195: *
196: * @throws Alpha\Exception\IllegalArguementException
197: */
198: public function setSize($size)
199: {
200: if ($size <= self::MAX_SIZE) {
201: $this->size = $size;
202: } else {
203: throw new IllegalArguementException('The value '.$size.' provided by setSize is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.');
204: }
205: }
206:
207: /**
208: * Get the allowable size of the Double in the database field.
209: *
210: * @return int
211: *
212: * @since 1.0
213: */
214: public function getSize()
215: {
216: return $this->size;
217: }
218:
219: /**
220: * Set the $allowHTML value.
221: *
222: * @param bool $allowHTML
223: *
224: * @since 1.0
225: */
226: public function setAllowHTML($allowHTML)
227: {
228: $this->allowHTML = $allowHTML;
229: }
230:
231: /**
232: * Get the $allowHTML value.
233: *
234: * @return bool
235: *
236: * @since 1.0
237: */
238: public function getAllowHTML()
239: {
240: return $this->allowHTML;
241: }
242: }
243: