Completed
Pull Request — develop (#55)
by
unknown
01:46
created

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