Skip to content

Commit

Permalink
Added from()
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Laviale committed Sep 3, 2013
1 parent 70aa3c5 commit dd66267
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __get($property)

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

return $this->location;
Expand Down
21 changes: 21 additions & 0 deletions lib/timezonelocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@
*/
class TimeZoneLocation
{
static private $cache;

/**
* Creates an instance from a {@link \DateTimeZone} instance.
*
* @param \DateTimeZone $zone
*
* @return \ICanBoogie\TimeZoneLocation
*/
static public function from(\DateTimeZone $zone)
{
$hash = spl_object_hash($zone);

if (empty(self::$cache[$hash]))
{
self::$cache[$hash] = new static($zone->getLocation());
}

return self::$cache[$hash];
}

private $location;

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/TimeZoneLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@

class TimeZoneLocationTest extends \PHPUnit_Framework_TestCase
{
public function test_from()
{
$zone = new \DateTimeZone('Europe/Paris');
$location = TimeZoneLocation::from($zone);

$this->assertInstanceOf('ICanBoogie\TimeZoneLocation', $location);
}

public function test_from_cache()
{
$zone = new \DateTimeZone('Europe/Paris');
$location = TimeZoneLocation::from($zone);
$cached = TimeZoneLocation::from($zone);

$this->assertEquals(spl_object_hash($location), spl_object_hash($cached));
}

public function test_location()
{
$zone = new \DateTimeZone('Europe/Paris');
Expand Down

0 comments on commit dd66267

Please sign in to comment.