|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpha\Model\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Alpha\Util\Helper\Validator; |
|
6
|
|
|
use Alpha\Exception\IllegalArguementException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The SmallText complex data type. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 1.0 |
|
12
|
|
|
* |
|
13
|
|
|
* @author John Collins <[email protected]> |
|
14
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
|
15
|
|
|
* @copyright Copyright (c) 2017, 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 SmallText extends Type implements TypeInterface |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* The value of the string. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
* |
|
57
|
|
|
* @since 1.0 |
|
58
|
|
|
*/ |
|
59
|
|
|
private $value; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The validation rule for the string type. |
|
63
|
|
|
* |
|
64
|
|
|
* @var string |
|
65
|
|
|
* |
|
66
|
|
|
* @since 1.0 |
|
67
|
|
|
*/ |
|
68
|
|
|
private $validationRule; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The error message for the string type when validation fails. |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
* |
|
75
|
|
|
* @since 1.0 |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $helper = 'Not a valid smalltext value!'; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The size of the value for the this String. |
|
81
|
|
|
* |
|
82
|
|
|
* @var int |
|
83
|
|
|
* |
|
84
|
|
|
* @since 1.0 |
|
85
|
|
|
*/ |
|
86
|
|
|
private $size = 255; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* The absolute maximum size of the value for the this String. |
|
90
|
|
|
* |
|
91
|
|
|
* @var int |
|
92
|
|
|
* |
|
93
|
|
|
* @since 1.0 |
|
94
|
|
|
*/ |
|
95
|
|
|
const MAX_SIZE = 255; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Simple boolean to determine if the string is a password or not. |
|
99
|
|
|
* |
|
100
|
|
|
* @var bool |
|
101
|
|
|
* |
|
102
|
|
|
* @since 1.0 |
|
103
|
|
|
*/ |
|
104
|
|
|
private $password = false; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Constructor. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $val |
|
110
|
|
|
* |
|
111
|
|
|
* @since 1.0 |
|
112
|
|
|
* |
|
113
|
|
|
* @throws Alpha\Exception\IllegalArguementException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function __construct($val = '') |
|
116
|
|
|
{ |
|
117
|
|
|
$this->validationRule = Validator::ALLOW_ALL; |
|
118
|
|
|
|
|
119
|
|
|
if (mb_strlen($val) <= $this->size) { |
|
120
|
|
|
if (preg_match($this->validationRule, $val)) { |
|
121
|
|
|
$this->value = $val; |
|
122
|
|
|
} else { |
|
123
|
|
|
throw new IllegalArguementException($this->helper); |
|
124
|
|
|
} |
|
125
|
|
|
} else { |
|
126
|
|
|
throw new IllegalArguementException($this->helper); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Setter for the value. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $val |
|
134
|
|
|
* |
|
135
|
|
|
* @since 1.0 |
|
136
|
|
|
* |
|
137
|
|
|
* @throws Alpha\Exception\IllegalArguementException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setValue($val) |
|
140
|
|
|
{ |
|
141
|
|
|
if (mb_strlen($val) <= $this->size) { |
|
142
|
|
|
if (preg_match($this->validationRule, $val)) { |
|
143
|
|
|
$this->value = $val; |
|
144
|
|
|
} else { |
|
145
|
|
|
throw new IllegalArguementException($this->helper); |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
throw new IllegalArguementException($this->helper); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Getter for the value. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
* |
|
157
|
|
|
* @since 1.0 |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getValue() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->value; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Setter to override the default validation rule. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $rule |
|
168
|
|
|
* |
|
169
|
|
|
* @since 1.0 |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setRule($rule) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->validationRule = $rule; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the validation rule. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
* |
|
181
|
|
|
* @since 1.0 |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getRule() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->validationRule; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Used to set the allowable size of the String in the database field. |
|
190
|
|
|
* |
|
191
|
|
|
* @param int $size |
|
192
|
|
|
* |
|
193
|
|
|
* @since 1.0 |
|
194
|
|
|
* |
|
195
|
|
|
* @throws Alpha\Exception\IllegalArguementException |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setSize($size) |
|
198
|
|
|
{ |
|
199
|
|
|
if ($size <= self::MAX_SIZE) { |
|
200
|
|
|
$this->size = $size; |
|
201
|
|
|
} else { |
|
202
|
|
|
throw new IllegalArguementException('Error: the value '.$size.' provided by setSize is greater than the MAX_SIZE '.self::MAX_SIZE.' of this data type.'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the allowable size of the Double in the database field. |
|
208
|
|
|
* |
|
209
|
|
|
* @return int |
|
210
|
|
|
* |
|
211
|
|
|
* @since 1.0 |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getSize() |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->size; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Sets up an appropriate validation rule for a required field. |
|
220
|
|
|
* |
|
221
|
|
|
* @param bool $req |
|
222
|
|
|
* |
|
223
|
|
|
* @since 1.0 |
|
224
|
|
|
*/ |
|
225
|
|
|
public function isRequired($req = true) |
|
226
|
|
|
{ |
|
227
|
|
|
if ($req) { |
|
228
|
|
|
$this->validationRule = Validator::REQUIRED_STRING; |
|
229
|
|
|
$this->helper = 'This smalltext requires a value!'; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Define the string as a password (making it required by validation rule). |
|
235
|
|
|
* |
|
236
|
|
|
* @param bool $pass |
|
237
|
|
|
* |
|
238
|
|
|
* @since 1.0 |
|
239
|
|
|
*/ |
|
240
|
|
|
public function isPassword($pass = true) |
|
241
|
|
|
{ |
|
242
|
|
|
$this->password = $pass; |
|
243
|
|
|
|
|
244
|
|
|
if ($pass) { |
|
245
|
|
|
$this->validationRule = '/\w+/'; |
|
246
|
|
|
$this->helper = 'Password is required!'; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Checks to see if the string is a password or not. |
|
252
|
|
|
* |
|
253
|
|
|
* @return bool |
|
254
|
|
|
* |
|
255
|
|
|
* @since 1.0 |
|
256
|
|
|
*/ |
|
257
|
|
|
public function checkIsPassword() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->password; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|