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