/** * Converts a value from its database representation to its PHP representation * * @param mixed $value The value to convert * @param AbstractPlatform $platform The currently used database platform * * @return mixed * * @throws ConversionException When the conversion fails */ public function convertToPHPValue($value, AbstractPlatform $platform) { if (empty($value)) { return null; } if ($value instanceof Timezone) { return $value; } try { $timezone = Timezone::create($value); } catch (Exception $exception) { throw ConversionException::conversionFailed($value, static::TYPE_NAME); } return $timezone; }
/** * @expectedException Novuso\System\Exception\DomainException */ public function test_that_constructor_throws_exception_for_invalid_timezone() { Timezone::create('Universal'); }
/** * Creates instance from a date/time string * * @param string $dateTime The date/time string * * @return DateTime * * @throws DomainException When the string is invalid */ public static function fromString($dateTime) { assert(Test::isString($dateTime), sprintf('%s expects $dateTime to be a string; received (%s) %s', __METHOD__, gettype($dateTime), VarPrinter::toString($dateTime))); $pattern = sprintf('/\\A%s-%s-%sT%s:%s:%s\\.%s\\[%s\\]\\z/', '(?P<year>[\\d]{4})', '(?P<month>[\\d]{2})', '(?P<day>[\\d]{2})', '(?P<hour>[\\d]{2})', '(?P<minute>[\\d]{2})', '(?P<second>[\\d]{2})', '(?P<micro>[\\d]{6})', '(?P<timezone>.+)'); if (!preg_match($pattern, $dateTime, $matches)) { $message = sprintf('%s expects $dateTime in "Y-m-d\\TH:i:s.u[timezone]" format', __METHOD__); throw DomainException::create($message); } $year = (int) $matches['year']; $month = (int) $matches['month']; $day = (int) $matches['day']; $hour = (int) $matches['hour']; $minute = (int) $matches['minute']; $second = (int) $matches['second']; $micro = (int) $matches['micro']; $timezone = $matches['timezone']; return new self(Date::create($year, $month, $day), Time::create($hour, $minute, $second, $micro), Timezone::create($timezone)); }
/** * @expectedException \AssertionError */ public function test_that_compare_to_throws_exception_for_invalid_argument() { $timezone = Timezone::create('UTC'); $timezone->compareTo('America/Chicago'); }
/** * Creates instance with a given timezone * * Note: This method does not convert the date/time values * * @param mixed $timezone The timezone value * * @return DateTime * * @throws DomainException When the timezone is not valid */ public function withTimezone($timezone) : DateTime { return new static($this->date(), $this->time(), Timezone::create($timezone)); }