Skip to content

Commit

Permalink
Tidying: TimeZone
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 23, 2016
1 parent 97ba1f3 commit 0afea0d
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions lib/TimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,46 +107,20 @@ public function __construct($timezone)
}

/**
* Returns the {@link $location}, {@link $name} and {@link $offset} properties.
*
* @throws \LogicException in attempt to get an unsupported property.
* @throws \LogicException in attempt to get an undefined property.
*
* @inheritdoc
*/
public function __get($property)
{
switch ($property)
{
case 'location':

if (!$this->location)
{
$this->location = TimeZoneLocation::from($this);
}

return $this->location;

case 'name':

return $this->name;

case 'offset':

$utc_time = self::$utc_time;

if (!$utc_time)
{
self::$utc_time = $utc_time = new \DateTime('now', new \DateTimeZone('utc'));
}
$getter = "get_$property";

return $this->getOffset($utc_time);

case 'is_utc':
case 'is_local':
return $this->{ 'get_' . $property }();
if (!method_exists($this, $getter))
{
throw new \LogicException("Property no defined: $property.");
}

throw new \LogicException("Property no defined: $property.");
return $this->$getter();
}

/**
Expand All @@ -159,6 +133,39 @@ public function __set($property, $value)
throw new \LogicException("Property no writable: $property.");
}

/**
* @return TimeZoneLocation
*/
protected function get_location()
{
$location = &$this->location;

return $location ?: $location = TimeZoneLocation::from($this);
}

/**
* @return string
*/
protected function get_name()
{
return $this->name;
}

/**
* @return int
*/
protected function get_offset()
{
$utc_time = &self::$utc_time;

if (!$utc_time)
{
$utc_time = new \DateTime('now', new \DateTimeZone('utc'));
}

return $this->getOffset($utc_time);
}

/**
* @return bool `true` if time zone is `UTC`, `false` otherwise.
*/
Expand Down

0 comments on commit 0afea0d

Please sign in to comment.