Completed
Pull Request — develop (#55)
by
unknown
03:23
created

Country::getDefaultCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
        return $this->getCurrency($this->get("default_currency"));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal default_currency does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
249
    }
250
251
    /**
252
     * Get the TLD.
253
     *
254
     * @return string|null
255
     */
256
    public function getTld()
257
    {
258
        return current($this->get('tld', [])) ?: null;
259
    }
260
261
    /**
262
     * Get the TLDs.
263
     *
264
     * @return array|null
265
     */
266
    public function getTlds()
267
    {
268
        return $this->get('tld');
269
    }
270
271
    /**
272
     * Get the alternative spellings.
273
     *
274
     * @return array|null
275
     */
276
    public function getAltSpellings()
277
    {
278
        return $this->get('alt_spellings');
279
    }
280
281
    /**
282
     * Get the given language or fallback to first language.
283
     *
284
     * @param string|null $languageCode
285
     *
286
     * @return string|null
287
     */
288
    public function getLanguage($languageCode = null)
289
    {
290
        $languageCode = $languageCode ? mb_strtoupper($languageCode) : null;
291
292
        return $this->get("languages.{$languageCode}") ?: (current($this->get('languages', [])) ?: null);
293
    }
294
295
    /**
296
     * Get the languages.
297
     *
298
     * @return array|null
299
     */
300
    public function getLanguages()
301
    {
302
        return $this->get('languages');
303
    }
304
305
    /**
306
     * Get the translations.
307
     *
308
     * @return array
309
     */
310
    public function getTranslations()
311
    {
312
        // Get english name
313
        $name = [
314
            'eng' => [
315
                'common' => $this->getName(),
316
                'official' => $this->getOfficialName(),
317
            ],
318
        ];
319
320
        // Get native names
321
        $natives = $this->getNativeNames() ?: [];
322
323
        // Get other translations
324
        $file = __DIR__.'/../resources/translations/'.mb_strtolower($this->getIsoAlpha2()).'.json';
325
        $translations = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
326
327
        // Merge all names together
328
        $result = array_merge($translations, $natives, $name);
329
330
        // Sort alphabetically
331
        ksort($result);
332
333
        return $result;
334
    }
335
336
    /**
337
     * Get the translation.
338
     *
339
     * @param string|null $languageCode
340
     *
341
     * @return array
342
     */
343
    public function getTranslation($languageCode = null)
344
    {
345
        return $this->getTranslations()[$languageCode] ?? current($this->getTranslations());
346
    }
347
348
    /**
349
     * Get the geodata.
350
     *
351
     * @return array|null
352
     */
353
    public function getGeodata()
354
    {
355
        return $this->get('geo');
356
    }
357
358
    /**
359
     * Get the continent.
360
     *
361
     * @return string|null
362
     */
363
    public function getContinent()
364
    {
365
        return current($this->get('geo.continent', [])) ?: null;
366
    }
367
368
    /**
369
     * Determine whether the country uses postal code.
370
     *
371
     * @return bool|null
372
     */
373
    public function usesPostalCode()
374
    {
375
        return $this->get('geo.postal_code');
376
    }
377
378
    /**
379
     * Get the latitude.
380
     *
381
     * @return string|null
382
     */
383
    public function getLatitude()
384
    {
385
        return $this->get('geo.latitude');
386
    }
387
388
    /**
389
     * Get the longitude.
390
     *
391
     * @return string|null
392
     */
393
    public function getLongitude()
394
    {
395
        return $this->get('geo.longitude');
396
    }
397
398
    /**
399
     * Get the described latitude.
400
     *
401
     * @return string|null
402
     */
403
    public function getLatitudeDesc()
404
    {
405
        return $this->get('geo.latitude_desc');
406
    }
407
408
    /**
409
     * Get the described longitude.
410
     *
411
     * @return string|null
412
     */
413
    public function getLongitudeDesc()
414
    {
415
        return $this->get('geo.longitude_desc');
416
    }
417
418
    /**
419
     * Get the maximum latitude.
420
     *
421
     * @return string|null
422
     */
423
    public function getMaxLatitude()
424
    {
425
        return $this->get('geo.max_latitude');
426
    }
427
428
    /**
429
     * Get the maximum longitude.
430
     *
431
     * @return string|null
432
     */
433
    public function getMaxLongitude()
434
    {
435
        return $this->get('geo.max_longitude');
436
    }
437
438
    /**
439
     * Get the minimum latitude.
440
     *
441
     * @return string|null
442
     */
443
    public function getMinLatitude()
444
    {
445
        return $this->get('geo.min_latitude');
446
    }
447
448
    /**
449
     * Get the minimum longitude.
450
     *
451
     * @return string|null
452
     */
453
    public function getMinLongitude()
454
    {
455
        return $this->get('geo.min_longitude');
456
    }
457
458
    /**
459
     * Get the area.
460
     *
461
     * @return int|null
462
     */
463
    public function getArea()
464
    {
465
        return $this->get('geo.area');
466
    }
467
468
    /**
469
     * Get the region.
470
     *
471
     * @return string|null
472
     */
473
    public function getRegion()
474
    {
475
        return $this->get('geo.region');
476
    }
477
478
    /**
479
     * Get the subregion.
480
     *
481
     * @return string|null
482
     */
483
    public function getSubregion()
484
    {
485
        return $this->get('geo.subregion');
486
    }
487
488
    /**
489
     * Get the world region.
490
     *
491
     * @return string|null
492
     */
493
    public function getWorldRegion()
494
    {
495
        return $this->get('geo.world_region');
496
    }
497
498
    /**
499
     * Get the region code.
500
     *
501
     * @return string|null
502
     */
503
    public function getRegionCode()
504
    {
505
        return $this->get('geo.region_code');
506
    }
507
508
    /**
509
     * Get the subregion code.
510
     *
511
     * @return string|null
512
     */
513
    public function getSubregionCode()
514
    {
515
        return $this->get('geo.subregion_code');
516
    }
517
518
    /**
519
     * Check the landlock status.
520
     *
521
     * @return bool|null
522
     */
523
    public function isLandlocked()
524
    {
525
        return $this->get('geo.landlocked');
526
    }
527
528
    /**
529
     * Get the borders.
530
     *
531
     * @return array|null
532
     */
533
    public function getBorders()
534
    {
535
        return $this->get('geo.borders');
536
    }
537
538
    /**
539
     * Determine whether the country is independent.
540
     *
541
     * @return string|null
542
     */
543
    public function isIndependent()
544
    {
545
        return $this->get('geo.independent');
546
    }
547
548
    /**
549
     * Get the given calling code or fallback to first calling code.
550
     *
551
     * @return string|null
552
     */
553
    public function getCallingCode()
554
    {
555
        return current($this->get('dialling.calling_code', [])) ?: (current($this->get('calling_code', [])) ?: null);
556
    }
557
558
    /**
559
     * Get the calling codes.
560
     *
561
     * @return array|null
562
     */
563
    public function getCallingCodes()
564
    {
565
        return $this->get('dialling.calling_code');
566
    }
567
568
    /**
569
     * Get the national prefix.
570
     *
571
     * @return string|null
572
     */
573
    public function getNationalPrefix()
574
    {
575
        return $this->get('dialling.national_prefix');
576
    }
577
578
    /**
579
     * Get the national number length.
580
     *
581
     * @return int|null
582
     */
583
    public function getNationalNumberLength()
584
    {
585
        return current($this->get('dialling.national_number_lengths', [])) ?: null;
586
    }
587
588
    /**
589
     * Get the national number lengths.
590
     *
591
     * @return array|null
592
     */
593
    public function getNationalNumberLengths()
594
    {
595
        return $this->get('dialling.national_number_lengths');
596
    }
597
598
    /**
599
     * Get the national destination code length.
600
     *
601
     * @return int|null
602
     */
603
    public function getNationalDestinationCodeLength()
604
    {
605
        return current($this->get('dialling.national_destination_code_lengths', [])) ?: null;
606
    }
607
608
    /**
609
     * Get the national destination code lengths.
610
     *
611
     * @return array|null
612
     */
613
    public function getnationaldestinationcodelengths()
614
    {
615
        return $this->get('dialling.national_destination_code_lengths');
616
    }
617
618
    /**
619
     * Get the international prefix.
620
     *
621
     * @return string|null
622
     */
623
    public function getInternationalPrefix()
624
    {
625
        return $this->get('dialling.international_prefix');
626
    }
627
628
    /**
629
     * Get the extras.
630
     *
631
     * @return array|null
632
     */
633
    public function getExtra()
634
    {
635
        return $this->get('extra');
636
    }
637
638
    /**
639
     * Get the geonameid.
640
     *
641
     * @return int|null
642
     */
643
    public function getGeonameid()
644
    {
645
        return $this->get('extra.geonameid');
646
    }
647
648
    /**
649
     * Get the edgar code.
650
     *
651
     * @return string|null
652
     */
653
    public function getEdgar()
654
    {
655
        return $this->get('extra.edgar');
656
    }
657
658
    /**
659
     * Get the itu code.
660
     *
661
     * @return string|null
662
     */
663
    public function getItu()
664
    {
665
        return $this->get('extra.itu');
666
    }
667
668
    /**
669
     * Get the marc code.
670
     *
671
     * @return string|null
672
     */
673
    public function getMarc()
674
    {
675
        return $this->get('extra.marc');
676
    }
677
678
    /**
679
     * Get the wmo code.
680
     *
681
     * @return string|null
682
     */
683
    public function getWmo()
684
    {
685
        return $this->get('extra.wmo');
686
    }
687
688
    /**
689
     * Get the ds code.
690
     *
691
     * @return string|null
692
     */
693
    public function getDs()
694
    {
695
        return $this->get('extra.ds');
696
    }
697
698
    /**
699
     * Get the fifa code.
700
     *
701
     * @return string|null
702
     */
703
    public function getFifa()
704
    {
705
        return $this->get('extra.fifa');
706
    }
707
708
    /**
709
     * Get the fips code.
710
     *
711
     * @return string|null
712
     */
713
    public function getFips()
714
    {
715
        return $this->get('extra.fips');
716
    }
717
718
    /**
719
     * Get the gaul code.
720
     *
721
     * @return int|null
722
     */
723
    public function getGaul()
724
    {
725
        return $this->get('extra.gaul');
726
    }
727
728
    /**
729
     * Get the ioc code.
730
     *
731
     * @return string|null
732
     */
733
    public function getIoc()
734
    {
735
        return $this->get('extra.ioc');
736
    }
737
738
    /**
739
     * Get the cowc code.
740
     *
741
     * @return string|null
742
     */
743
    public function getCowc()
744
    {
745
        return $this->get('extra.cowc');
746
    }
747
748
    /**
749
     * Get the cown code.
750
     *
751
     * @return int|null
752
     */
753
    public function getCown()
754
    {
755
        return $this->get('extra.cown');
756
    }
757
758
    /**
759
     * Get the fao code.
760
     *
761
     * @return int|null
762
     */
763
    public function getFao()
764
    {
765
        return $this->get('extra.fao');
766
    }
767
768
    /**
769
     * Get the imf code.
770
     *
771
     * @return int|null
772
     */
773
    public function getImf()
774
    {
775
        return $this->get('extra.imf');
776
    }
777
778
    /**
779
     * Get the ar5 code.
780
     *
781
     * @return string|null
782
     */
783
    public function getAr5()
784
    {
785
        return $this->get('extra.ar5');
786
    }
787
788
    /**
789
     * Get the address format.
790
     *
791
     * @return string|null
792
     */
793
    public function getAddressFormat()
794
    {
795
        return $this->get('extra.address_format');
796
    }
797
798
    /**
799
     * Determine whether the country is EU member.
800
     *
801
     * @return bool|null
802
     */
803
    public function isEuMember()
804
    {
805
        return $this->get('extra.eu_member');
806
    }
807
808
    /**
809
     * Get the VAT rates.
810
     *
811
     * @return array|null
812
     */
813
    public function getVatRates()
814
    {
815
        return $this->get('extra.vat_rates');
816
    }
817
818
    /**
819
     * Get the emoji.
820
     *
821
     * @return array|null
822
     */
823
    public function getEmoji()
824
    {
825
        return $this->get('extra.emoji') ?: $this->get('emoji');
826
    }
827
828
    /**
829
     * Get the geographic data structure.
830
     *
831
     * @return string|null
832
     */
833
    public function getGeoJson()
834
    {
835
        if (! ($code = $this->getIsoAlpha2())) {
836
            return;
837
        }
838
839
        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...
840
    }
841
842
    /**
843
     * Get the flag.
844
     *
845
     * @return string|null
846
     */
847
    public function getFlag()
848
    {
849
        if (! ($code = $this->getIsoAlpha2())) {
850
            return;
851
        }
852
853
        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...
854
    }
855
856
    /**
857
     * Get the divisions.
858
     *
859
     * @return array|null
860
     */
861
    public function getDivisions()
862
    {
863
        if (! ($code = $this->getIsoAlpha2())) {
864
            return;
865
        }
866
867
        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...
868
    }
869
870
    /**
871
     * Get the divisions.
872
     *
873
     * @param string $division
874
     *
875
     * @return array|null
876
     */
877
    public function getDivision($division)
878
    {
879
        return ! empty($this->getDivisions()) && isset($this->getDivisions()[$division])
880
            ? $this->getDivisions()[$division] : null;
881
    }
882
}
883