Overview

Namespaces

  • PHP
  • PhpOptions
    • Types

Classes

  • AType
  • CharType
  • DatetimeType
  • DateType
  • DirectoryType
  • EmailType
  • EnumType
  • FileType
  • InifileType
  • IntegerType
  • RealType
  • SeriesType
  • StringType
  • TimeType
  • Types
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * PhpOptions
  5:  * @link https://github.com/masicek/PhpOptions
  6:  * @author Viktor Mašíček <viktor@masicek.net>
  7:  * @license "New" BSD License
  8:  */
  9: 
 10: namespace PhpOptions\Types;
 11: 
 12: require_once __DIR__ . '/AType.php';
 13: 
 14: /**
 15:  * Date type
 16:  *
 17:  * Format: YEAR(-|.)MONTH(-|.)DAY
 18:  * YEAR = four-digit number
 19:  * MONTH = one-digit or two-digit number or short name (three character)
 20:  * DAY = one-digit or two-digit number
 21:  *
 22:  * @author Viktor Mašíček <viktor@masicek.net>
 23:  */
 24: class DateType extends AType
 25: {
 26: 
 27:     /**
 28:      * Filtered value return as timestamp
 29:      *
 30:      * @var bool
 31:      */
 32:     private $timestamp = FALSE;
 33: 
 34: 
 35:     /**
 36:      * Set object
 37:      *
 38:      * @param array $setting Array of setting of object
 39:      */
 40:     public function __construct($settings = array())
 41:     {
 42:         parent::__construct($settings);
 43:         if ($this->settingsHasFlag('timestamp', $settings))
 44:         {
 45:             $this->timestamp = TRUE;
 46:         }
 47:     }
 48: 
 49: 
 50:     /**
 51:      * Check type of value.
 52:      *
 53:      * @param mixed $value Checked value
 54:      *
 55:      * @return bool
 56:      */
 57:     public function check($value)
 58:     {
 59:         $dateString = $this->getDatetimeString($value);
 60: 
 61:         $isDate = FALSE;
 62:         if ($dateString)
 63:         {
 64:             // check validation
 65:             try {
 66:                 $dateObj = new \DateTime($dateString);
 67:                 $isDate = ($dateObj) ? TRUE : FALSE;
 68:             } catch (\Exception $e) {
 69:                 $isDate = FALSE;
 70:             }
 71:         }
 72: 
 73:         return $isDate;
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Return modified value
 79:      *
 80:      * @param mixed $value Filtered value
 81:      *
 82:      * @return mixed
 83:      */
 84:     protected function useFilter($value)
 85:     {
 86:         $dateString = $this->getDatetimeString($value);
 87:         $date = FALSE;
 88:         if ($dateString)
 89:         {
 90:             $date = new \DateTime($dateString);
 91:             if ($this->timestamp)
 92:             {
 93:                 $date = $date->getTimestamp();
 94:             }
 95:         }
 96:         return $date;
 97:     }
 98: 
 99: 
100:     /**
101:      * Return input date formated for DateTime object.
102:      *
103:      * @param string $value
104:      *
105:      * @return string
106:      */
107:     private function getDatetimeString($value)
108:     {
109:         // parse value
110:         $match = preg_match('/^([0-9]{4})[-.]([0-9a-zA-Z]+)[-.]([0-9]{1,2})$/', $value, $matches);
111:         $date = '';
112:         if ($match)
113:         {
114:             // prepare date string
115:             $year = $matches[1];
116:             $month = $this->complete($matches[2]);
117:             $day = $this->complete($matches[3]);
118:             $date = $year . '-' . $month . '-' . $day . ' 00:00:00';
119:         }
120: 
121:         return $date;
122:     }
123: 
124: 
125:     /**
126:      * Add zero if input value have only one character
127:      *
128:      * @param string $value
129:      *
130:      * @return string
131:      */
132:     private function complete($value)
133:     {
134:         if (strlen($value) == 1)
135:         {
136:             $value = '0' . $value;
137:         }
138:         return $value;
139:     }
140: 
141: 
142: }
143: 
PhpOptions API documentation generated by ApiGen.
Generated using the TokenReflection library.