1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Countries\Package\Support; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use IlluminateAgnostic\Arr\Support\Arr; |
7
|
|
|
use IlluminateAgnostic\Str\Support\Str; |
8
|
|
|
use PragmaRX\Coollection\Package\Coollection; |
9
|
|
|
|
10
|
|
|
class Collection extends Coollection |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Magic call methods. |
14
|
|
|
* |
15
|
|
|
* @param string $name |
16
|
|
|
* @param array $arguments |
17
|
|
|
* @return mixed|static |
18
|
|
|
*/ |
19
|
32 |
|
public function __call($name, $arguments) |
20
|
|
|
{ |
21
|
32 |
|
if ($name !== 'where' && Str::startsWith($name, 'where')) { |
22
|
1 |
|
$name = strtolower(preg_replace('/([A-Z])/', '.$1', lcfirst(substr($name, 5)))); |
23
|
1 |
|
if (\count($arguments) === 2) { |
24
|
|
|
return $this->where($name, $arguments[0], $arguments[1]); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
if (\count($arguments) === 1) { |
28
|
1 |
|
return $this->where($name, $arguments[0]); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
32 |
|
return parent::__call($name, $arguments); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Hydrate configured default elements. |
37
|
|
|
* |
38
|
|
|
* @param Collection $countries |
39
|
|
|
* @return Collection |
40
|
|
|
*/ |
41
|
27 |
|
public function hydrateDefaultElements($countries) |
42
|
|
|
{ |
43
|
27 |
|
return $countries->hydrate(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Where on steroids. |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* @param mixed $operator |
51
|
|
|
* @param null $value |
52
|
|
|
* @return static |
53
|
|
|
*/ |
54
|
27 |
|
public function where($key, $operator, $value = null) |
55
|
|
|
{ |
56
|
27 |
|
if (\func_num_args() === 2) { |
57
|
27 |
|
$value = $operator; |
58
|
|
|
|
59
|
27 |
|
$operator = '='; |
60
|
|
|
} |
61
|
|
|
|
62
|
27 |
|
$countries = method_exists($this, 'where'.ucfirst($key)) |
63
|
2 |
|
? $this->{'where'.ucfirst($key)}($value) |
64
|
27 |
|
: parent::where($key, $operator, $value); |
65
|
|
|
|
66
|
27 |
|
return $this->hydrateDefaultElements($countries); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Where language. |
71
|
|
|
* |
72
|
|
|
* @param $value |
73
|
|
|
* @return static |
74
|
|
|
*/ |
75
|
1 |
|
public function whereLanguage($value) |
76
|
|
|
{ |
77
|
1 |
|
return $this->_whereAttribute('languages', $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Where language using iso code. |
82
|
|
|
* |
83
|
|
|
* @param $value |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
1 |
|
public function whereISO639_3($value) |
87
|
|
|
{ |
88
|
1 |
|
return $this->_whereKey('languages', $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Where currency using ISO code. |
93
|
|
|
* |
94
|
|
|
* @param $value |
95
|
|
|
* @return static |
96
|
|
|
*/ |
97
|
1 |
|
public function whereISO4217($value) |
98
|
|
|
{ |
99
|
1 |
|
return $this->_whereAttribute('currency', $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Where for different attributes. |
104
|
|
|
* |
105
|
|
|
* @param string $propertyName |
106
|
|
|
* @param $find |
107
|
|
|
* @param Closure $finderClosure |
108
|
|
|
* @return static |
109
|
|
|
*/ |
110
|
3 |
|
private function arrayFinder(string $propertyName, $find, Closure $finderClosure) |
111
|
|
|
{ |
112
|
|
|
return $this->filter(function ($data) use ($find, $propertyName, $finderClosure) { |
113
|
|
|
try { |
114
|
3 |
|
$attributeValue = $data->{$propertyName}; |
115
|
3 |
|
} catch (\Exception $e) { |
|
|
|
|
116
|
3 |
|
$attributeValue = null; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
return \is_null($attributeValue) |
120
|
3 |
|
? null |
121
|
3 |
|
: $finderClosure($find, $attributeValue, $data); |
122
|
3 |
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Where for keys. |
127
|
|
|
* |
128
|
|
|
* @param string $arrayName |
129
|
|
|
* @param $value |
130
|
|
|
* @return static |
131
|
|
|
*/ |
132
|
1 |
View Code Duplication |
private function _whereKey(string $arrayName, $value) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$finderClosure = function ($value, $attributeValue) { |
135
|
1 |
|
return Arr::has($attributeValue, $value); |
136
|
1 |
|
}; |
137
|
|
|
|
138
|
1 |
|
return $this->hydrateDefaultElements( |
139
|
1 |
|
$this->arrayFinder($arrayName, $value, $finderClosure) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Where for different attributes. |
145
|
|
|
* |
146
|
|
|
* @param string $arrayName |
147
|
|
|
* @param $value |
148
|
|
|
* @return static |
149
|
|
|
*/ |
150
|
2 |
View Code Duplication |
private function _whereAttribute(string $arrayName, $value) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$finderClosure = function ($value, $attributeValue) { |
153
|
2 |
|
return \in_array($value, $attributeValue->toArray()); |
154
|
2 |
|
}; |
155
|
|
|
|
156
|
2 |
|
return $this->hydrateDefaultElements( |
157
|
2 |
|
$this->arrayFinder($arrayName, $value, $finderClosure) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.