Completed
Pull Request — master (#102)
by Hilmi Erdem
03:43
created

Country::getResourceFileContents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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