]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/intl/Symfony/Component/Intl/ResourceBundle/Reader/StructuredBundleReader.php
e3656fe2ebdf6dbdcc8d40ad643173b75974ab73
[github/wallabag/wallabag.git] / vendor / symfony / intl / Symfony / Component / Intl / ResourceBundle / Reader / StructuredBundleReader.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Intl\ResourceBundle\Reader;
13
14 use Symfony\Component\Intl\ResourceBundle\Util\RecursiveArrayAccess;
15
16 /**
17 * A structured reader wrapping an existing resource bundle reader.
18 *
19 * @author Bernhard Schussek <bschussek@gmail.com>
20 *
21 * @see StructuredResourceBundleBundleReaderInterface
22 */
23 class StructuredBundleReader implements StructuredBundleReaderInterface
24 {
25 /**
26 * @var BundleReaderInterface
27 */
28 private $reader;
29
30 /**
31 * Creates an entry reader based on the given resource bundle reader.
32 *
33 * @param BundleReaderInterface $reader A resource bundle reader to use.
34 */
35 public function __construct(BundleReaderInterface $reader)
36 {
37 $this->reader = $reader;
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function read($path, $locale)
44 {
45 return $this->reader->read($path, $locale);
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 public function getLocales($path)
52 {
53 return $this->reader->getLocales($path);
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function readEntry($path, $locale, array $indices, $fallback = true)
60 {
61 $data = $this->reader->read($path, $locale);
62
63 $entry = RecursiveArrayAccess::get($data, $indices);
64 $multivalued = is_array($entry) || $entry instanceof \Traversable;
65
66 if (!($fallback && (null === $entry || $multivalued))) {
67 return $entry;
68 }
69
70 if (null !== ($fallbackLocale = $this->getFallbackLocale($locale))) {
71 $parentEntry = $this->readEntry($path, $fallbackLocale, $indices, true);
72
73 if ($entry || $parentEntry) {
74 $multivalued = $multivalued || is_array($parentEntry) || $parentEntry instanceof \Traversable;
75
76 if ($multivalued) {
77 if ($entry instanceof \Traversable) {
78 $entry = iterator_to_array($entry);
79 }
80
81 if ($parentEntry instanceof \Traversable) {
82 $parentEntry = iterator_to_array($parentEntry);
83 }
84
85 $entry = array_merge(
86 $parentEntry ?: array(),
87 $entry ?: array()
88 );
89 } else {
90 $entry = null === $entry ? $parentEntry : $entry;
91 }
92 }
93 }
94
95 return $entry;
96 }
97
98 /**
99 * Returns the fallback locale for a given locale, if any
100 *
101 * @param string $locale The locale to find the fallback for.
102 *
103 * @return string|null The fallback locale, or null if no parent exists
104 */
105 private function getFallbackLocale($locale)
106 {
107 if (false === $pos = strrpos($locale, '_')) {
108 return null;
109 }
110
111 return substr($locale, 0, $pos);
112 }
113 }