ICanBoogie/CLDR v1.6.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • CLDR

Classes

  • Calendar
  • CalendarCollection
  • ContextTransforms
  • Currency
  • CurrencyCollection
  • CurrencyFormatter
  • DateFormatter
  • DateTimeFormatter
  • FileProvider
  • ListFormatter
  • Locale
  • LocaleCollection
  • LocalizedCurrency
  • LocalizedDateTime
  • LocalizedListFormatter
  • LocalizedLocale
  • LocalizedNumberFormatter
  • LocalizedObject
  • LocalizedObjectWithFormatter
  • LocalizedTerritory
  • NumberFormatter
  • NumberPattern
  • NumberPatternParser
  • Numbers
  • ProviderCollection
  • RedisProvider
  • Repository
  • RunTimeProvider
  • Supplemental
  • Territory
  • TerritoryCollection
  • TimeFormatter
  • WebProvider

Interfaces

  • Exception
  • Formatter
  • LocalizeAwareInterface
  • Provider

Traits

  • CodePropertyTrait
  • CollectionTrait
  • LocalePropertyTrait
  • ProviderStorageBinding
  • RepositoryPropertyTrait

Exceptions

  • ResourceNotFound
  • TerritoryNotDefined
  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 
<?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\OffsetNotWritable;
use ICanBoogie\OffsetNotDefined;

/**
 * Representation of a locale.
 *
 * @property-read Repository $repository The repository provided during construct.
 * @property-read string $code The ISO code of the locale.
 * @property-read string $language The language code.
 * @property-read CalendarCollection $calendars The calendar collection of the locale.
 * @property-read Calendar $calendar The preferred calendar for this locale.
 * @property-read Numbers $numbers
 * @property-read LocalizedNumberFormatter $number_formatter
 * @property-read LocalizedListFormatter $list_formatter
 * @property-read ContextTransforms $context_transforms
 */
class Locale implements \ArrayAccess
{
    static private $available_sections = [

        'ca-buddhist'            => 'dates/calendars/buddhist',
        'ca-chinese'             => 'dates/calendars/chinese',
        'ca-coptic'              => 'dates/calendars/coptic',
        'ca-dangi'               => 'dates/calendars/dangi',
        'ca-ethiopic-amete-alem' => 'dates/calendars/ethiopic-amete-alem',
        'ca-ethiopic'            => 'dates/calendars/ethiopic',
        'ca-generic'             => 'dates/calendars/generic',
        'ca-gregorian'           => 'dates/calendars/gregorian',
        'ca-hebrew'              => 'dates/calendars/hebrew',
        'ca-indian'              => 'dates/calendars/indian',
        'ca-islamic-civil'       => 'dates/calendars/islamic-civil',
        'ca-islamic-rgsa'        => 'dates/calendars/islamic-rgsa',
        'ca-islamic-tbla'        => 'dates/calendars/islamic-tbla',
        'ca-islamic-umalqura'    => 'dates/calendars/islamic-umalqura',
        'ca-islamic'             => 'dates/calendars/islamic',
        'ca-japanese'            => 'dates/calendars/japanese',
        'ca-persian'             => 'dates/calendars/persian',
        'ca-roc'                 => 'dates/calendars/roc',
        'characters'             => 'characters',
        'contextTransforms'      => 'contextTransforms',
        'currencies'             => 'numbers/currencies',
        'dateFields'             => 'dates/fields',
        'delimiters'             => 'delimiters',
        'languages'              => 'localeDisplayNames/languages',
        'layout'                 => 'layout',
        'listPatterns'           => 'listPatterns',
        'localeDisplayNames'     => 'localeDisplayNames',
        'measurementSystemNames' => 'localeDisplayNames/measurementSystemNames',
        'numbers'                => 'numbers',
        'posix'                  => 'posix',
        'scripts'                => 'localeDisplayNames/scripts',
        'territories'            => 'localeDisplayNames/territories',
        'timeZoneNames'          => 'dates/timeZoneNames',
        'transformNames'         => 'localeDisplayNames/transformNames',
        'units'                  => 'units',
        'variants'               => 'localeDisplayNames/variants'

    ];

    use AccessorTrait;
    use RepositoryPropertyTrait;
    use CodePropertyTrait;

    /**
     * Loaded sections.
     *
     * @var array
     */
    protected $sections = [];

    /**
     * Initializes the {@link $repository} and {@link $code} properties.
     *
     * @param Repository $repository
     * @param string $code The ISO code of the locale.
     */
    public function __construct(Repository $repository, $code)
    {
        if (!$code)
        {
            throw new \InvalidArgumentException("Locale identifier cannot be empty.");
        }

        $this->repository = $repository;
        $this->code = $code;
    }

    /**
     * @return string
     */
    protected function get_language()
    {
        list($language) = explode('-', $this->code, 2);

        return $language;
    }

    /**
     * @return CalendarCollection
     */
    protected function lazy_get_calendars()
    {
        return new CalendarCollection($this);
    }

    /**
     * @return Calendar
     */
    protected function lazy_get_calendar()
    {
        return $this->calendars['gregorian']; // TODO-20131101: use preferred data
    }

    /**
     * @return Numbers
     */
    protected function lazy_get_numbers()
    {
        return new Numbers($this, $this['numbers']);
    }

    /**
     * @return LocalizedNumberFormatter
     */
    protected function lazy_get_number_formatter()
    {
        return $this->localize($this->repository->number_formatter);
    }

    /**
     * @return LocalizedListFormatter
     */
    protected function lazy_get_list_formatter()
    {
        return $this->localize($this->repository->list_formatter);
    }

    /**
     * @return ContextTransforms
     */
    protected function lazy_get_context_transforms()
    {
        return new ContextTransforms($this['contextTransforms']);
    }

    /**
     * @inheritdoc
     */
    public function offsetExists($offset)
    {
        return isset(self::$available_sections[$offset]);
    }

    /**
     * @inheritdoc
     */
    public function offsetGet($offset)
    {
        if (isset($this->sections[$offset]))
        {
            return $this->sections[$offset];
        }

        if (empty(self::$available_sections[$offset]))
        {
            throw new OffsetNotDefined([ $offset, $this ]);
        }

        $data = $this->repository->fetch("main/{$this->code}/{$offset}");
        $path = "main/{$this->code}/" . self::$available_sections[$offset];
        $path_parts = explode('/', $path);

        foreach ($path_parts as $part)
        {
            $data = $data[$part];
        }

        return $this->sections[$offset] = $data;
    }

    /**
     * @inheritdoc
     *
     * @throws OffsetNotWritable
     */
    public function offsetSet($offset, $value)
    {
        throw new OffsetNotWritable([ $offset, $this ]);
    }

    /**
     * @inheritdoc
     *
     * @throws OffsetNotWritable
     */
    public function offsetUnset($offset)
    {
        throw new OffsetNotWritable([ $offset, $this ]);
    }

    /**
     * Localize the specified source.
     *
     * @param object|string $source_or_code The source to localize, or the locale code to localize
     * this instance.
     * @param array $options The options are passed to the localizer.
     *
     * @return mixed
     */
    public function localize($source_or_code, array $options = [])
    {
        if (is_string($source_or_code))
        {
            return $this->repository->locales[$source_or_code]->localize($this, $options);
        }

        $constructor = $this->resolve_localize_constructor($source_or_code);

        if ($constructor)
        {
            return call_user_func($constructor, $source_or_code, $this, $options);
        }

        throw new \LogicException("Unable to localize source");
    }

    /**
     * @param string $source
     *
     * @return string|null
     */
    private function resolve_localize_constructor($source)
    {
        $class = get_class($source);

        if ($source instanceof LocalizeAwareInterface)
        {
            return $class . '::localize';
        }

        $base = basename(strtr($class, '\\', '/'));
        $constructor = __NAMESPACE__ . "\\Localized$base";

        if (!class_exists($constructor))
        {
            return null;
        }

        return $constructor . '::from';
    }

    /**
     * Formats a number using {@link $number_formatter}.
     *
     * @param number $number
     * @param string|null $pattern
     * @param array $symbols
     *
     * @return string
     *
     * @see LocalizedNumberFormatter::format
     */
    public function format_number($number, $pattern = null, array $symbols = [])
    {
        return $this->number_formatter->format($number, $pattern, $symbols);
    }

    /**
     * Formats a variable-length lists of things using {@link $list_formatter}.
     *
     * @param array $list The list to format.
     * @param array|string $list_patterns_or_type A list patterns or a list patterns type (one
     * of `LocalizedListFormatter::TYPE_*`).
     *
     * @return string
     */
    public function format_list(array $list, $list_patterns_or_type = LocalizedListFormatter::TYPE_STANDARD)
    {
        return $this->list_formatter->format($list, $list_patterns_or_type);
    }

    /**
     * Transforms a string depending on the context and the locale rules.
     *
     * @param string $str
     * @param string $usage One of `ContextTransforms::USAGE_*`
     * @param string $type One of `ContextTransforms::TYPE_*`
     *
     * @return string
     */
    public function context_transform($str, $usage, $type)
    {
        return $this->context_transforms->transform($str, $usage, $type);
    }
}
ICanBoogie/CLDR v1.6.0 API documentation generated by ApiGen