1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie\CLDR;
13
14 use ICanBoogie\OffsetNotWritable;
15 use ICanBoogie\OffsetNotDefined;
16 use ICanBoogie\PropertyNotDefined;
17
18 19 20 21 22 23 24 25
26 class Locale implements \ArrayAccess
27 {
28 static private $available_sections = array
29 (
30 'ca-buddhist' => 'dates/calendars/buddhist',
31 'ca-chinese' => 'dates/calendars/chinese',
32 'ca-coptic' => 'dates/calendars/coptic',
33 'ca-dangi' => 'dates/calendars/dangi',
34 'ca-ethiopic-amete-alem' => 'dates/calendars/ethiopic-amete-alem',
35 'ca-ethiopic' => 'dates/calendars/ethiopic',
36 'ca-generic' => 'dates/calendars/generic',
37 'ca-gregorian' => 'dates/calendars/gregorian',
38 'ca-hebrew' => 'dates/calendars/hebrew',
39 'ca-indian' => 'dates/calendars/indian',
40 'ca-islamic-civil' => 'dates/calendars/islamic-civil',
41 'ca-islamic-rgsa' => 'dates/calendars/islamic-rgsa',
42 'ca-islamic-tbla' => 'dates/calendars/islamic-tbla',
43 'ca-islamic-umalqura' => 'dates/calendars/islamic-umalqura',
44 'ca-islamic' => 'dates/calendars/islamic',
45 'ca-japanese' => 'dates/calendars/japanese',
46 'ca-persian' => 'dates/calendars/persian',
47 'ca-roc' => 'dates/calendars/roc',
48 'characters' => 'characters',
49 'contextTransforms' => 'contextTransforms',
50 'currencies' => 'numbers/currencies',
51 'dateFields' => 'dates/fields',
52 'delimiters' => 'delimiters',
53 'languages' => 'localeDisplayNames/languages',
54 'layout' => 'layout',
55 'listPatterns' => 'listPatterns',
56 'localeDisplayNames' => 'localeDisplayNames',
57 'measurementSystemNames' => 'localeDisplayNames/measurementSystemNames',
58 'numbers' => 'numbers',
59 'posix' => 'posix',
60 'scripts' => 'localeDisplayNames/scripts',
61 'territories' => 'localeDisplayNames/territories',
62 'timeZoneNames' => 'dates/timeZoneNames',
63 'transformNames' => 'localeDisplayNames/transformNames',
64 'units' => 'units',
65 'variants' => 'localeDisplayNames/variants'
66 );
67
68 69 70 71 72
73 protected $repository;
74
75 76 77 78 79
80 protected $identity;
81
82 83 84 85 86
87 protected $sections = array();
88
89 90 91 92 93 94
95 public function __construct(Repository $repository, $identity)
96 {
97 $this->repository = $repository;
98 $this->identity = $identity;
99 }
100
101 public function __get($property)
102 {
103 switch ($property)
104 {
105 case 'repository': return $this->get_repository();
106 case 'identity': return $this->get_identity();
107 case 'calendars': return $this->get_calendars();
108 case 'calendar': return $this->get_calendar();
109 }
110
111 throw new PropertyNotDefined(array($property, $this));
112 }
113
114 protected function get_repository()
115 {
116 return $this->repository;
117 }
118
119 protected function get_identity()
120 {
121 return $this->identity;
122 }
123
124 125 126 127 128
129 private $calendars;
130
131 132 133 134 135
136 protected function get_calendars()
137 {
138 if ($this->calendars)
139 {
140 return $this->calendars;
141 }
142
143 return $this->calendars = new CalendarCollection($this);
144 }
145
146 private $calendar;
147
148 protected function get_calendar()
149 {
150 if ($this->calendar)
151 {
152 return $this->calendar;
153 }
154
155 return $this->calendar = $this->get_calendars()->offsetGet('gregorian');
156 }
157
158 public function offsetExists($offset)
159 {
160 return isset(self::$available_sections[$offset]);
161 }
162
163 public function offsetGet($offset)
164 {
165 if (empty($this->sections[$offset]))
166 {
167 if (empty(self::$available_sections[$offset]))
168 {
169 throw new OffsetNotDefined(array($offset, $this));
170 }
171
172 $data = $this->repository->provider->fetch("{$this->identity}/{$offset}");
173 $path = "main/{$this->identity}/" . self::$available_sections[$offset];
174 $path_parts = explode('/', $path);
175
176 foreach ($path_parts as $part)
177 {
178 $data = $data[$part];
179 }
180
181 $this->sections[$offset] = $data;
182 }
183
184 return $this->sections[$offset];
185 }
186
187 public function offsetSet($offset, $value)
188 {
189 throw new OffsetNotWritable(array($offset, $this));
190 }
191
192 public function offsetUnset($offset)
193 {
194 throw new OffsetNotWritable(array($offset, $this));
195 }
196 }