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