Hoa central
Locale.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Locale as LUT;
40 use Hoa\Test;
41 
50 class Locale extends Test\Unit\Suite
51 {
52  public function case_no_default_no_locale()
53  {
54  $this
55  ->exception(function () {
56  new LUT();
57  })
58  ->isInstanceOf('Hoa\Locale\Exception');
59  }
60 
61  public function case_no_locale()
62  {
63  $this
64  ->given(
65  LUT::setDefault('fr-FR'),
66  $locale = new LUT()
67  )
68  ->when(
69  $localizer = $locale->getLocalizer(),
70  $language = $locale->getLanguage(),
71  $region = $locale->getRegion()
72  )
73  ->then
74  ->object($localizer)
75  ->isInstanceOf('Hoa\Locale\Localizer\Coerce')
76  ->variable($localizer->getLocale())
77  ->isNull()
78  ->string($language)
79  ->isEqualTo('fr')
80  ->string($region)
81  ->isEqualTo('FR');
82  }
83 
84  public function case_locale_autoBoxing()
85  {
86  $this
87  ->given($locale = new LUT('fr-FR'))
88  ->when($localizer = $locale->getLocalizer())
89  ->then
90  ->object($localizer)
91  ->isInstanceOf('Hoa\Locale\Localizer\Coerce');
92  }
93 
94  public function case_invalid_locale()
95  {
96  $this
97  ->exception(function () {
98  new LUT('fr_FR');
99  })
100  ->isInstanceOf('Hoa\Locale\Exception');
101  }
102 
103  public function case_type_privateUse()
104  {
105  $this
106  ->given(
107  $regex = $this->realdom->regex(
108  '/x\-[a-z0-9]{1,8}/'
109  )
110  )
111  ->when(function () use ($regex) {
112  foreach ($this->sampleMany($regex, 1000) as $datum) {
113  $this
114  ->given($locale = new LUT($datum))
115  ->when(
116  $type = $locale->getType(),
117  $privateUse = $locale->getPrivateUse()
118  )
119  ->then
120  ->integer($type)
121  ->isEqualTo(LUT::TYPE_PRIVATEUSE)
122  ->string('x-' . $privateUse)
123  ->isEqualTo($datum);
124  }
125  });
126  }
127 
128  public function case_type_grandfathered()
129  {
130  $this
131  ->given(
132  $regex = $this->realdom->regex(
133  // Modified regular expression,
134  // to avoid conflict with TYPE_LANGTAG
135  '/[a-wyz](\-[a-z0-9]{2,8}){1,2}/'
136  )
137  )
138  ->when(function () use ($regex) {
139  foreach ($this->sampleMany($regex, 1000) as $datum) {
140  $this
141  ->given($locale = new LUT($datum))
142  ->when(
143  $type = $locale->getType(),
144  $grandFathered = $locale->getGrandfathered()
145  )
146  ->then
147  ->integer($type)
148  ->isEqualTo(LUT::TYPE_GRANDFATHERED)
149  ->string($grandFathered)
150  ->isEqualTo($datum);
151  }
152  });
153  }
154 
155  public function case_type_langtag()
156  {
157  $this
158  ->given(
159  $regex = $this->realdom->regex(
160  '/' .
161  '[a-z]{2,3}' .
162  '(\-[a-z]{4})?' .
163  '(\-(?:[a-z]{2}|[0-9]{4}))?' .
164  '((?:\-(?:[a-z]{2}|[0-9]{3}))+)?' .
165  '((?:\-(?:[a-wy-z]|\d)\-[a-z0-9]{2,8})+)?' .
166  '(\-x\-[a-z0-9]{1,8})?' .
167  '/'
168  )
169  )
170  ->when(function () use ($regex) {
171  foreach ($this->sampleMany($regex, 1000) as $datum) {
172  $this
173  ->given($locale = new LUT($datum))
174  ->when($type = $locale->getType())
175  ->then
176  ->integer($type)
177  ->isEqualTo(LUT::TYPE_LANGTAG);
178  }
179  });
180  }
181 
182  public function case_langtag_exploded()
183  {
184  $this
185  ->given($locale = 'zh-Hant-TW-xy-ab-123-f-oo-4-42-x-qux')
186  ->when($result = new LUT($locale))
187  ->then
188  ->string($result->getLanguage())
189  ->isEqualTo('zh')
190  ->string($result->getScript())
191  ->isEqualTo('Hant')
192  ->string($result->getRegion())
193  ->isEqualTo('TW')
194  ->array($result->getVariants())
195  ->isEqualTo(['xy', 'ab', '123'])
196  ->array($result->getExtensions())
197  ->isEqualTo(['f' => 'oo', 4 => '42'])
198  ->string($result->getPrivateUse())
199  ->isEqualTo('qux');
200  }
201 
202  public function case_langtag_default()
203  {
204  $this
205  ->given($locale = 'fr')
206  ->when($result = new LUT($locale))
207  ->then
208  ->string($result->getLanguage())
209  ->isEqualTo('fr')
210  ->variable($result->getScript())
211  ->isNull()
212  ->variable($result->getRegion())
213  ->isNull()
214  ->array($result->getVariants())
215  ->isEmpty()
216  ->array($result->getExtensions())
217  ->isEmpty()
218  ->variable($result->getPrivateUse())
219  ->isNull();
220  }
221 
222  public function case_reset()
223  {
224  $this
225  ->given(
226  $localizer = new LUT\Localizer\Coerce(
227  'zh-Hant-TW-xy-ab-123-f-oo-4-42-x-qux'
228  )
229  )
230  ->when($locale = new LUT($localizer))
231  ->then
232  ->string($locale->getLanguage())
233  ->isEqualTo('zh')
234  ->string($locale->getScript())
235  ->isEqualTo('Hant')
236  ->string($locale->getRegion())
237  ->isEqualTo('TW')
238  ->array($locale->getVariants())
239  ->isEqualTo(['xy', 'ab', '123'])
240  ->array($locale->getExtensions())
241  ->isEqualTo(['f' => 'oo', 4 => '42'])
242  ->string($locale->getPrivateUse())
243  ->isEqualTo('qux')
244 
245  ->given($localizer = new LUT\Localizer\Coerce('fr'))
246  ->when($locale->setLocalizer($localizer))
247  ->then
248  ->string($locale->getLanguage())
249  ->isEqualTo('fr')
250  ->variable($locale->getScript())
251  ->isNull()
252  ->variable($locale->getRegion())
253  ->isNull()
254  ->array($locale->getVariants())
255  ->isEmpty()
256  ->array($locale->getExtensions())
257  ->isEmpty()
258  ->variable($locale->getPrivateUse())
259  ->isNull();
260  }
261 }