1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\University; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
|
9
|
|
|
class UniversityLoader |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The universities array. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected static $universities; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the university by slug. |
20
|
|
|
* |
21
|
|
|
* @param string $slug |
22
|
|
|
* @param bool $hydrate |
23
|
|
|
* |
24
|
|
|
* @throws \Rinvex\University\UniversityLoaderException |
25
|
|
|
* |
26
|
|
|
* @return \Rinvex\University\University|array |
27
|
|
|
*/ |
28
|
|
|
public static function university($slug, $hydrate = true) |
29
|
|
|
{ |
30
|
|
|
if (! isset(static::$universities[$slug])) { |
31
|
|
|
$university = current(glob(__DIR__."/../resources/*/{$slug}.json")); |
32
|
|
|
static::$universities[$slug] = $university ? json_decode(static::getFile($university), true) : null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (! isset(static::$universities[$slug])) { |
36
|
|
|
throw UniversityLoaderException::invalidUniversity(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $hydrate ? new University(static::$universities[$slug]) : static::$universities[$slug]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get universities for the given country. |
44
|
|
|
* |
45
|
|
|
* @param string|null $countryCode |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public static function universities($countryCode = null) |
50
|
|
|
{ |
51
|
|
|
if (! isset(static::$universities['names'])) { |
52
|
|
|
static::$universities['names'] = json_decode(static::getFile(__DIR__.'/../resources/names.json'), true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return is_null($countryCode) ? static::$universities['names'] : static::$universities['names'][$countryCode] ?? null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get an item from an array or object using "dot" notation. |
60
|
|
|
* |
61
|
|
|
* @param mixed $target |
62
|
|
|
* @param string|array $key |
63
|
|
|
* @param mixed $default |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
protected static function get($target, $key, $default = null) |
68
|
|
|
{ |
69
|
|
|
if (is_null($key)) { |
70
|
|
|
return $target; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$key = is_array($key) ? $key : explode('.', $key); |
74
|
|
|
|
75
|
|
|
while (($segment = array_shift($key)) !== null) { |
76
|
|
|
if ($segment === '*') { |
77
|
|
|
if (! is_array($target)) { |
78
|
|
|
return $default instanceof Closure ? $default() : $default; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$result = static::pluck($target, $key); |
82
|
|
|
|
83
|
|
|
return in_array('*', $key) ? static::collapse($result) : $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (is_array($target) && array_key_exists($segment, $target)) { |
87
|
|
|
$target = $target[$segment]; |
88
|
|
|
} elseif (is_object($target) && isset($target->{$segment})) { |
89
|
|
|
$target = $target->{$segment}; |
90
|
|
|
} else { |
91
|
|
|
return $default instanceof Closure ? $default() : $default; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $target; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Pluck an array of values from an array. |
100
|
|
|
* |
101
|
|
|
* @param array $array |
102
|
|
|
* @param string|array $value |
103
|
|
|
* @param string|array|null $key |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected static function pluck($array, $value, $key = null) |
108
|
|
|
{ |
109
|
|
|
$results = []; |
110
|
|
|
|
111
|
|
|
$value = is_string($value) ? explode('.', $value) : $value; |
112
|
|
|
|
113
|
|
|
$key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
114
|
|
|
|
115
|
|
|
foreach ($array as $item) { |
116
|
|
|
$itemValue = static::get($item, $value); |
117
|
|
|
|
118
|
|
|
// If the key is "null", we will just append the value to the array and keep |
119
|
|
|
// looping. Otherwise we will key the array using the value of the key we |
120
|
|
|
// received from the developer. Then we'll return the final array form. |
121
|
|
|
if (is_null($key)) { |
122
|
|
|
$results[] = $itemValue; |
123
|
|
|
} else { |
124
|
|
|
$itemKey = static::get($item, $key); |
125
|
|
|
|
126
|
|
|
$results[$itemKey] = $itemValue; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $results; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Collapse an array of arrays into a single array. |
135
|
|
|
* |
136
|
|
|
* @param array $array |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected static function collapse($array) |
141
|
|
|
{ |
142
|
|
|
$results = []; |
143
|
|
|
|
144
|
|
|
foreach ($array as $values) { |
145
|
|
|
if (! is_array($values)) { |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$results = array_merge($results, $values); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $results; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get contents of the given file path. |
157
|
|
|
* |
158
|
|
|
* @param string $filePath |
159
|
|
|
* |
160
|
|
|
* @throws \Rinvex\University\UniversityLoaderException |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public static function getFile($filePath) |
165
|
|
|
{ |
166
|
|
|
if (! file_exists($filePath)) { |
167
|
|
|
throw UniversityLoaderException::invalidUniversity(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return file_get_contents($filePath); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|