4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\ResourceBundle\Reader
;
14 use Symfony\Component\Intl\ResourceBundle\Util\RecursiveArrayAccess
;
17 * A structured reader wrapping an existing resource bundle reader.
19 * @author Bernhard Schussek <bschussek@gmail.com>
21 * @see StructuredResourceBundleBundleReaderInterface
23 class StructuredBundleReader
implements StructuredBundleReaderInterface
26 * @var BundleReaderInterface
31 * Creates an entry reader based on the given resource bundle reader.
33 * @param BundleReaderInterface $reader A resource bundle reader to use.
35 public function __construct(BundleReaderInterface
$reader)
37 $this->reader
= $reader;
43 public function read($path, $locale)
45 return $this->reader
->read($path, $locale);
51 public function getLocales($path)
53 return $this->reader
->getLocales($path);
59 public function readEntry($path, $locale, array $indices, $fallback = true)
61 $data = $this->reader
->read($path, $locale);
63 $entry = RecursiveArrayAccess
::get($data, $indices);
64 $multivalued = is_array($entry) || $entry instanceof \Traversable
;
66 if (!($fallback && (null === $entry || $multivalued))) {
70 if (null !== ($fallbackLocale = $this->getFallbackLocale($locale))) {
71 $parentEntry = $this->readEntry($path, $fallbackLocale, $indices, true);
73 if ($entry || $parentEntry) {
74 $multivalued = $multivalued || is_array($parentEntry) || $parentEntry instanceof \Traversable
;
77 if ($entry instanceof \Traversable
) {
78 $entry = iterator_to_array($entry);
81 if ($parentEntry instanceof \Traversable
) {
82 $parentEntry = iterator_to_array($parentEntry);
86 $parentEntry ?: array(),
90 $entry = null === $entry ? $parentEntry : $entry;
99 * Returns the fallback locale for a given locale, if any
101 * @param string $locale The locale to find the fallback for.
103 * @return string|null The fallback locale, or null if no parent exists
105 private function getFallbackLocale($locale)
107 if (false === $pos = strrpos($locale, '_')) {
111 return substr($locale, 0, $pos);