Completed
Push — develop ( 4e8449...27824b )
by Abdelrahman
01:30
created

Country::getNativeName()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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