ICanBoogie/CLDR 2.0.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • CLDR
      • Plurals
      • Provider
        • WebProvider
      • Units

Classes

  • PathMapper
  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 
<?php

namespace ICanBoogie\CLDR\Provider\WebProvider;

/**
 * Maps CLDR paths to GitHub URLs.
 */
class PathMapper
{
    const DEFAULT_ORIGIN = "https://raw.githubusercontent.com/unicode-cldr/";
    const DEFAULT_VERSION = "29.0.0";
    const DEFAULT_VARIATION = self::PREFER_MODERN;

    const PREFER_MODERN = 'modern';
    const PREFER_FULL = 'full';

    static private $compact_files = [

        'dates' => 'dateFields timeZoneNames',
        'localenames' => 'languages localeDisplayNames scripts territories transformNames variants',
        'numbers' => 'currencies numbers',
        'misc' => 'characters contextTransforms delimiters layout listPatterns posix',
        'units' => 'measurementSystemNames units',
        'segments' => 'suppressions'

    ];

    /**
     * @var string
     */
    private $origin;

    /**
     * @var string
     */
    private $version;

    /**
     * @var string
     */
    private $variation;

    /**
     * @var array
     */
    private $files;

    /**
     * @param string $origin
     * @param string $version
     * @param string $variation
     */
    public function __construct(
        $origin = self::DEFAULT_ORIGIN,
        $version = self::DEFAULT_VERSION,
        $variation = self::DEFAULT_VARIATION
    ) {
        $this->origin = $origin;
        $this->version = $version;
        $this->variation = $variation;
        $this->files = $this->compile_files(self::$compact_files);
    }

    /**
     * @param string $path
     *
     * @return string
     */
    public function map($path)
    {
        $parts = explode('/', $path);

        switch ($parts[0])
        {
            case 'main':
            case 'segments':
                $parts = $this->map_localized($parts);
                break;

            default:
                array_unshift($parts, 'core');
        }

        $p0 = array_shift($parts);
        array_unshift($parts, $p0, $this->version);

        $path = implode('/', $parts);

        return "{$this->origin}cldr-$path.json";
    }

    /**
     * @param array $parts
     *
     * @return array
     */
    private function map_localized(array $parts)
    {
        $file = $parts[2];

        if (strpos($file, 'ca-') === 0)
        {
            $calendar = substr($file, 3);
            $repository = "cal-{$calendar}";

            if ($calendar === 'generic' || $calendar === 'gregorian')
            {
                $repository = "dates";
            }
        }
        else
        {
            $repository = $this->files[$file];
        }

        array_unshift($parts, "$repository-{$this->variation}");

        return $parts;
    }

    /**
     * @param array $compact_files
     *
     * @return array
     */
    private function compile_files(array $compact_files)
    {
        $compiled = [];

        foreach ($compact_files as $name => $files)
        {
            $compiled += array_fill_keys(explode(' ', $files), $name);
        }

        return $compiled;
    }
}
ICanBoogie/CLDR 2.0.x API documentation generated by ApiGen