1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\CLDR;
use ICanBoogie\Accessor\AccessorTrait;
/**
* A territory.
*
* @property-read array $containment The `territoryContainment` data.
* @property-read array $info The `territoryInfo` data.
* @property-read array $currencies The currencies available in the country.
* @property-read string $currency The current currency.
* @property-read string $first_day The code of the first day of the week for the territory.
* @property-read string $weekend_start The code of the first day of the weekend.
* @property-read string $weekend_end The code of the last day of the weekend.
* @property-read string|bool $language The ISO code of the official language, or `false' if it
* cannot be determined.
* @property-read string $name_as_* The name of the territory in the specified language.
* @property-read int $population The population of the territory.
*
* @see http://www.unicode.org/reports/tr35/tr35-numbers.html#Supplemental_Currency_Data
*/
class Territory
{
use AccessorTrait;
use RepositoryPropertyTrait;
use CodePropertyTrait;
/**
* Initialize the {@link $repository} and {@link $code} properties.
*
* @param Repository $repository
* @param string $code The ISO code of the territory.
*/
public function __construct(Repository $repository, $code)
{
$this->repository = $repository;
$this->code = $code;
$repository->territories->assert_defined($code);
}
/**
* @inheritdoc
*/
public function __get($property)
{
if (strpos($property, 'name_as_') === 0)
{
$locale_code = substr($property, strlen('name_as_'));
$locale_code = strtr($locale_code, '_', '-');
return $this->name_as($locale_code);
}
return $this->accessor_get($property);
}
/**
* Retrieve a territory section from supplemental.
*
* @param string $section
*
* @return mixed
*/
private function retrieve_from_supplemental($section)
{
return $this->repository->supplemental[$section][$this->code];
}
/**
* Return the `territoryContainment` data.
*
* @return array
*/
protected function lazy_get_containment()
{
return $this->retrieve_from_supplemental('territoryContainment');
}
/**
* Returns the currencies used throughout the history of the territory.
*
* @return array
*/
protected function lazy_get_currencies()
{
return $this->repository->supplemental['currencyData']['region'][$this->code];
}
/**
* @return Currency
*/
protected function lazy_get_currency()
{
return $this->currency_at();
}
/**
* Return the currency used in the territory at a point in time.
*
* @param \DateTimeInterface|mixed $date
*
* @return Currency
*/
public function currency_at($date = null)
{
$date = $this->ensure_is_datetime($date);
$code = $this->find_currency_at($this->currencies, $date->format('Y-m-d'));
if (!$code)
{
return null;
}
return new Currency($this->repository, $code);
}
/**
* Return the currency in a list used at a point in time.
*
* @param array $currencies
* @param string $normalized_date
*
* @return string
*/
private function find_currency_at(array $currencies, $normalized_date)
{
$rc = false;
foreach ($currencies as $currency)
{
$name = key($currency);
$interval = current($currency);
$_from = null;
$_to = null;
extract($interval);
if (($_from && $_from > $normalized_date) || ($_to && $_to < $normalized_date))
{
continue;
}
$rc = $name;
}
return $rc;
}
/*
* weekData
*/
/**
* Returns week data.
*
* @param string $which Week data code.
*
* @return array
*/
private function get_week_data($which)
{
$code = $this->code;
$data = $this->repository->supplemental['weekData'][$which];
return empty($data[$code]) ? $data['001'] : $data[$code];
}
/**
* Return the code of the first day of the week.
*
* @return string
*/
protected function get_first_day()
{
return $this->get_week_data('firstDay');
}
/**
* Return the code of the first day of the weekend.
*
* @return string
*/
protected function get_weekend_start()
{
return $this->get_week_data('weekendStart');
}
/**
* Return the code of the last day of the weekend.
*
* @return string
*/
protected function get_weekend_end()
{
return $this->get_week_data('weekendEnd');
}
/**
* Return the `territoryInfo` data.
*
* @return array
*/
protected function lazy_get_info()
{
return $this->retrieve_from_supplemental('territoryInfo');
}
/**
* Return the ISO code of the official language of the territory.
*
* @return string|bool The ISO code of the official language, or `false' if it cannot be
* determined.
*/
protected function lazy_get_language()
{
$info = $this->info;
foreach ($info['languagePopulation'] as $language => $lp)
{
if (empty($lp['_officialStatus']) || ($lp['_officialStatus'] != "official" && $lp['_officialStatus'] != "de_facto_official"))
{
continue;
}
return $language;
}
return false;
}
/**
* Return the population of the territory.
*
* @return int
*/
protected function get_population()
{
$info = $this->info;
return (int) $info['_population'];
}
/**
* Whether the territory contains the specified territory.
*
* @param string $code
*
* @return bool
*/
public function is_containing($code)
{
$containment = $this->containment;
return in_array($code, $containment['_contains']);
}
/**
* Return the name of the territory localized according to the specified locale code.
*
* @param string $locale_code The ISO code of the locale.
*
* @return string
*/
public function name_as($locale_code)
{
return $this->localize($locale_code)->name;
}
/**
* Localize the currency.
*
* @param string $locale_code
*
* @return LocalizedCurrency
*/
public function localize($locale_code)
{
return $this->repository->locales[$locale_code]->localize($this);
}
/**
* @param \DateTimeInterface|string $datetime
*
* @return \DateTimeInterface
*/
private function ensure_is_datetime($datetime)
{
return $datetime instanceof \DateTimeInterface
? $datetime
: new \DateTime($datetime);
}
}