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::util::search
  • 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 Date complex data type
  5:  *
  6:  * @package alpha::model::types
  7:  * @since 1.0
  8:  * @author John Collins <dev@alphaframework.org>
  9:  * @version $Id: Date.inc 1693 2013-12-09 23:33:24Z 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 Date extends AlphaType implements AlphaTypeInterface {
 48: 
 49:     /**
 50:      * The year part
 51:      *
 52:      * @var integer
 53:      * @since 1.0
 54:      */
 55:     private $year;
 56: 
 57:     /**
 58:      * The month part
 59:      *
 60:      * @var integer
 61:      * @since 1.0
 62:      */
 63:     private $month;
 64: 
 65:     /**
 66:      * The day part
 67:      *
 68:      * @var integer
 69:      * @since 1.0
 70:      */
 71:     private $day;
 72: 
 73:     /**
 74:      * The textual version of the day, e.g. Monday
 75:      *
 76:      * @var string
 77:      * @since 1.0
 78:      */
 79:     private $weekday;
 80: 
 81:     /**
 82:      * The validation rule (reg-ex) applied to Date values
 83:      *
 84:      * @var string
 85:      * @since 1.0
 86:      */
 87:     private $validationRule;
 88: 
 89:     /**
 90:      * The error message returned for invalid values
 91:      *
 92:      * @var string
 93:      * @since 1.0
 94:      */
 95:     protected $helper= 'Not a valid date value!  A date should be in the ISO format YYYY-MM-DD.';
 96: 
 97:     /**
 98:      * Constructor.  Leave $date param empty to default to now.
 99:      *
100:      * @param string $date Date string in the ISO format YYYY-MM-DD.
101:      * @since 1.0
102:      * @throws IllegalArguementException
103:      */
104:     public function __construct($date='') {
105:         global $config;
106: 
107:         $this->validationRule = AlphaValidator::ALLOW_ALL;
108: 
109:         if(empty($date)) {
110:             if($config->get('app.default.datetime') == 'now') {
111:                 $this->year=date('Y');
112:                 $this->month=date('m');
113:                 $this->day=date('d');
114:                 $this->weekday=date('l');
115:             }else{
116:                 $this->year='0000';
117:                 $this->month='00';
118:                 $this->day='00';
119:             }
120:         }else{
121:             if (preg_match($this->validationRule, $date)) {
122:                 $this->populateFromString($date);
123:             }else{
124:                 throw new IllegalArguementException($this->helper);
125:             }
126:         }
127:     }
128: 
129:     /**
130:      * Accepts a full date string in ISO YYYY-mm-dd format and populates relevent Date attributes.
131:      *
132:      * @param string $date
133:      * @since 1.0
134:      * @throws IllegalArguementException
135:      */
136:     public function setValue($date) {
137:         $this->populateFromString($date);
138:     }
139: 
140:     /**
141:      * Set the Date attributes to match the three values provided
142:      *
143:      * @param integer $year
144:      * @param integer $month
145:      * @param integer $day
146:      * @throws IllegalArguementException
147:      * @since 1.0
148:      */
149:     public function setDateValue($year, $month, $day) {
150:         $valid = null;
151: 
152:         if (!preg_match('/^[0-9]{4}$/', $year))
153:             $valid = 'Error: the year value '.$year.' provided is invalid!';
154:         if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
155:             $valid = 'Error: the month value '.$month.' provided is invalid!';
156:         if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
157:             $valid = 'Error: the day value '.$day.' provided is invalid!';
158:         if (!isset($valid) && !checkdate($month, $day, $year))
159:             $valid = 'Error: the day value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
160: 
161:         if (isset($valid)){
162:             throw new IllegalArguementException($valid);
163:         }else{
164:             $this->year = $year;
165:             $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
166:             $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
167:             $unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
168:             $this->weekday=date('l', $unixTime);
169:         }
170:     }
171: 
172:     /**
173:      * Get the date value as a string in the format "YYYY-MM-DD"
174:      *
175:      * @return string
176:      * @since 1.0
177:      */
178:     public function getValue() {
179:         return $this->year.'-'.$this->month.'-'.$this->day;
180:     }
181: 
182:     /**
183:      * Return the value in UNIX timestamp format
184:      *
185:      * @return integer
186:      * @since 1.0
187:      */
188:     public function getUnixValue() {
189:         return mktime(0, 0, 0, $this->month, $this->day, $this->year);
190:     }
191: 
192:     /**
193:      * Get the date value as a string in the format "DD/MM/YYYY"
194:      *
195:      * @return string
196:      * @since 1.0
197:      */
198:     public function getEuroValue() {
199:         return $this->day.'/'.$this->month.'/'.mb_substr($this->year, 2, 2);
200:     }
201: 
202:     /**
203:      * Get the date value as a string in the format "MM/DD/YYYY"
204:      *
205:      * @return string
206:      * @since 1.0
207:      */
208:     public function getUSValue() {
209:         return $this->month.'/'.$this->day.'/'.mb_substr($this->year, 2, 2);
210:     }
211: 
212:     /**
213:      * Get the year part
214:      *
215:      * @return integer
216:      * @since 1.0
217:      */
218:     public function getYear() {
219:         return $this->year;
220:     }
221: 
222:     /**
223:      * Get the month part
224:      *
225:      * @return integer
226:      * @since 1.0
227:      */
228:     public function getMonth() {
229:         return $this->month;
230:     }
231: 
232:     /**
233:      * Get the day part
234:      *
235:      * @return integer
236:      * @since 1.0
237:      */
238:     public function getDay() {
239:         return $this->day;
240:     }
241: 
242:     /**
243:      * Get the textual weekday part, e.g. Monday
244:      *
245:      * @return string
246:      * @since 1.0
247:      */
248:     public function getWeekday() {
249:         return $this->weekday;
250:     }
251: 
252:     /**
253:      * Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes.
254:      *
255:      * @param string $date
256:      * @throws IllegalArguementException
257:      * @since 1.0
258:      */
259:     public function populateFromString($date) {
260:         $valid = null;
261: 
262:         if($date == '' || $date == '0000-00-00') {
263:             $this->year = '0000';
264:             $this->month = '00';
265:             $this->day = '00';
266:         }else{
267:             // This is just here for legacy to ensure that any old time value from a Date object is ignored
268:             $spilt_by_space = explode(" ", $date);
269: 
270:             if(isset($spilt_by_space[0])) {
271:                 $date = $spilt_by_space[0];
272:             }else{
273:                 throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!');
274:             }
275: 
276:             $split_by_dash = explode("-", $date);
277: 
278:             // Parse for the date parts, seperated by "-"
279:             if(isset($split_by_dash[0]) && isset($split_by_dash[1]) && isset($split_by_dash[2])) {
280:                 $year = $split_by_dash[0];
281:                 $month = $split_by_dash[1];
282:                 $day = $split_by_dash[2];
283:             }else{
284:                 throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!');
285:             }
286: 
287:             if (!preg_match('/^[0-9]{4}$/', $year))
288:                 $valid = 'Error: the year value '.$year.' provided is invalid!';
289:             if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
290:                 $valid = 'Error: the month value '.$month.' provided is invalid!';
291:             if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
292:                 $valid = 'Error: the day value '.$day.' provided is invalid!';
293:             if (!isset($valid) && !checkdate($month, $day, $year))
294:                 $valid = 'Error: the date value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
295: 
296:             if (isset($valid)){
297:                 throw new IllegalArguementException($valid);
298:             }else{
299:                 $this->year = $year;
300:                 $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
301:                 $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
302:                 $unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
303:                 $this->weekday=date('l', $unixTime);
304:             }
305:         }
306:     }
307: 
308:     /**
309:      * Get the validation rule
310:      *
311:      * @return string
312:      * @since 1.0
313:      */
314:     public function getRule() {
315:         return $this->validationRule;
316:     }
317: 
318:     /**
319:      * Set the validation rule
320:      *
321:      * @param string $rule
322:      * @since 1.0
323:      */
324:     public function setRule($rule) {
325:         $this->validationRule = $rule;
326:     }
327: }
328: 
329: ?>
Alpha Framework 1.2.4 API Documentation API documentation generated by ApiGen 2.8.0