Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • BigDecimal
  • BigInt
  • ByteStream
  • LocalDate
  • Location
  • Money
  • Name
  • Point
  • S3
  • Timestamp
  • Utils
  • UUID
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace NGS;
  3: 
  4: require_once(__DIR__.'/Utils.php');
  5: require_once(__DIR__.'/Timestamp.php');
  6: 
  7: use NGS\Utils;
  8: 
  9: /**
 10:  * Date object with timezone
 11:  * Internally uses core \Datetime object
 12:  *
 13:  * @property \DateTime $value Returns DateTime object
 14:  */
 15: class LocalDate
 16: {
 17:     const DEFAULT_TIMEZONE = 'UTC';
 18:     const STRING_FORMAT = 'Y-m-d';
 19:     const FALLBACK_FORMAT = 'Y-m-d\\TH:i:s';
 20: 
 21:     /**
 22:      * @var \DateTime $value
 23:      */
 24:     protected $datetime;
 25: 
 26:     /**
 27:      * Constructs a new LocalDate instance using int or float value
 28:      *
 29:      * @param int|float $utime numeric value with epoch time (with decimals representing milli & microseconds)
 30:      * @param string $timezone string representation of a timezone
 31:      *
 32:      * @return \DateTime
 33:      */
 34:     private static function fromNumeric($utime, $timezone)
 35:     {
 36:         $strtime = sprintf('%.6f', $utime);
 37: 
 38:         $dt = \DateTime::createFromFormat('U.u', $strtime, new \DateTimeZone($timezone));
 39:         if($dt === false) {
 40:             throw new \InvalidArgumentException('Cannot initialize "NGS\\LocalDate". Input number was in invalid format: "'.$utime.'"');
 41:         }
 42: 
 43:         $dt->setTimezone(new \DateTimeZone($timezone));
 44:         return $dt;
 45:     }
 46: 
 47:     /**
 48:      * Constructs a new LocalDate instance from microtime epoch
 49:      *
 50:      * @param string $strtime string representation of the date
 51:      * @param string $timezone string representation of a timezone
 52:      * @param string $pattern format in which to parse the date
 53:      *
 54:      * @return \DateTime
 55:      */
 56:     private static function fromString($strtime, $timezone, $pattern)
 57:     {
 58:         try {
 59:             $dt = \DateTime::createFromFormat($pattern, $strtime, new \DateTimeZone($timezone));
 60:             if($dt === false) {
 61:                 if($pattern===self::STRING_FORMAT) {
 62:                     $dt = \DateTime::createFromFormat(self::FALLBACK_FORMAT, $strtime, new \DateTimeZone($timezone));
 63:                 }
 64:                 if($dt === false) {
 65:                     //let's try again
 66:                     $dt = new \DateTime($strtime);
 67:                     if(!$dt instanceof \DateTime) {
 68:                         throw new \InvalidArgumentException('Cannot initialize "NGS\\LocalDate". Input string was in invalid format: "'.$strtime.'"');
 69:                     }
 70:                 }
 71:             }
 72:         }
 73:         catch(\Exception $ex) {
 74:             // propagate our exception
 75:             if ($ex instanceof InvalidArgumentException) {
 76:                 throw $ex;
 77:             }
 78:             // DateTime::__construct() can throw generic \Exception, rather
 79:             // throw InvalidArgumentException
 80:             throw new \InvalidArgumentException('Cannot initialize "NGS\\LocalDate". Input string was in invalid format: "'.$strtime.'"',
 81:                 null,
 82:                 $ex);
 83:         }
 84:         return $dt;
 85:     }
 86: 
 87:     /**
 88:      * Constructs a new LocalDate instance
 89:      *
 90:      * @param \DateTime|\NGS\DateTime|string|int|float|null $value Instance of \DateTime or \NGS\LocalDate, valid string format, date as int/float, or null for current time
 91:      * @param string $timezone|null string representation of a timezone, null defaults to 'UTC'
 92:      * @param string $pattern format in which to parse the date, defaults to 'Y-m-d\\TH:i:s.uP'
 93:      * @throws InvalidArgumentException If cannot initalize from given type
 94:      */
 95:     public function __construct($value = 'now', $pattern = self::STRING_FORMAT, $timezone = self::DEFAULT_TIMEZONE)
 96:     {
 97:         // current date
 98:         if($value === 'now') {
 99:             $value = microtime(true);
100:         }
101: 
102:         if($pattern === null) {
103:             $pattern = self::STRING_FORMAT;
104:         }
105: 
106:         if($value instanceof \DateTime) {
107:             $this->datetime = clone $value;
108:         }
109:         elseif($value instanceof \NGS\Timestamp) {
110:             $this->datetime = $value->toDateTime();
111:         }
112:         elseif($value instanceof \NGS\LocalDate) {
113:             $this->datetime = $value->toDateTime();
114:         }
115:         else if(is_int($value) || is_float($value)) {
116:             $this->datetime = self::fromNumeric($value, $timezone);
117:         }
118:         elseif(is_string($value)) {
119:             $this->datetime = self::fromString($value, $timezone, $pattern);
120:         }
121:         else {
122:             throw new \InvalidArgumentException('LocalDate cannot be constructed from type "'.Utils::getType($value).'", valid types are \NGS\LocalDate, \DateTime, string, int, float or null (for current date/time).');
123:         }
124: 
125:         if($this->datetime === null) {
126:             throw new \InvalidArgumentException('LocalDate could not be constructed from type "'.Utils::getType($value).'" with value: "'.$value.'"');
127:         }
128: 
129:         // LocalDate should have hours/minutes/seconds set to 0, to safely compare with other objects
130:         $this->datetime->setTime(0, 0, 0);
131:     }
132: 
133:     /**
134:      * Converts all elements in array to \NGS\LocalDate instance
135:      *
136:      * @param array $items Source array, each element must be a valid argument for LocalDate constructor
137:      * @return array Resulting array of LocalDate instances
138:      * @throws InvalidArgumentException If any element is null or invalid type for LocalDate constructor
139:      */
140:     public static function toArray(array $items, $allowNullValues=false)
141:     {
142:         $results = array();
143:         try {
144:             foreach ($items as $key => $val) {
145:                 if ($allowNullValues && $val===null) {
146:                     $results[] = null;
147:                 } elseif ($val === null) {
148:                     throw new \InvalidArgumentException('Null value found in provided array');
149:                 } elseif(!$val instanceof \NGS\LocalDate) {
150:                     $results[] = new \NGS\LocalDate($val);
151:                 } else {
152:                     $results[] = $val;
153:                 }
154:             }
155:         }
156:         catch(\Exception $e) {
157:             throw new \InvalidArgumentException('Element at index '.$key.' could not be converted to LocalDate!', 42, $e);
158:         }
159:         return $results;
160:     }
161: 
162:     /**
163:      * Returns time in default format 'Y-m-d\\TH:i:s.uP'
164:      *
165:      * @return string formatted date with time zone
166:      */
167:     public function __toString()
168:     {
169:         return $this->format(self::STRING_FORMAT);
170:     }
171: 
172:     /**
173:      * Returns time in default format 'Y-m-d\\TH:i:s.uP'
174:      *
175:      * @return string formatted date with time zone
176:      */
177:     public function format($pattern)
178:     {
179:         return $this->datetime->format($pattern);
180:     }
181: 
182:     /**
183:      * Checks for equality against another LocalDate instance
184:      *
185:      * @param \NGS\LocalDate $other Instance of NGS\LocalDate
186:      */
187:     public function equals(\NGS\LocalDate $other)
188:     {
189:         return $this->datetime == $other->toDateTime();
190:     }
191: 
192:     /**
193:      * Gets time in Unix timestamp
194:      *
195:      * @return int Unix timestamp
196:      */
197:     public function toInt()
198:     {
199:         return $this->datetime->getTimestamp();
200:     }
201: 
202:     /**
203:      * Gets time value as Datetime instance
204:      *
205:      * @return \DateTime Time value as DateTime instance
206:      */
207:     public function toDateTime()
208:     {
209:         return clone $this->datetime;
210:     }
211: 
212:     /**
213:      * Gets as \NGS\Timestamp instance
214:      *
215:      * @return \NGS\Timestamp Time value as NGS\Timestamp instance
216:      */
217:     public function toTimestamp()
218:     {
219:         return new \NGS\Timestamp($this->datetime);
220:     }
221: }
222: 
API documentation generated by ApiGen 2.8.0