forked from alexander-schranz/iso-3166-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubdivision.php
More file actions
93 lines (80 loc) · 2.2 KB
/
Subdivision.php
File metadata and controls
93 lines (80 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace L91\ISO_3166_2;
use stdClass;
class Subdivision
{
/**
* @return string
*/
protected static function getSubdivisionFolderPath() : string
{
return __DIR__ . '/subdivisions';
}
/**
* @param string $countryCode
*
* @return string
*/
protected static function getSubdivisionFilePath(string $countryCode) : string
{
return self::getSubdivisionFolderPath() . '/' . $countryCode . '.json';
}
/**
* @param string $countryCode
* @param string $locale
*
* @return string
*/
protected static function getSubdivisionLocaleFilePath(string $countryCode, string $locale) : string
{
return self::getSubdivisionFolderPath() . '/' . $countryCode . '/' . $locale . '.json';
}
/**
* @param string $path
* @param bool $assoc
*
* @return array|stdClass
*/
protected static function loadJsonFile(string $path, bool $assoc = false) : array|stdClass
{
$data = array();
if (! $assoc) {
$data = new \stdClass();
}
if (file_exists($path)) {
$json = file_get_contents($path);
$data = json_decode($json, $assoc);
}
return $data;
}
/**
* @param string $countryCode
* @param ?string $locale
*
* @return array|stdClass
*/
protected static function loadSubdivisionFile(string $countryCode, ?string $locale = null) : array|stdClass
{
$data = self::loadJsonFile(self::getSubdivisionFilePath($countryCode));
if ($locale) {
$data = array_merge(
$data,
self::loadJsonFile(self::getSubdivisionLocaleFilePath($countryCode, $locale))
);
}
return $data;
}
/**
* @param string $countryCode
* @param string $locale
*
* @return array|stdClass
*/
public static function getSubdivisions(string $countryCode, string $locale = null) : array|stdClass
{
return self::loadSubdivisionFile(
strtolower($countryCode),
$locale ? strtolower($locale) : null
);
}
}