1: <?php
2:
3: namespace Alpha\Model\Type;
4:
5: use Alpha\Util\Helper\Validator;
6: use Alpha\Exception\IllegalArguementException;
7: use Alpha\Exception\RecordNotFoundException;
8: use Alpha\Model\ActiveRecord;
9:
10: /**
11: * A custom sequence datatype, which is stored as a string and is made up of a string prefix
12: * and an integer sequence, which is stored in a database.
13: *
14: * @since 1.0
15: *
16: * @author John Collins <dev@alphaframework.org>
17: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
18: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
19: * All rights reserved.
20: *
21: * <pre>
22: * Redistribution and use in source and binary forms, with or
23: * without modification, are permitted provided that the
24: * following conditions are met:
25: *
26: * * Redistributions of source code must retain the above
27: * copyright notice, this list of conditions and the
28: * following disclaimer.
29: * * Redistributions in binary form must reproduce the above
30: * copyright notice, this list of conditions and the
31: * following disclaimer in the documentation and/or other
32: * materials provided with the distribution.
33: * * Neither the name of the Alpha Framework nor the names
34: * of its contributors may be used to endorse or promote
35: * products derived from this software without specific
36: * prior written permission.
37: *
38: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
43: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
49: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51: * </pre>
52: */
53: class Sequence extends ActiveRecord implements TypeInterface
54: {
55: /**
56: * The string prefix (must be capital alphabet characters only).
57: *
58: * @var Alpha\Model\String
59: *
60: * @since 1.0
61: */
62: protected $prefix;
63:
64: /**
65: * The integer sequence number incremented for each Sequence value with this prefix.
66: *
67: * @var Alpha\Model\Integer
68: *
69: * @since 1.0
70: */
71: protected $sequence;
72:
73: /**
74: * The name of the database table for the class.
75: *
76: * @var string
77: *
78: * @since 1.0
79: */
80: const TABLE_NAME = 'Sequence';
81:
82: /**
83: * An array of data display labels for the class properties.
84: *
85: * @var array
86: *
87: * @since 1.0
88: */
89: protected $dataLabels = array('OID' => 'Sequence ID#', 'prefix' => 'Sequence prefix', 'sequence' => 'Sequence number');
90:
91: /**
92: * The message to display to the user when validation fails.
93: *
94: * @var string
95: *
96: * @since 1.0
97: */
98: protected $helper = 'Not a valid sequence value!';
99:
100: /**
101: * The size of the value for the this Sequence.
102: *
103: * @var int
104: *
105: * @since 1.0
106: */
107: protected $size = 255;
108:
109: /**
110: * The validation rule for the Sequence type.
111: *
112: * @var string
113: *
114: * @since 1.0
115: */
116: protected $validationRule;
117:
118: /**
119: * The absolute maximum size of the value for the this Sequence.
120: *
121: * @var int
122: *
123: * @since 1.0
124: */
125: const MAX_SIZE = 255;
126:
127: /**
128: * The constructor.
129: *
130: * @since 1.0
131: */
132: public function __construct()
133: {
134: // ensure to call the parent constructor
135: parent::__construct();
136:
137: $this->validationRule = Validator::ALLOW_ALL;
138:
139: $this->sequence = new Integer();
140:
141: $this->prefix = new String();
142: $this->prefix->setRule(Validator::REQUIRED_ALPHA_UPPER);
143: $this->prefix->setHelper('Sequence prefix must be uppercase string!');
144: $this->markUnique('prefix');
145:
146: $this->markTransient('helper');
147: $this->markTransient('validationRule');
148: $this->markTransient('size');
149: }
150:
151: /**
152: * Get the validation rule.
153: *
154: * @return string
155: *
156: * @since 1.0
157: */
158: public function getRule()
159: {
160: return $this->validationRule;
161: }
162:
163: /**
164: * Sets the sequence number to be the maximum value matching the prefix in the database
165: * plus one. Note that calling this method increments the maximum value in the database.
166: *
167: * @since 1.0
168: */
169: public function setSequenceToNext()
170: {
171: try {
172: $this->loadByAttribute('prefix', $this->prefix->getValue());
173: } catch (RecordNotFoundException $e) {
174: $this->set('sequence', 0);
175: }
176:
177: $this->set('sequence', $this->get('sequence') + 1);
178: $this->save();
179: }
180:
181: /**
182: * Getter for the validation helper string.
183: *
184: * @return string
185: *
186: * @since 1.0
187: */
188: public function getHelper()
189: {
190: return $this->helper;
191: }
192:
193: /**
194: * Set the validation helper text.
195: *
196: * @param string $helper
197: *
198: * @since 1.0
199: */
200: public function setHelper($helper)
201: {
202: $this->helper = $helper;
203: }
204:
205: /**
206: * Used to get the Sequence value as a string.
207: *
208: * @return string
209: *
210: * @since 1.0
211: */
212: public function getValue()
213: {
214: if ($this->prefix->getValue() != '' && $this->sequence->getValue() != 0) {
215: return $this->prefix->getValue().'-'.$this->sequence->getValue();
216: } else {
217: return '';
218: }
219: }
220:
221: /**
222: * Accepts a string to set the Sequence prefix/sequence values to, in the
223: * format PREFIX-00000000000.
224: *
225: * @param string $val
226: *
227: * @since 1.0
228: *
229: * @throws Alpha\Exception\IllegalArguementException
230: */
231: public function setValue($val)
232: {
233: if (mb_strlen($val) <= $this->size) {
234: if (!empty($val)) {
235: if (!Validator::isSequence($val)) {
236: throw new IllegalArguementException($this->helper);
237: }
238:
239: $parts = explode('-', $val);
240: $this->prefix->setValue($parts[0]);
241: $this->sequence->setValue($parts[1]);
242: }
243: } else {
244: throw new IllegalArguementException($this->helper);
245: }
246: }
247:
248: /**
249: * Get the allowable size of the Sequence in the database field.
250: *
251: * @return int
252: *
253: * @since 1.0
254: */
255: public function getSize()
256: {
257: return $this->size;
258: }
259:
260: /**
261: * Used to convert the object to a printable string.
262: *
263: * @return string
264: *
265: * @since 1.0
266: */
267: public function __toString()
268: {
269: return $this->prefix->getValue().'-'.$this->sequence->getValue();
270: }
271: }
272: