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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
<?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;
use ICanBoogie\CLDR\Units\Unit;
use ICanBoogie\CLDR\Units\Sequence;
/**
* @property-read Sequence $sequence
*
* @property-read Unit $acceleration_g_force
* @property-read Unit $acceleration_meter_per_second_squared
* @property-read Unit $angle_arc_minute
* @property-read Unit $angle_arc_second
* @property-read Unit $angle_degree
* @property-read Unit $angle_radian
* @property-read Unit $area_acre
* @property-read Unit $area_hectare
* @property-read Unit $area_square_centimeter
* @property-read Unit $area_square_foot
* @property-read Unit $area_square_inch
* @property-read Unit $area_square_kilometer
* @property-read Unit $area_square_meter
* @property-read Unit $area_square_mile
* @property-read Unit $area_square_yard
* @property-read Unit $consumption_liter_per_kilometer
* @property-read Unit $consumption_mile_per_gallon
* @property-read Unit $digital_bit
* @property-read Unit $digital_byte
* @property-read Unit $digital_gigabit
* @property-read Unit $digital_gigabyte
* @property-read Unit $digital_kilobit
* @property-read Unit $digital_kilobyte
* @property-read Unit $digital_megabit
* @property-read Unit $digital_megabyte
* @property-read Unit $digital_terabit
* @property-read Unit $digital_terabyte
* @property-read Unit $duration_day
* @property-read Unit $duration_hour
* @property-read Unit $duration_microsecond
* @property-read Unit $duration_millisecond
* @property-read Unit $duration_minute
* @property-read Unit $duration_month
* @property-read Unit $duration_nanosecond
* @property-read Unit $duration_second
* @property-read Unit $duration_week
* @property-read Unit $duration_year
* @property-read Unit $electric_ampere
* @property-read Unit $electric_milliampere
* @property-read Unit $electric_ohm
* @property-read Unit $electric_volt
* @property-read Unit $energy_calorie
* @property-read Unit $energy_foodcalorie
* @property-read Unit $energy_joule
* @property-read Unit $energy_kilocalorie
* @property-read Unit $energy_kilojoule
* @property-read Unit $energy_kilowatt_hour
* @property-read Unit $frequency_gigahertz
* @property-read Unit $frequency_hertz
* @property-read Unit $frequency_kilohertz
* @property-read Unit $frequency_megahertz
* @property-read Unit $length_astronomical_unit
* @property-read Unit $length_centimeter
* @property-read Unit $length_decimeter
* @property-read Unit $length_fathom
* @property-read Unit $length_foot
* @property-read Unit $length_furlong
* @property-read Unit $length_inch
* @property-read Unit $length_kilometer
* @property-read Unit $length_light_year
* @property-read Unit $length_meter
* @property-read Unit $length_micrometer
* @property-read Unit $length_mile
* @property-read Unit $length_millimeter
* @property-read Unit $length_nanometer
* @property-read Unit $length_nautical_mile
* @property-read Unit $length_parsec
* @property-read Unit $length_picometer
* @property-read Unit $length_yard
* @property-read Unit $light_lux
* @property-read Unit $mass_carat
* @property-read Unit $mass_gram
* @property-read Unit $mass_kilogram
* @property-read Unit $mass_metric_ton
* @property-read Unit $mass_microgram
* @property-read Unit $mass_milligram
* @property-read Unit $mass_ounce
* @property-read Unit $mass_ounce_troy
* @property-read Unit $mass_pound
* @property-read Unit $mass_stone
* @property-read Unit $mass_ton
* @property-read Unit $power_gigawatt
* @property-read Unit $power_horsepower
* @property-read Unit $power_kilowatt
* @property-read Unit $power_megawatt
* @property-read Unit $power_milliwatt
* @property-read Unit $power_watt
* @property-read Unit $pressure_hectopascal
* @property-read Unit $pressure_inch_hg
* @property-read Unit $pressure_millibar
* @property-read Unit $pressure_millimeter_of_mercury
* @property-read Unit $pressure_pound_per_square_inch
* @property-read Unit $proportion_karat
* @property-read Unit $speed_kilometer_per_hour
* @property-read Unit $speed_meter_per_second
* @property-read Unit $speed_mile_per_hour
* @property-read Unit $temperature_celsius
* @property-read Unit $temperature_fahrenheit
* @property-read Unit $temperature_kelvin
* @property-read Unit $volume_acre_foot
* @property-read Unit $volume_bushel
* @property-read Unit $volume_centiliter
* @property-read Unit $volume_cubic_centimeter
* @property-read Unit $volume_cubic_foot
* @property-read Unit $volume_cubic_inch
* @property-read Unit $volume_cubic_kilometer
* @property-read Unit $volume_cubic_meter
* @property-read Unit $volume_cubic_mile
* @property-read Unit $volume_cubic_yard
* @property-read Unit $volume_cup
* @property-read Unit $volume_deciliter
* @property-read Unit $volume_fluid_ounce
* @property-read Unit $volume_gallon
* @property-read Unit $volume_hectoliter
* @property-read Unit $volume_liter
* @property-read Unit $volume_megaliter
* @property-read Unit $volume_milliliter
* @property-read Unit $volume_pint
* @property-read Unit $volume_quart
* @property-read Unit $volume_tablespoon
* @property-read Unit $volume_teaspoon
*
* @method string acceleration_g_force(mixed $number, string $length = "long")
* @method string acceleration_meter_per_second_squared(mixed $number, string $length = "long")
* @method string angle_arc_minute(mixed $number, string $length = "long")
* @method string angle_arc_second(mixed $number, string $length = "long")
* @method string angle_degree(mixed $number, string $length = "long")
* @method string angle_radian(mixed $number, string $length = "long")
* @method string area_acre(mixed $number, string $length = "long")
* @method string area_hectare(mixed $number, string $length = "long")
* @method string area_square_centimeter(mixed $number, string $length = "long")
* @method string area_square_foot(mixed $number, string $length = "long")
* @method string area_square_inch(mixed $number, string $length = "long")
* @method string area_square_kilometer(mixed $number, string $length = "long")
* @method string area_square_meter(mixed $number, string $length = "long")
* @method string area_square_mile(mixed $number, string $length = "long")
* @method string area_square_yard(mixed $number, string $length = "long")
* @method string consumption_liter_per_kilometer(mixed $number, string $length = "long")
* @method string consumption_mile_per_gallon(mixed $number, string $length = "long")
* @method string digital_bit(mixed $number, string $length = "long")
* @method string digital_byte(mixed $number, string $length = "long")
* @method string digital_gigabit(mixed $number, string $length = "long")
* @method string digital_gigabyte(mixed $number, string $length = "long")
* @method string digital_kilobit(mixed $number, string $length = "long")
* @method string digital_kilobyte(mixed $number, string $length = "long")
* @method string digital_megabit(mixed $number, string $length = "long")
* @method string digital_megabyte(mixed $number, string $length = "long")
* @method string digital_terabit(mixed $number, string $length = "long")
* @method string digital_terabyte(mixed $number, string $length = "long")
* @method string duration_day(mixed $number, string $length = "long")
* @method string duration_hour(mixed $number, string $length = "long")
* @method string duration_microsecond(mixed $number, string $length = "long")
* @method string duration_millisecond(mixed $number, string $length = "long")
* @method string duration_minute(mixed $number, string $length = "long")
* @method string duration_month(mixed $number, string $length = "long")
* @method string duration_nanosecond(mixed $number, string $length = "long")
* @method string duration_second(mixed $number, string $length = "long")
* @method string duration_week(mixed $number, string $length = "long")
* @method string duration_year(mixed $number, string $length = "long")
* @method string electric_ampere(mixed $number, string $length = "long")
* @method string electric_milliampere(mixed $number, string $length = "long")
* @method string electric_ohm(mixed $number, string $length = "long")
* @method string electric_volt(mixed $number, string $length = "long")
* @method string energy_calorie(mixed $number, string $length = "long")
* @method string energy_foodcalorie(mixed $number, string $length = "long")
* @method string energy_joule(mixed $number, string $length = "long")
* @method string energy_kilocalorie(mixed $number, string $length = "long")
* @method string energy_kilojoule(mixed $number, string $length = "long")
* @method string energy_kilowatt_hour(mixed $number, string $length = "long")
* @method string frequency_gigahertz(mixed $number, string $length = "long")
* @method string frequency_hertz(mixed $number, string $length = "long")
* @method string frequency_kilohertz(mixed $number, string $length = "long")
* @method string frequency_megahertz(mixed $number, string $length = "long")
* @method string length_astronomical_unit(mixed $number, string $length = "long")
* @method string length_centimeter(mixed $number, string $length = "long")
* @method string length_decimeter(mixed $number, string $length = "long")
* @method string length_fathom(mixed $number, string $length = "long")
* @method string length_foot(mixed $number, string $length = "long")
* @method string length_furlong(mixed $number, string $length = "long")
* @method string length_inch(mixed $number, string $length = "long")
* @method string length_kilometer(mixed $number, string $length = "long")
* @method string length_light_year(mixed $number, string $length = "long")
* @method string length_meter(mixed $number, string $length = "long")
* @method string length_micrometer(mixed $number, string $length = "long")
* @method string length_mile(mixed $number, string $length = "long")
* @method string length_millimeter(mixed $number, string $length = "long")
* @method string length_nanometer(mixed $number, string $length = "long")
* @method string length_nautical_mile(mixed $number, string $length = "long")
* @method string length_parsec(mixed $number, string $length = "long")
* @method string length_picometer(mixed $number, string $length = "long")
* @method string length_yard(mixed $number, string $length = "long")
* @method string light_lux(mixed $number, string $length = "long")
* @method string mass_carat(mixed $number, string $length = "long")
* @method string mass_gram(mixed $number, string $length = "long")
* @method string mass_kilogram(mixed $number, string $length = "long")
* @method string mass_metric_ton(mixed $number, string $length = "long")
* @method string mass_microgram(mixed $number, string $length = "long")
* @method string mass_milligram(mixed $number, string $length = "long")
* @method string mass_ounce(mixed $number, string $length = "long")
* @method string mass_ounce_troy(mixed $number, string $length = "long")
* @method string mass_pound(mixed $number, string $length = "long")
* @method string mass_stone(mixed $number, string $length = "long")
* @method string mass_ton(mixed $number, string $length = "long")
* @method string power_gigawatt(mixed $number, string $length = "long")
* @method string power_horsepower(mixed $number, string $length = "long")
* @method string power_kilowatt(mixed $number, string $length = "long")
* @method string power_megawatt(mixed $number, string $length = "long")
* @method string power_milliwatt(mixed $number, string $length = "long")
* @method string power_watt(mixed $number, string $length = "long")
* @method string pressure_hectopascal(mixed $number, string $length = "long")
* @method string pressure_inch_hg(mixed $number, string $length = "long")
* @method string pressure_millibar(mixed $number, string $length = "long")
* @method string pressure_millimeter_of_mercury(mixed $number, string $length = "long")
* @method string pressure_pound_per_square_inch(mixed $number, string $length = "long")
* @method string proportion_karat(mixed $number, string $length = "long")
* @method string speed_kilometer_per_hour(mixed $number, string $length = "long")
* @method string speed_meter_per_second(mixed $number, string $length = "long")
* @method string speed_mile_per_hour(mixed $number, string $length = "long")
* @method string temperature_celsius(mixed $number, string $length = "long")
* @method string temperature_fahrenheit(mixed $number, string $length = "long")
* @method string temperature_kelvin(mixed $number, string $length = "long")
* @method string volume_acre_foot(mixed $number, string $length = "long")
* @method string volume_bushel(mixed $number, string $length = "long")
* @method string volume_centiliter(mixed $number, string $length = "long")
* @method string volume_cubic_centimeter(mixed $number, string $length = "long")
* @method string volume_cubic_foot(mixed $number, string $length = "long")
* @method string volume_cubic_inch(mixed $number, string $length = "long")
* @method string volume_cubic_kilometer(mixed $number, string $length = "long")
* @method string volume_cubic_meter(mixed $number, string $length = "long")
* @method string volume_cubic_mile(mixed $number, string $length = "long")
* @method string volume_cubic_yard(mixed $number, string $length = "long")
* @method string volume_cup(mixed $number, string $length = "long")
* @method string volume_deciliter(mixed $number, string $length = "long")
* @method string volume_fluid_ounce(mixed $number, string $length = "long")
* @method string volume_gallon(mixed $number, string $length = "long")
* @method string volume_hectoliter(mixed $number, string $length = "long")
* @method string volume_liter(mixed $number, string $length = "long")
* @method string volume_megaliter(mixed $number, string $length = "long")
* @method string volume_milliliter(mixed $number, string $length = "long")
* @method string volume_pint(mixed $number, string $length = "long")
* @method string volume_quart(mixed $number, string $length = "long")
* @method string volume_tablespoon(mixed $number, string $length = "long")
* @method string volume_teaspoon(mixed $number, string $length = "long")
*/
class Units
{
use AccessorTrait;
use LocalePropertyTrait;
const LENGTH_LONG = 'long';
const LENGTH_SHORT = 'short';
const LENGTH_NARROW = 'narrow';
const DEFAULT_LENGTH = self::LENGTH_LONG;
const COUNT_PREFIX = 'unitPattern-count-';
/**
* @param string $length One of `LENGTH_*`.
*
* @return string One of `LocalizedListFormatter::TYPE_UNIT*`.
*/
static private function length_to_unit_type($length)
{
static $types = [
self::LENGTH_LONG => LocalizedListFormatter::TYPE_UNIT,
self::LENGTH_SHORT => LocalizedListFormatter::TYPE_UNIT_SHORT,
self::LENGTH_NARROW => LocalizedListFormatter::TYPE_UNIT_NARROW,
];
return $types[$length];
}
/**
* @var array
*/
private $data;
/**
* @var Plurals
*/
private $plurals;
/**
* @param Locale $locale
*/
public function __construct(Locale $locale)
{
$this->locale = $locale;
$this->data = $locale['units'];
}
/**
* @inheritdoc
*/
public function __call($name, $arguments)
{
$unit = strtr($name, '_', '-');
if (isset($this->data[self::DEFAULT_LENGTH][$unit]))
{
$number = array_shift($arguments);
array_unshift($arguments, $number, $unit);
return call_user_func_array([ $this, 'format' ], $arguments);
}
throw new \BadMethodCallException("No such unit: $name");
}
/**
* @inheritdoc
*/
public function __get($property)
{
$unit = strtr($property, '_', '-');
if (isset($this->data[self::DEFAULT_LENGTH][$unit]))
{
return $this->$property = new Unit($this, $unit);
}
return $this->accessor_get($property);
}
/**
* @return Sequence
*/
protected function get_sequence()
{
return new Sequence($this);
}
/**
* @param string $unit
*
* @throws \LogicException if the specified unit is not defined.
*/
public function assert_is_unit($unit)
{
if (!isset($this->data[self::DEFAULT_LENGTH][(string) $unit]))
{
throw new \LogicException("No such unit: $unit");
}
}
/**
* @param string $unit
* @param string $length
*
* @return string
*/
public function name_for($unit, $length = self::DEFAULT_LENGTH)
{
return $this->data[$length][$unit]['displayName'];
}
/**
* @param number $number
* @param string $unit
* @param string $length
*
* @return string
*/
public function format($number, $unit, $length = self::DEFAULT_LENGTH)
{
$pattern = $this->pattern_for_unit($unit, $number, $length);
$number = $this->ensure_number_if_formatted($number);
return strtr($pattern, [ '{0}' => $number ]);
}
/**
* Format a combination of units is X per Y, such as _miles per hour_ or _liters per second_.
*
* @param string|number $number
* @param Unit|string $number_unit
* @param Unit|string $per_unit
* @param string $length
*
* @return string
*
* @see http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
*/
public function format_combination($number, $number_unit, $per_unit, $length = self::DEFAULT_LENGTH)
{
$formatted = $this->format($number, $number_unit, $length);
$data = $this->data[$length][$per_unit];
if (isset($data['perUnitPattern']))
{
return strtr($data['perUnitPattern'], [
'{0}' => $formatted
]);
}
$denominator = $this->pattern_for_denominator($per_unit, $number, $length);
$pattern = $this->pattern_for_combination($length);
return strtr($pattern, [
'{0}' => $formatted,
'{1}' => $denominator
]);
}
/**
* Units may be used in composed sequences, such as 5° 30′ for 5 degrees 30 minutes,
* or 3 ft 2 in.For that purpose, the appropriate width of the unit listPattern can be used
* to compose the units in a sequence.
*
* @param array $units_and_numbers
* @param string $length
*
* @return string
*
* @see http://unicode.org/reports/tr35/tr35-general.html#Unit_Sequences
*/
public function format_sequence(array $units_and_numbers, $length = self::DEFAULT_LENGTH)
{
$list = [];
foreach ($units_and_numbers as $unit => $number)
{
$list[] = $this->format($number, $unit, $length);
}
return $this->locale->format_list($list, self::length_to_unit_type($length));
}
/**
* @param string $unit
* @param string|number $number
* @param string $length
*
* @return string
*/
private function pattern_for_unit($unit, $number, $length)
{
$this->assert_is_unit($unit);
$count = $this->count_for($number);
return $this->data[$length][$unit][self::COUNT_PREFIX . $count];
}
/**
* @param string $unit
* @param string|number $number
* @param string $length
*
* @return string
*/
private function pattern_for_denominator($unit, $number, $length)
{
$pattern = $this->pattern_for_unit($unit, $number, $length);
return trim(str_replace('{0}', '', $pattern));
}
/**
* @param string $length
*
* @return string
*/
private function pattern_for_combination($length)
{
return $this->data[$length]['per']['compoundUnitPattern'];
}
/**
* @param number $number
*
* @return string
*/
private function count_for($number)
{
$plurals = &$this->plurals;
if (!$plurals)
{
$plurals = $this->locale->repository->plurals;
}
return $plurals->rule_for($number, $this->locale->language);
}
/**
* @param number|string $number
*
* @return string
*/
private function ensure_number_if_formatted($number)
{
if (is_string($number))
{
return $number;
}
return $this->locale->format_number($number);
}
}