Classes List

Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader

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

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\Intl\Data\Bundle\Reader;

use Symfony\Component\Intl\Exception\MissingResourceException;
use Symfony\Component\Intl\Exception\OutOfBoundsException;
use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
use Symfony\Component\Intl\Locale;
use Symfony\Component\Intl\Data\Util\RecursiveArrayAccess;

/** * Default implementation of {@link BundleEntryReaderInterface}. * * @author Bernhard Schussek <bschussek@gmail.com> * * @see BundleEntryReaderInterface * * @internal */
class BundleEntryReader implements BundleEntryReaderInterface
{
    private $reader;

    /** * A mapping of locale aliases to locales. */
    private $localeAliases = array();

    /** * Creates an entry reader based on the given resource bundle reader. */
    public function __construct(BundleReaderInterface $reader)
    {
        $this->reader = $reader;
    }

    /** * Stores a mapping of locale aliases to locales. * * This mapping is used when reading entries and merging them with their * fallback locales. If an entry is read for a locale alias (e.g. "mo") * that points to a locale with a fallback locale ("ro_MD"), the reader * can continue at the correct fallback locale ("ro"). * * @param array $localeAliases A mapping of locale aliases to locales */
    public function setLocaleAliases($localeAliases)
    {
        $this->localeAliases = $localeAliases;
    }

    /** * {@inheritdoc} */
    public function read($path$locale)
    {
        return $this->reader->read($path$locale);
    }

    /** * {@inheritdoc} */
    public function readEntry($path$localearray $indices$fallback = true)
    {
        $entry = null;
        $isMultiValued = false;
        $readSucceeded = false;
        $exception = null;
        $currentLocale = $locale;
        $testedLocales = array();

        while (null !== $currentLocale) {
            // Resolve any aliases to their target locales
            if (isset($this->localeAliases[$currentLocale])) {
                $currentLocale = $this->localeAliases[$currentLocale];
            }

            try {
                $data = $this->reader->read($path$currentLocale);
                $currentEntry = RecursiveArrayAccess::get($data$indices);
                $readSucceeded = true;

                $isCurrentTraversable = $currentEntry instanceof \Traversable;
                $isCurrentMultiValued = $isCurrentTraversable || is_array($currentEntry);

                // Return immediately if fallback is disabled or we are dealing
                // with a scalar non-null entry
                if (!$fallback || (!$isCurrentMultiValued && null !== $currentEntry)) {
                    return $currentEntry;
                }

                // =========================================================
                // Fallback is enabled, entry is either multi-valued or NULL
                // =========================================================

                // If entry is multi-valued, convert to array
                if ($isCurrentTraversable) {
                    $currentEntry = iterator_to_array($currentEntry);
                }

                // If previously read entry was multi-valued too, merge them
                if ($isCurrentMultiValued && $isMultiValued) {
                    $currentEntry = array_merge($currentEntry$entry);
                }

                // Keep the previous entry if the current entry is NULL
                if (null !== $currentEntry) {
                    $entry = $currentEntry;
                }

                // If this or the previous entry was multi-valued, we are dealing
                // with a merged, multi-valued entry now
                $isMultiValued = $isMultiValued || $isCurrentMultiValued;
            } catch (ResourceBundleNotFoundException $e) {
                // Continue if there is a fallback locale for the current
                // locale
                $exception = $e;
            } catch (OutOfBoundsException $e) {
                // Remember exception and rethrow if we cannot find anything in
                // the fallback locales either
                $exception = $e;
            }

            // Remember which locales we tried
            $testedLocales[] = $currentLocale;

            // Check whether fallback is allowed
            if (!$fallback) {
                break;
            }

            // Then determine fallback locale
            $currentLocale = Locale::getFallback($currentLocale);
        }

        // Multi-valued entry was merged
        if ($isMultiValued) {
            return $entry;
        }

        // Entry is still NULL, but no read error occurred
        if ($readSucceeded) {
            return $entry;
        }

        // Entry is still NULL, read error occurred. Throw an exception
        // containing the detailed path and locale
        $errorMessage = sprintf(
            'Couldn\'t read the indices [%s] for the locale "%s" in "%s".',
            implode(']['$indices),
            $locale,
            $path
        );

        // Append fallback locales, if any
        if (count($testedLocales) > 1) {
            // Remove original locale
            array_shift($testedLocales);

            $errorMessage .= sprintf(
                ' The indices also couldn\'t be found for the fallback locale(s) "%s".',
                implode('", "'$testedLocales)
            );
        }

        throw new MissingResourceException($errorMessage0$exception);
    }
}