Completed
Push — develop ( 81e696...4829a8 )
by Abdelrahman
08:45
created

Loader::countries()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 8
nop 2
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Country Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Country Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Country;
17
18
use Rinvex\Country\Exceptions\CountryLoaderException;
19
20
class Loader
21
{
22
    /**
23
     * The countries array.
24
     *
25
     * @var array
26
     */
27
    protected static $countries;
28
29
    /**
30
     * Get the country by it's ISO 3166-1 alpha-2.
31
     *
32
     * @param string $code
33
     * @param bool   $hydrate
34
     *
35
     * @return \Rinvex\Country\Country|array
36
     */
37
    public static function country($code, $hydrate = true)
38
    {
39
        if (! isset(static::$countries[$code])) {
40
            static::$countries[$code] = json_decode(static::getFile(__DIR__.'/../resources/data/'.$code.'.json'), true);
41
        }
42
43
        return $hydrate ? new Country(static::$countries[$code]) : static::$countries[$code];
44
    }
45
46
    /**
47
     * Get all countries short-listed.
48
     *
49
     * @param bool $longlist
50
     * @param bool $hydrate
51
     *
52
     * @return array
53
     */
54
    public static function countries($longlist = false, $hydrate = false)
55
    {
56
        $list = $longlist ? 'longlist' : 'shortlist';
57
58
        if (! isset(static::$countries[$list])) {
59
            static::$countries[$list] = json_decode(static::getFile(__DIR__.'/../resources/data/'.$list.'.json'), true);
60
        }
61
62
        return $hydrate ? array_map(function ($country) {
63
            return new Country($country);
64
        }, static::$countries[$list]) : static::$countries[$list];
65
    }
66
67
    /**
68
     * Filter items by the given key value pair.
69
     *
70
     * @param  string  $key
71
     * @param  mixed  $operator
72
     * @param  mixed  $value
73
     *
74
     * @return array
75
     */
76
    public static function where($key, $operator, $value = null)
77
    {
78
        if (func_num_args() == 2) {
79
            $value = $operator;
80
            $operator = '=';
81
        }
82
83
        if (! isset(static::$countries['longlist'])) {
84
            static::$countries['longlist'] = json_decode(static::getFile(__DIR__.'/../resources/data/longlist.json'), true);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 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...
85
        }
86
87
        return static::filter(static::$countries['longlist'], static::operatorForWhere($key, $operator, $value));
88
    }
89
90
    /**
91
     * Get an operator checker callback.
92
     *
93
     * @param  string  $key
94
     * @param  string  $operator
95
     * @param  mixed  $value
96
     *
97
     * @return \Closure
98
     */
99
    public static function operatorForWhere($key, $operator, $value)
100
    {
101
        return function ($item) use ($key, $operator, $value) {
102
            $retrieved = static::get($item, $key);
103
104
            switch ($operator) {
105
                default:
106
                case '=':
107
                case '==':  return $retrieved == $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108
                case '!=':
109
                case '<>':  return $retrieved != $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
110
                case '<':   return $retrieved < $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
111
                case '>':   return $retrieved > $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
112
                case '<=':  return $retrieved <= $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
113
                case '>=':  return $retrieved >= $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
                case '===': return $retrieved === $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
115
                case '!==': return $retrieved !== $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
116
            }
117
        };
118
    }
119
120
    /**
121
     * Run a filter over each of the items.
122
     *
123
     * @param array         $items
124
     * @param callable|null $callback
125
     *
126
     * @return array
127
     */
128
    public static function filter($items, callable $callback = null)
129
    {
130
        if ($callback) {
131
            return array_filter($items, $callback, ARRAY_FILTER_USE_BOTH);
132
        }
133
134
        return array_filter($items);
135
    }
136
137
    /**
138
     * Get an item from an array or object using "dot" notation.
139
     *
140
     * @param mixed        $target
141
     * @param string|array $key
142
     * @param mixed        $default
143
     *
144
     * @return mixed
145
     */
146
    public static function get($target, $key, $default = null)
147
    {
148
        if (is_null($key)) {
149
            return $target;
150
        }
151
152
        $key = is_array($key) ? $key : explode('.', $key);
153
154
        while (($segment = array_shift($key)) !== null) {
155
            if ($segment === '*') {
156
                if (! is_array($target)) {
157
                    return value($default);
158
                }
159
160
                $result = static::pluck($target, $key);
161
162
                return in_array('*', $key) ? static::collapse($result) : $result;
163
            }
164
165
            if (is_array($target) && array_key_exists($segment, $target)) {
166
                $target = $target[$segment];
167
            } elseif (is_object($target) && isset($target->{$segment})) {
168
                $target = $target->{$segment};
169
            } else {
170
                return value($default);
171
            }
172
        }
173
174
        return $target;
175
    }
176
177
    /**
178
     * Pluck an array of values from an array.
179
     *
180
     * @param array             $array
181
     * @param string|array      $value
182
     * @param string|array|null $key
183
     *
184
     * @return array
185
     */
186
    public static function pluck($array, $value, $key = null)
187
    {
188
        $results = [];
189
190
        $value = is_string($value) ? explode('.', $value) : $value;
191
192
        $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
193
194
        foreach ($array as $item) {
195
            $itemValue = static::get($item, $value);
196
197
            // If the key is "null", we will just append the value to the array and keep
198
            // looping. Otherwise we will key the array using the value of the key we
199
            // received from the developer. Then we'll return the final array form.
200
            if (is_null($key)) {
201
                $results[] = $itemValue;
202
            } else {
203
                $itemKey = static::get($item, $key);
204
205
                $results[$itemKey] = $itemValue;
206
            }
207
        }
208
209
        return $results;
210
    }
211
212
    /**
213
     * Collapse an array of arrays into a single array.
214
     *
215
     * @param array $array
216
     *
217
     * @return array
218
     */
219
    public static function collapse($array)
220
    {
221
        $results = [];
222
223
        foreach ($array as $values) {
224
            if (! is_array($values)) {
225
                continue;
226
            }
227
228
            $results = array_merge($results, $values);
229
        }
230
231
        return $results;
232
    }
233
234
    /**
235
     * Get contents of the given file path.
236
     *
237
     * @param string $filePath
238
     *
239
     * @throws \Rinvex\Country\Exceptions\CountryLoaderException
240
     *
241
     * @return string
242
     */
243
    public static function getFile($filePath)
244
    {
245
        if (! file_exists($filePath)) {
246
            throw CountryLoaderException::invalidCountry();
247
        }
248
249
        return file_get_contents($filePath);
250
    }
251
}
252