Overview

Packages

  • alpha::controller
  • alpha::controller::front
  • alpha::exceptions
  • alpha::model
  • alpha::model::types
  • alpha::tasks
  • alpha::tests
  • alpha::util
  • alpha::util::cache
  • alpha::util::codehighlight
  • alpha::util::convertors
  • alpha::util::feeds
  • alpha::util::filters
  • alpha::util::graphs
  • alpha::util::helpers
  • alpha::util::metrics
  • alpha::view
  • alpha::view::renderers
  • alpha::view::widgets

Classes

  • AlphaType
  • Boolean
  • Date
  • DEnum
  • DEnumItem
  • Double
  • Enum
  • Integer
  • Relation
  • RelationLookup
  • Sequence
  • String
  • Text
  • Timestamp

Interfaces

  • AlphaTypeInterface
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * The Timestamp complex data type
  5:  *
  6:  * @package alpha::model::types
  7:  * @since 1.0
  8:  * @author John Collins <dev@alphaframework.org>
  9:  * @version $Id: Timestamp.inc 1611 2012-12-18 11:21:25Z alphadevx $
 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 Timestamp extends AlphaType implements AlphaTypeInterface {
 48:     /**
 49:      * The year part
 50:      *
 51:      * @var integer
 52:      * @since 1.0
 53:      */
 54:     private $year;
 55: 
 56:     /**
 57:      * The month part
 58:      *
 59:      * @var integer
 60:      * @since 1.0
 61:      */
 62:     private $month;
 63: 
 64:     /**
 65:      * The day part
 66:      *
 67:      * @var integer
 68:      * @since 1.0
 69:      */
 70:     private $day;
 71: 
 72:     /**
 73:      * The hour part
 74:      *
 75:      * @var integer
 76:      * @since 1.0
 77:      */
 78:     private $hour;
 79: 
 80:     /**
 81:      * The minute part
 82:      *
 83:      * @var integer
 84:      * @since 1.0
 85:      */
 86:     private $minute;
 87: 
 88:     /**
 89:      * The second part
 90:      *
 91:      * @var integer
 92:      * @since 1.0
 93:      */
 94:     private $second;
 95: 
 96:     /**
 97:      * The textual version of the day, e.g. Monday
 98:      *
 99:      * @var string
100:      * @since 1.0
101:      */
102:     private $weekday;
103: 
104:     /**
105:      * The validation rule (reg-ex) applied to Date values
106:      *
107:      * @var string
108:      * @since 1.0
109:      */
110:     private $validationRule;
111: 
112:     /**
113:      * The error message returned for invalid values
114:      *
115:      * @var string
116:      * @since 1.0
117:      */
118:     private $helper= 'Not a valid timestamp value!  A timestamp should be in the format YYYY-MM-DD HH:MM:SS.';
119: 
120:     /**
121:      * Constructor
122:      *
123:      * @since 1.0
124:      * @throws IllegalArguementException
125:      */
126:     public function __construct($timestamp='') {
127:         global $config;
128: 
129:         $this->validationRule = AlphaValidator::ALLOW_ALL;
130: 
131:         if(empty($timestamp)) {
132:             if($config->get('app.default.datetime') == 'now') {
133:                 $this->year=date('Y');
134:                 $this->month=date('m');
135:                 $this->day=date('d');
136:                 $this->weekday=date('l');
137:                 $this->hour=date('H');
138:                 $this->minute=date('i');
139:                 $this->second=date('s');
140:             }else{
141:                 $this->year = '0000';
142:                 $this->month = '00';
143:                 $this->day = '00';
144:                 $this->hour = '00';
145:                 $this->minute = '00';
146:                 $this->second = '00';
147:             }
148:         }else{
149:             if (preg_match($this->validationRule, $timestamp)) {
150:                 $this->populateFromString($timestamp);
151:             }else{
152:                 throw new IllegalArguementException($this->helper);
153:             }
154:         }
155:     }
156: 
157:     /**
158:      * Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format
159:      *
160:      * @param string $dateTime
161:      * @since 1.0
162:      */
163:     public function setValue($dateTime) {
164:         $this->populateFromString($dateTime);
165:     }
166: 
167:     /**
168:      * Setter for the timestamp value
169:      *
170:      * @param integer $year
171:      * @param integer $month
172:      * @param integer $day
173:      * @param integer $hour
174:      * @param integer $minute
175:      * @param integer $second
176:      * @since 1.0
177:      * @throws IllegalArguementException
178:      */
179:     public function setTimestampValue($year, $month, $day, $hour, $minute, $second) {
180:         $valid = null;
181: 
182:         if (!preg_match('/^[0-9]{4}$/', $year))
183:             $valid = 'The year value '.$year.' provided is invalid!';
184:         if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
185:             $valid = 'The month value '.$month.' provided is invalid!';
186:         if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
187:             $valid = 'The day value '.$day.' provided is invalid!';
188:         if (!isset($valid) && !checkdate($month, $day, $year))
189:             $valid = 'The day value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
190:         if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $hour) || !($hour >= 0 && $hour < 24))
191:             $valid = 'The hour value '.$hour.' provided is invalid!';
192:         if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60))
193:             $valid = 'The minute value '.$minute.' provided is invalid!';
194:         if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60))
195:             $valid = 'The second value '.$second.' provided is invalid!';
196: 
197:         if (isset($valid)){
198:             throw new IllegalArguementException($valid);
199:         }else{
200:             $this->year = $year;
201:             $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
202:             $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
203:             $this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
204:             $this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
205:             $this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
206:             $unixTime = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
207:             $this->weekday=date('l', $unixTime);
208:         }
209:     }
210: 
211:     /**
212:      * Getter for the Timestamp value
213:      *
214:      * @return string
215:      * @since 1.0
216:      */
217:     public function getValue() {
218:         return $this->year.'-'.$this->month.'-'.$this->day.' '.$this->hour.':'.$this->minute.':'.$this->second;
219:     }
220: 
221:     /**
222:      * Return the value in UNIX timestamp format
223:      *
224:      * @return integer
225:      * @since 1.0
226:      */
227:     public function getUnixValue() {
228:         return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
229:     }
230: 
231:     /**
232:      * Getter for the date part
233:      *
234:      * @return string
235:      * @since 1.0
236:      */
237:     public function getDate() {
238:         return $this->year.'-'.$this->month.'-'.$this->day;
239:     }
240: 
241:     /**
242:      * Get the date value as a string in the format "DD/MM/YYYY"
243:      *
244:      * @return string
245:      * @since 1.0
246:      */
247:     public function getEuroValue() {
248:         return $this->day.'/'.$this->month.'/'.substr($this->year, 2, 2);
249:     }
250: 
251:     /**
252:      * Setter for the date part
253:      *
254:      * @param integer $year
255:      * @param integer $month
256:      * @param integer $day
257:      * @since 1.0
258:      * @throws IllegalArguementException
259:      */
260:     public function setDate($year, $month, $day) {
261:         $valid = null;
262: 
263:         if (!preg_match('/^[0-9]{4}$/', $year))
264:             $valid = 'The year value '.$year.' provided is invalid!';
265:         if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
266:             $valid = 'The month value '.$month.' provided is invalid!';
267:         if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
268:             $valid = 'The day value '.$day.' provided is invalid!';
269:         if (!isset($valid) && !checkdate($month, $day, $year))
270:             $valid = 'The day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
271: 
272:         if (isset($valid)){
273:             throw new IllegalArguementException($valid);
274:         }else{
275:             $this->year = $year;
276:             $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
277:             $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
278:             $unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
279:             $this->weekday=date('l', $unixTime);
280:         }
281:     }
282: 
283:     /**
284:      * Getter for the time part
285:      *
286:      * @return string
287:      * @since 1.0
288:      */
289:     public function getTime() {
290:         return $this->hour.':'.$this->minute.':'.$this->second;
291:     }
292: 
293:     /**
294:      * Getter for the year part
295:      *
296:      * @return string
297:      * @since 1.0
298:      */
299:     public function getYear() {
300:         return $this->year;
301:     }
302: 
303:     /**
304:      * Getter for the month part
305:      *
306:      * @return string
307:      * @since 1.0
308:      */
309:     public function getMonth() {
310:         return $this->month;
311:     }
312: 
313:     /**
314:      * Getter for the day part
315:      *
316:      * @return string
317:      * @since 1.0
318:      */
319:     public function getDay() {
320:         return $this->day;
321:     }
322: 
323:     /**
324:      * Get the textual weekday part, e.g. Monday
325:      *
326:      * @return string
327:      * @since 1.0
328:      */
329:     public function getWeekday() {
330:         return $this->weekday;
331:     }
332: 
333:     /**
334:      * Getter for the hour part
335:      *
336:      * @return string
337:      * @since 1.0
338:      */
339:     public function getHour() {
340:         return $this->hour;
341:     }
342: 
343:     /**
344:      * Getter for the minute part
345:      *
346:      * @return string
347:      * @since 1.0
348:      */
349:     public function getMinute() {
350:         return $this->minute;
351:     }
352: 
353:     /**
354:      * Getter for the second part
355:      *
356:      * @return string
357:      * @since 1.0
358:      */
359:     public function getSecond() {
360:         return $this->second;
361:     }
362: 
363:     /**
364:      * Setter for the time part
365:      *
366:      * @param integer $hour
367:      * @param integer $minute
368:      * @param integer $second
369:      * @since 1.0
370:      * @throws IllegalArguementException
371:      */
372:     function setTime($hour, $minute, $second) {
373:         $valid = null;
374: 
375:         if(!isset($valid) && !preg_match('/^[0-9]{2}$/', $hour) || !($hour >= 0 && $hour < 24))
376:             $valid = 'The hour value '.$hour.' provided is invalid!';
377:         if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60))
378:             $valid = 'The minute value '.$minute.' provided is invalid!';
379:         if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60))
380:             $valid = 'The second value '.$second.' provided is invalid!';
381: 
382:         if (isset($valid)) {
383:             throw new IllegalArguementException($valid);
384:         }else{
385:             $this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
386:             $this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
387:             $this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
388:         }
389:     }
390: 
391:     /**
392:      * Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format
393:      *
394:      * @param string $dateTime
395:      * @since 1.0
396:      * @throws IllegalArguementException
397:      */
398:     public function populateFromString($dateTime) {
399:         $valid = null;
400: 
401:         if($dateTime == 'Please select' || $dateTime == '0000-00-00 00:00:00') {
402:             $this->year = '0000';
403:             $this->month = '00';
404:             $this->day = '00';
405:             $this->hour = '00';
406:             $this->minute = '00';
407:             $this->second = '00';
408:         }else{
409:             $spilt_by_space = explode(" ", $dateTime);
410: 
411:             if(isset($spilt_by_space[0])) {
412:                 $date = $spilt_by_space[0];
413:             }else{
414:                 throw new IllegalArguementException($this->helper);
415:             }
416: 
417:             if(isset($spilt_by_space[1])) {
418:                 $time = $spilt_by_space[1];
419:             }else{
420:                 throw new IllegalArguementException($this->helper);
421:             }
422: 
423:             $split_by_dash = explode("-", $date);
424: 
425:             if(isset($split_by_dash[0])) {
426:                 $year = $split_by_dash[0];
427:             }else{
428:                 throw new IllegalArguementException($this->helper);
429:             }
430: 
431:             if(isset($split_by_dash[1])) {
432:                 $month = $split_by_dash[1];
433:             }else{
434:                 throw new IllegalArguementException($this->helper);
435:             }
436: 
437:             if(isset($split_by_dash[2])) {
438:                 $day = $split_by_dash[2];
439:             }else{
440:                 throw new IllegalArguementException($this->helper);
441:             }
442: 
443:             $split_by_colon = explode(":", $time);
444: 
445:             if(isset($split_by_colon[0])) {
446:                 $hour = $split_by_colon[0];
447:             }else{
448:                 throw new IllegalArguementException($this->helper);
449:             }
450: 
451:             if(isset($split_by_colon[1])) {
452:                 $minute = $split_by_colon[1];
453:             }else{
454:                 throw new IllegalArguementException($this->helper);
455:             }
456: 
457:             if(isset($split_by_colon[2])) {
458:                 $second = $split_by_colon[2];
459:             }else{
460:                 throw new IllegalArguementException($this->helper);
461:             }
462: 
463:             if(!preg_match('/^[0-9]{4}$/', $year))
464:                 $valid = 'The year value '.$year.' provided is invalid!';
465:             if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
466:                 $valid = 'The month value '.$month.' provided is invalid!';
467:             if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
468:                 $valid = 'The day value '.$day.' provided is invalid!';
469:             if(!isset($valid) && !checkdate($month, $day, $year))
470:                 $valid = 'The day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
471:             if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $hour) || !($hour >= 0 && $hour < 24))
472:                 $valid = 'The hour value '.$hour.' provided is invalid!';
473:             if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60))
474:                 $valid = 'The minute value '.$minute.' provided is invalid!';
475:             if(!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60))
476:                 $valid = 'The second value '.$second.' provided is invalid!';
477: 
478:             if (isset($valid)){
479:                 throw new IllegalArguementException($valid);
480:             }else{
481:                 $this->year = $year;
482:                 $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
483:                 $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
484:                 $this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
485:                 $this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
486:                 $this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
487:                 $unixTime = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
488:                 $this->weekday=date('l', $unixTime);
489:             }
490:         }
491:     }
492: 
493:     /**
494:      * Get the validation rule
495:      *
496:      * @return string
497:      * @since 1.0
498:      */
499:     public function getRule() {
500:         return $this->validationRule;
501:     }
502: 
503:     /**
504:      * Set the validation rule
505:      *
506:      * @param string $rule
507:      * @since 1.0
508:      */
509:     public function setRule($rule) {
510:         $this->validationRule = $rule;
511:     }
512: 
513:     /**
514:      * Get the validation helper text
515:      *
516:      * @return string
517:      * @since 1.0
518:      */
519:     public function getHelper() {
520:         return $this->helper;
521:     }
522: 
523:     /**
524:      * Set the validation helper text
525:      *
526:      * @param string $helper
527:      * @since 1.0
528:      */
529:     public function setHelper($helper) {
530:         $this->helper = $helper;
531:     }
532: }
533: 
534: ?>
Alpha Framework ${alpha.version.new} API Documentation API documentation generated by ApiGen 2.8.0