Completed
Pull Request — master (#66)
by Romain
02:21
created

Country::getWmo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Country Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Country Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
declare(strict_types=1);
17
18
namespace Rinvex\Country;
19
20
use DateTimeZone;
21
use Exception;
22
use Locale;
23
use ResourceBundle;
24
25
class Country
26
{
27
    /**
28
     * The attributes array.
29
     *
30
     * @var array
31
     */
32
    protected $attributes;
33
34
    /**
35
     * Create a new Country instance.
36
     *
37
     * @param array $attributes
38
     *
39
     * @throws \Exception
40
     */
41
    public function __construct($attributes)
42
    {
43
        // Set the attributes
44
        $this->setAttributes($attributes);
45
46
        // Check required mandatory attributes
47
        if (empty($this->getName()) || empty($this->getOfficialName())
48
            || empty($this->getNativeName()) || empty($this->getNativeOfficialName())
49
            || empty($this->getIsoAlpha2()) || empty($this->getIsoAlpha3())) {
50
            throw new Exception('Missing mandatory country attributes!');
51
        }
52
    }
53
54
    /**
55
     * Set the attributes.
56
     *
57
     * @param array $attributes
58
     *
59
     * @return $this
60
     */
61
    public function setAttributes($attributes)
62
    {
63
        $this->attributes = $attributes;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Get the attributes.
70
     *
71
     * @return array|null
72
     */
73
    public function getAttributes()
74
    {
75
        return $this->attributes;
76
    }
77
78
    /**
79
     * Set single attribute.
80
     *
81
     * @param string $key
82
     * @param mixed  $value
83
     *
84
     * @return $this
85
     */
86
    public function set($key, $value)
87
    {
88
        $this->attributes[$key] = $value;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Get an item from attributes array using "dot" notation.
95
     *
96
     * @param string $key
97
     * @param mixed  $default
98
     *
99
     * @return mixed
100
     */
101
    public function get($key, $default = null)
102
    {
103
        $array = $this->attributes;
104
105
        if (is_null($key)) {
106
            return $array;
107
        }
108
109
        if (array_key_exists($key, $array)) {
110
            return $array[$key];
111
        }
112
113
        foreach (explode('.', $key) as $segment) {
114
            if (is_array($array) && array_key_exists($segment, $array)) {
115
                $array = $array[$segment];
116
            } else {
117
                return $default;
118
            }
119
        }
120
121
        return $array;
122
    }
123
124
    /**
125
     * Get the common name.
126
     *
127
     * @return string|null
128
     */
129
    public function getName()
130
    {
131
        return $this->get('name.common') ?: $this->get('name');
132
    }
133
134
    /**
135
     * Get the official name.
136
     *
137
     * @return string|null
138
     */
139
    public function getOfficialName()
140
    {
141
        return $this->get('name.official') ?: $this->get('official_name');
142
    }
143
144
    /**
145
     * Get the given native name or fallback to first native name.
146
     *
147
     * @param string|null $language
148
     *
149
     * @return string|null
150
     */
151
    public function getNativeName($language = null)
152
    {
153
        $language = $language ? mb_strtolower($language) : null;
154
155
        return $this->get("name.native.{$language}.common")
156
            ?: (current($this->get('name.native', []))['common'] ?: $this->get('native_name'));
157
    }
158
159
    /**
160
     * Get the given native official name or fallback to first native official name.
161
     *
162
     * @param string|null $language
163
     *
164
     * @return string|null
165
     */
166
    public function getNativeOfficialName($language = null)
167
    {
168
        $language = $language ? mb_strtolower($language) : null;
169
170
        return $this->get("name.native.{$language}.official")
171
            ?: (current($this->get('name.native', []))['official'] ?: $this->get('native_official_name'));
172
    }
173
174
    /**
175
     * Get the native names.
176
     *
177
     * @return array|null
178
     */
179
    public function getNativeNames()
180
    {
181
        return $this->get('name.native');
182
    }
183
184
    /**
185
     * Get the demonym.
186
     *
187
     * @return string|null
188
     */
189
    public function getDemonym()
190
    {
191
        return $this->get('demonym');
192
    }
193
194
    /**
195
     * Get the capital.
196
     *
197
     * @return string|null
198
     */
199
    public function getCapital()
200
    {
201
        return $this->get('capital');
202
    }
203
204
    /**
205
     * Get the ISO 3166-1 alpha2.
206
     *
207
     * @return string|null
208
     */
209
    public function getIsoAlpha2()
210
    {
211
        return $this->get('iso_3166_1_alpha2');
212
    }
213
214
    /**
215
     * Get the ISO 3166-1 alpha3.
216
     *
217
     * @return string|null
218
     */
219
    public function getIsoAlpha3()
220
    {
221
        return $this->get('iso_3166_1_alpha3');
222
    }
223
224
    /**
225
     * Get the ISO 3166-1 numeric.
226
     *
227
     * @return string|null
228
     */
229
    public function getIsoNumeric()
230
    {
231
        return $this->get('iso_3166_1_numeric');
232
    }
233
234
    /**
235
     * Get the given currency or fallback to first currency.
236
     *
237
     * @param string|null $currency
238
     *
239
     * @return string|null
240
     */
241
    public function getCurrency($currency = null)
242
    {
243
        $currency = $currency ? mb_strtoupper($currency) : null;
244
245
        return $this->get("currency.{$currency}") ?: (current($this->get('currency', [])) ?: null);
246
    }
247
248
    /**
249
     * Get the currencies.
250
     *
251
     * @return array|null
252
     */
253
    public function getCurrencies()
254
    {
255
        return $this->get('currency');
256
    }
257
258
    /**
259
     * Get the TLD.
260
     *
261
     * @return string|null
262
     */
263
    public function getTld()
264
    {
265
        return current($this->get('tld', [])) ?: null;
266
    }
267
268
    /**
269
     * Get the TLDs.
270
     *
271
     * @return array|null
272
     */
273
    public function getTlds()
274
    {
275
        return $this->get('tld');
276
    }
277
278
    /**
279
     * Get the alternative spellings.
280
     *
281
     * @return array|null
282
     */
283
    public function getAltSpellings()
284
    {
285
        return $this->get('alt_spellings');
286
    }
287
288
    /**
289
     * Get the given language or fallback to first language.
290
     *
291
     * @param string|null $language
292
     *
293
     * @return string|null
294
     */
295
    public function getLanguage($language = null)
296
    {
297
        $language = $language ? mb_strtoupper($language) : null;
298
299
        return $this->get("languages.{$language}") ?: (current($this->get('languages', [])) ?: null);
300
    }
301
302
    /**
303
     * Get the languages.
304
     *
305
     * @return array|null
306
     */
307
    public function getLanguages()
308
    {
309
        return $this->get('languages');
310
    }
311
312
    /**
313
     * Get the translations.
314
     *
315
     * @return array
316
     */
317
    public function getTranslations()
318
    {
319
        // Get english name
320
        $name = [
321
            'eng' => [
322
                'common' => $this->getName(),
323
                'official' => $this->getOfficialName(),
324
            ],
325
        ];
326
327
        // Get native names
328
        $natives = $this->getNativeNames() ?: [];
329
330
        // Get other translations
331
        $file = __DIR__.'/../resources/translations/'.mb_strtolower($this->getIsoAlpha2()).'.json';
332
        $translations = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
333
334
        // Merge all names together
335
        $result = array_merge($translations, $natives, $name);
336
337
        // Sort alphabetically
338
        ksort($result);
339
340
        return $result;
341
    }
342
343
    /**
344
     * Get the translation.
345
     *
346
     * @param string|null $language
347
     *
348
     * @return array
349
     */
350
    public function getTranslation($language = null)
351
    {
352
        return isset($this->getTranslations()[$language])
353
            ? $this->getTranslations()[$language] : current($this->getTranslations());
354
    }
355
356
    /**
357
     * Get the geodata.
358
     *
359
     * @return array|null
360
     */
361
    public function getGeodata()
362
    {
363
        return $this->get('geo');
364
    }
365
366
    /**
367
     * Get the continent.
368
     *
369
     * @return string|null
370
     */
371
    public function getContinent()
372
    {
373
        return current($this->get('geo.continent', [])) ?: null;
374
    }
375
376
    /**
377
     * Determine whether the country uses postal code.
378
     *
379
     * @return bool|null
380
     */
381
    public function usesPostalCode()
382
    {
383
        return $this->get('geo.postal_code');
384
    }
385
386
    /**
387
     * Get the latitude.
388
     *
389
     * @return string|null
390
     */
391
    public function getLatitude()
392
    {
393
        return $this->get('geo.latitude');
394
    }
395
396
    /**
397
     * Get the longitude.
398
     *
399
     * @return string|null
400
     */
401
    public function getLongitude()
402
    {
403
        return $this->get('geo.longitude');
404
    }
405
406
    /**
407
     * Get the described latitude.
408
     *
409
     * @return string|null
410
     */
411
    public function getLatitudeDesc()
412
    {
413
        return $this->get('geo.latitude_desc');
414
    }
415
416
    /**
417
     * Get the described longitude.
418
     *
419
     * @return string|null
420
     */
421
    public function getLongitudeDesc()
422
    {
423
        return $this->get('geo.longitude_desc');
424
    }
425
426
    /**
427
     * Get the maximum latitude.
428
     *
429
     * @return string|null
430
     */
431
    public function getMaxLatitude()
432
    {
433
        return $this->get('geo.max_latitude');
434
    }
435
436
    /**
437
     * Get the maximum longitude.
438
     *
439
     * @return string|null
440
     */
441
    public function getMaxLongitude()
442
    {
443
        return $this->get('geo.max_longitude');
444
    }
445
446
    /**
447
     * Get the minimum latitude.
448
     *
449
     * @return string|null
450
     */
451
    public function getMinLatitude()
452
    {
453
        return $this->get('geo.min_latitude');
454
    }
455
456
    /**
457
     * Get the minimum longitude.
458
     *
459
     * @return string|null
460
     */
461
    public function getMinLongitude()
462
    {
463
        return $this->get('geo.min_longitude');
464
    }
465
466
    /**
467
     * Get the area.
468
     *
469
     * @return int|null
470
     */
471
    public function getArea()
472
    {
473
        return $this->get('geo.area');
474
    }
475
476
    /**
477
     * Get the region.
478
     *
479
     * @return string|null
480
     */
481
    public function getRegion()
482
    {
483
        return $this->get('geo.region');
484
    }
485
486
    /**
487
     * Get the subregion.
488
     *
489
     * @return string|null
490
     */
491
    public function getSubregion()
492
    {
493
        return $this->get('geo.subregion');
494
    }
495
496
    /**
497
     * Get the world region.
498
     *
499
     * @return string|null
500
     */
501
    public function getWorldRegion()
502
    {
503
        return $this->get('geo.world_region');
504
    }
505
506
    /**
507
     * Get the region code.
508
     *
509
     * @return string|null
510
     */
511
    public function getRegionCode()
512
    {
513
        return $this->get('geo.region_code');
514
    }
515
516
    /**
517
     * Get the subregion code.
518
     *
519
     * @return string|null
520
     */
521
    public function getSubregionCode()
522
    {
523
        return $this->get('geo.subregion_code');
524
    }
525
526
    /**
527
     * Check the landlock status.
528
     *
529
     * @return bool|null
530
     */
531
    public function isLandlocked()
532
    {
533
        return $this->get('geo.landlocked');
534
    }
535
536
    /**
537
     * Get the borders.
538
     *
539
     * @return array|null
540
     */
541
    public function getBorders()
542
    {
543
        return $this->get('geo.borders');
544
    }
545
546
    /**
547
     * Determine whether the country is independent.
548
     *
549
     * @return string|null
550
     */
551
    public function isIndependent()
552
    {
553
        return $this->get('geo.independent');
554
    }
555
556
    /**
557
     * Get the given calling code or fallback to first calling code.
558
     *
559
     * @return string|null
560
     */
561
    public function getCallingCode()
562
    {
563
        return current($this->get('dialling.calling_code', [])) ?: (current($this->get('calling_code', [])) ?: null);
564
    }
565
566
    /**
567
     * Get the calling codes.
568
     *
569
     * @return array|null
570
     */
571
    public function getCallingCodes()
572
    {
573
        return $this->get('dialling.calling_code');
574
    }
575
576
    /**
577
     * Get the national prefix.
578
     *
579
     * @return string|null
580
     */
581
    public function getNationalPrefix()
582
    {
583
        return $this->get('dialling.national_prefix');
584
    }
585
586
    /**
587
     * Get the national number length.
588
     *
589
     * @return int|null
590
     */
591
    public function getNationalNumberLength()
592
    {
593
        return current($this->get('dialling.national_number_lengths', [])) ?: null;
594
    }
595
596
    /**
597
     * Get the national number lengths.
598
     *
599
     * @return array|null
600
     */
601
    public function getNationalNumberLengths()
602
    {
603
        return $this->get('dialling.national_number_lengths');
604
    }
605
606
    /**
607
     * Get the national destination code length.
608
     *
609
     * @return int|null
610
     */
611
    public function getNationalDestinationCodeLength()
612
    {
613
        return current($this->get('dialling.national_destination_code_lengths', [])) ?: null;
614
    }
615
616
    /**
617
     * Get the national destination code lengths.
618
     *
619
     * @return array|null
620
     */
621
    public function getnationaldestinationcodelengths()
622
    {
623
        return $this->get('dialling.national_destination_code_lengths');
624
    }
625
626
    /**
627
     * Get the international prefix.
628
     *
629
     * @return string|null
630
     */
631
    public function getInternationalPrefix()
632
    {
633
        return $this->get('dialling.international_prefix');
634
    }
635
636
    /**
637
     * Get the extras.
638
     *
639
     * @return array|null
640
     */
641
    public function getExtra()
642
    {
643
        return $this->get('extra');
644
    }
645
646
    /**
647
     * Get the geonameid.
648
     *
649
     * @return int|null
650
     */
651
    public function getGeonameid()
652
    {
653
        return $this->get('extra.geonameid');
654
    }
655
656
    /**
657
     * Get the edgar code.
658
     *
659
     * @return string|null
660
     */
661
    public function getEdgar()
662
    {
663
        return $this->get('extra.edgar');
664
    }
665
666
    /**
667
     * Get the itu code.
668
     *
669
     * @return string|null
670
     */
671
    public function getItu()
672
    {
673
        return $this->get('extra.itu');
674
    }
675
676
    /**
677
     * Get the marc code.
678
     *
679
     * @return string|null
680
     */
681
    public function getMarc()
682
    {
683
        return $this->get('extra.marc');
684
    }
685
686
    /**
687
     * Get the wmo code.
688
     *
689
     * @return string|null
690
     */
691
    public function getWmo()
692
    {
693
        return $this->get('extra.wmo');
694
    }
695
696
    /**
697
     * Get the ds code.
698
     *
699
     * @return string|null
700
     */
701
    public function getDs()
702
    {
703
        return $this->get('extra.ds');
704
    }
705
706
    /**
707
     * Get the fifa code.
708
     *
709
     * @return string|null
710
     */
711
    public function getFifa()
712
    {
713
        return $this->get('extra.fifa');
714
    }
715
716
    /**
717
     * Get the fips code.
718
     *
719
     * @return string|null
720
     */
721
    public function getFips()
722
    {
723
        return $this->get('extra.fips');
724
    }
725
726
    /**
727
     * Get the gaul code.
728
     *
729
     * @return int|null
730
     */
731
    public function getGaul()
732
    {
733
        return $this->get('extra.gaul');
734
    }
735
736
    /**
737
     * Get the ioc code.
738
     *
739
     * @return string|null
740
     */
741
    public function getIoc()
742
    {
743
        return $this->get('extra.ioc');
744
    }
745
746
    /**
747
     * Get the cowc code.
748
     *
749
     * @return string|null
750
     */
751
    public function getCowc()
752
    {
753
        return $this->get('extra.cowc');
754
    }
755
756
    /**
757
     * Get the cown code.
758
     *
759
     * @return int|null
760
     */
761
    public function getCown()
762
    {
763
        return $this->get('extra.cown');
764
    }
765
766
    /**
767
     * Get the fao code.
768
     *
769
     * @return int|null
770
     */
771
    public function getFao()
772
    {
773
        return $this->get('extra.fao');
774
    }
775
776
    /**
777
     * Get the imf code.
778
     *
779
     * @return int|null
780
     */
781
    public function getImf()
782
    {
783
        return $this->get('extra.imf');
784
    }
785
786
    /**
787
     * Get the ar5 code.
788
     *
789
     * @return string|null
790
     */
791
    public function getAr5()
792
    {
793
        return $this->get('extra.ar5');
794
    }
795
796
    /**
797
     * Get the address format.
798
     *
799
     * @return string|null
800
     */
801
    public function getAddressFormat()
802
    {
803
        return $this->get('extra.address_format');
804
    }
805
806
    /**
807
     * Determine whether the country is EU member.
808
     *
809
     * @return bool|null
810
     */
811
    public function isEuMember()
812
    {
813
        return $this->get('extra.eu_member');
814
    }
815
816
    /**
817
     * Get the VAT rates.
818
     *
819
     * @return array|null
820
     */
821
    public function getVatRates()
822
    {
823
        return $this->get('extra.vat_rates');
824
    }
825
826
    /**
827
     * Get the emoji.
828
     *
829
     * @return array|null
830
     */
831
    public function getEmoji()
832
    {
833
        return $this->get('extra.emoji') ?: $this->get('emoji');
834
    }
835
836
    /**
837
     * Get the geographic data structure.
838
     *
839
     * @return string|null
840
     */
841
    public function getGeoJson()
842
    {
843
        if (! ($code = $this->getIsoAlpha2())) {
844
            return;
845
        }
846
847
        return file_exists($file = __DIR__.'/../resources/geodata/'.mb_strtolower($code).'.json') ? file_get_contents($file) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
848
    }
849
850
    /**
851
     * Get the flag.
852
     *
853
     * @return string|null
854
     */
855
    public function getFlag()
856
    {
857
        if (! ($code = $this->getIsoAlpha2())) {
858
            return;
859
        }
860
861
        return file_exists($file = __DIR__.'/../resources/flags/'.mb_strtolower($code).'.svg') ? file_get_contents($file) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
862
    }
863
864
    /**
865
     * Get the divisions.
866
     *
867
     * @return array|null
868
     */
869
    public function getDivisions()
870
    {
871
        if (! ($code = $this->getIsoAlpha2())) {
872
            return;
873
        }
874
875
        return file_exists($file = __DIR__.'/../resources/divisions/'.mb_strtolower($code).'.json') ? json_decode(file_get_contents($file), true) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 153 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
876
    }
877
878
    /**
879
     * Get the divisions.
880
     *
881
     * @param string $division
882
     *
883
     * @return array|null
884
     */
885
    public function getDivision($division)
886
    {
887
        return ! empty($this->getDivisions()) && isset($this->getDivisions()[$division])
888
            ? $this->getDivisions()[$division] : null;
889
    }
890
891
    /**
892
     * Get the timezones.
893
     *
894
     * @return array|null
895
     */
896
    public function getTimezones()
897
    {
898
        if (! ($code = $this->getIsoAlpha2())) {
899
            return;
900
        }
901
902
        return DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $code);
903
    }
904
905
    /**
906
     * Get the locales.
907
     *
908
     * @return array|null
909
     */
910
    public function getLocales()
911
    {
912
        if (! ($code = $this->getIsoAlpha2())) {
913
            return;
914
        }
915
916
        $locales = [];
917
        foreach (ResourceBundle::getLocales('') as $localeCode) {
918
            if ($code === Locale::getRegion($localeCode)) {
919
                $locales[] = $localeCode;
920
            }
921
        }
922
923
        return $locales;
924
    }
925
}
926