1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie;
13
14 /**
15 * Representation of a time zone location.
16 *
17 * <pre>
18 * <?php
19 *
20 * use ICanBoogie\TimeZoneLocation;
21 *
22 * $zone = new \DateTimeZone('Europe/Paris');
23 * $location = new TimeZoneLocation($zone->getLocation());
24 *
25 * echo $location; // FR,48.86667,2.33333
26 * echo $location->country_code; // FR
27 * echo $location->latitude; // 48.86667
28 * echo $location->longitude; // 2.33333
29 *
30 * $location->latitude = true; // throws ICanBoogie\PropertyNotWritable
31 * </pre>
32 *
33 * @property-read string $country_code The country code of the location.
34 * @property-read float $latitude The latitude of the location.
35 * @property-read float $longitude The longitude of the location.
36 * @property-read string $comments Comments on the location.
37 */
38 class TimeZoneLocation
39 {
40 static private $cache;
41
42 /**
43 * Creates an instance from a {@link \DateTimeZone} instance.
44 *
45 * @param \DateTimeZone $zone
46 *
47 * @return \ICanBoogie\TimeZoneLocation
48 */
49 static public function from(\DateTimeZone $zone)
50 {
51 $hash = spl_object_hash($zone);
52
53 if (empty(self::$cache[$hash]))
54 {
55 self::$cache[$hash] = new static($zone->getLocation());
56 }
57
58 return self::$cache[$hash];
59 }
60
61 private $location;
62
63 /**
64 * Initializes the {@link $location} property.
65 *
66 * @param array $location Location information provided by {@link \DateTimeZone::getLocation()}.
67 */
68 public function __construct(array $location)
69 {
70 $this->location = $location;
71 }
72
73 /**
74 * Returns the {@link $country_code}, {@link $latitude}, {@link $longitude} and
75 * {@link $comments} properties.
76 *
77 * @param string $property
78 *
79 * @throws PropertyNotDefined in attempt to get an unsupported property.
80 */
81 public function __get($property)
82 {
83 if (isset($this->location[$property]))
84 {
85 return $this->location[$property];
86 }
87
88 if (class_exists('ICanBoogie\PropertyNotDefined'))
89 {
90 throw new PropertyNotDefined(array($property, $this));
91 }
92 else
93 {
94 throw new \RuntimeException("Property is not defined: $property.");
95 }
96 }
97
98 /**
99 * @throws PropertyNotWritable in attempt to set an unsupported property.
100 */
101 public function __set($property, $value)
102 {
103 if (class_exists('ICanBoogie\PropertyNotWritable'))
104 {
105 throw new PropertyNotWritable(array($property, $this));
106 }
107 else
108 {
109 throw new \RuntimeException("Property is not writable: $property.");
110 }
111 }
112
113 /**
114 * Returns the instance formatted as "{$country_code},{$latitude},{$longitude}".
115 *
116 * @return string
117 */
118 public function __toString()
119 {
120 return "{$this->country_code},{$this->latitude},{$this->longitude}";
121 }
122 }