PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.0.2
WP Popular Posts v5.0.2
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Moment / MomentLocale.php
wordpress-popular-posts / src / Moment Last commit date
CustomFormats 6 years ago Locales 6 years ago FormatsInterface.php 6 years ago LICENSE.md 6 years ago Moment.php 6 years ago MomentException.php 6 years ago MomentFromVo.php 6 years ago MomentHelper.php 6 years ago MomentLocale.php 6 years ago MomentPeriodVo.php 6 years ago
MomentLocale.php
228 lines
1 <?php
2
3 namespace WordPressPopularPosts\Moment;
4
5 /**
6 * MomentLocale
7 * @package Moment
8 * @author Tino Ehrich (tino@bigpun.me)
9 */
10 class MomentLocale
11 {
12 /**
13 * @var Moment
14 */
15 private static $moment;
16
17 /**
18 * @var string
19 */
20 private static $locale = 'en_GB';
21
22 /**
23 * @var boolean
24 */
25 private static $findSimilar = false;
26
27 /**
28 * @var array
29 */
30 private static $localeContent = array();
31
32 /**
33 * @param Moment $moment
34 */
35 public static function setMoment(Moment $moment)
36 {
37 self::$moment = $moment;
38 }
39
40 /**
41 * @param string $locale
42 * @param bool $findSimilar
43 *
44 * @return void
45 * @throws MomentException
46 */
47 public static function setLocale($locale, $findSimilar = false)
48 {
49 self::$locale = $locale;
50 self::$findSimilar = $findSimilar;
51 self::loadLocaleContent();
52 }
53
54 /**
55 * @return void
56 * @throws MomentException
57 */
58 public static function loadLocaleContent()
59 {
60 $pathFile = self::findLocaleFile();
61
62 if (!$pathFile)
63 {
64 throw new MomentException('Locale does not exist: ' . $pathFile);
65 }
66
67 /** @noinspection PhpIncludeInspection */
68 self::$localeContent = require $pathFile;
69 }
70
71 /**
72 * @return array
73 */
74 public static function getLocaleContent()
75 {
76 return self::$localeContent;
77 }
78
79 /**
80 * @param array $keys
81 *
82 * @return array|string|\Closure
83 * @throws MomentException
84 */
85 public static function getLocaleString(array $keys)
86 {
87 $string = self::$localeContent;
88
89 foreach ($keys as $key)
90 {
91 if (isset($string[$key]) === false)
92 {
93 if ($key == 'monthsNominative' && isset($string['months']))
94 {
95 $string = $string['months'];
96 continue;
97 }
98
99 throw new MomentException('Locale string does not exist for key: ' . join(' > ', $keys));
100 }
101
102 $string = $string[$key];
103 }
104
105 return $string;
106 }
107
108 /**
109 * @param array $localeKeys
110 * @param array $formatArgs
111 *
112 * @return string
113 * @throws MomentException
114 */
115 public static function renderLocaleString(array $localeKeys, array $formatArgs = array())
116 {
117 // get locale handler
118 $localeString = self::getLocaleString($localeKeys);
119
120 // handle callback
121 if ($localeString instanceof \Closure)
122 {
123 $localeString = call_user_func_array($localeString, $formatArgs);
124 }
125
126 return vsprintf($localeString, $formatArgs);
127 }
128
129 /**
130 * @param string $format
131 *
132 * @return string
133 */
134 public static function prepareSpecialLocaleTags($format)
135 {
136 $placeholders = array(
137 // months
138 '(?<!\\\)F' => 'n__0001',
139 '(?<!\\\)M' => 'n__0002',
140 '(?<!\\\)f' => 'n__0005',
141 // weekdays
142 '(?<!\\\)l' => 'N__0003',
143 '(?<!\\\)D' => 'N__0004',
144 );
145
146 foreach ($placeholders as $regexp => $tag)
147 {
148 $format = preg_replace('/' . $regexp . '/u', $tag, $format);
149 }
150
151 return $format;
152 }
153
154 /**
155 * @param string $format
156 *
157 * @return string
158 * @throws MomentException
159 */
160 public static function renderSpecialLocaleTags($format)
161 {
162 $placeholders = array(
163 // months
164 '\d{1,2}__0001' => 'months',
165 '\d{1,2}__0002' => 'monthsShort',
166 '\d{1,2}__0005' => 'monthsNominative',
167 // weekdays
168 '\d__0003' => 'weekdays',
169 '\d__0004' => 'weekdaysShort',
170 );
171
172 foreach ($placeholders as $regexp => $tag)
173 {
174 preg_match_all('/(' . $regexp . ')/', $format, $match);
175
176 if (isset($match[1]))
177 {
178 foreach ($match[1] as $date)
179 {
180 list($localeIndex, $type) = explode('__', $date);
181 $localeString = self::renderLocaleString(array($tag, --$localeIndex));
182 $format = preg_replace('/' . $date . '/u', $localeString, $format);
183 }
184 }
185 }
186
187 return $format;
188 }
189
190 /**
191 * @return null|string
192 */
193 private static function findLocaleFile()
194 {
195 $basePathFile = __DIR__ . '/Locales/' . self::$locale;
196 $pathFile = $basePathFile . '.php';
197
198 if (file_exists($pathFile) === false)
199 {
200 $pathFile = null;
201
202 if (self::$findSimilar && $similarLocales = self::fetchSimilarLocales($basePathFile))
203 {
204 $pathFile = $similarLocales[0];
205 }
206 }
207
208 return $pathFile;
209 }
210
211 /**
212 * @param string $path
213 *
214 * @return null|array
215 */
216 private static function fetchSimilarLocales($path)
217 {
218 $locales = glob($path . '*.php');
219
220 if ($locales && !empty($locales))
221 {
222 return $locales;
223 }
224
225 return null;
226 }
227 }
228