1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
4
|
|
|
|
5
|
|
|
class GeoConstraint |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Determines if the constraint is allowing |
9
|
|
|
* requests from the specified countries |
10
|
|
|
* |
11
|
|
|
* @var boolean |
12
|
|
|
*/ |
13
|
|
|
protected $allowed; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The countries to apply the constraint on |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $countries; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The callback to execute if the country |
24
|
|
|
* is not allowed |
25
|
|
|
* |
26
|
|
|
* @var callable |
27
|
|
|
*/ |
28
|
|
|
protected $callback; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Determine if the constraint is |
32
|
|
|
* bound to a callback |
33
|
|
|
* |
34
|
|
|
* @var null|string |
35
|
|
|
*/ |
36
|
|
|
protected $binding = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new GeoConstraint instance |
40
|
|
|
* |
41
|
|
|
* @param boolean $allowed |
42
|
|
|
* @param array $countries |
43
|
|
|
* @param null|\LaraCrafts\GeoRoutes\GeoCallback $callback |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
120 |
|
public function __construct(bool $allowed, array $countries, GeoCallback $callback = null) |
48
|
|
|
{ |
49
|
120 |
|
$this->allowed = $allowed; |
50
|
120 |
|
$this->countries = $countries; |
51
|
120 |
|
$this->callback = $callback; |
52
|
120 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the constraint's callback |
56
|
|
|
* |
57
|
|
|
* @return \LaraCrafts\GeoRoutes\GeoCallback |
58
|
|
|
*/ |
59
|
110 |
|
public function getCallback() |
60
|
|
|
{ |
61
|
110 |
|
if (!is_null($this->binding)) { |
62
|
40 |
|
return CallbacksRegistrar::resolve($this->binding); |
63
|
|
|
} |
64
|
|
|
|
65
|
70 |
|
return $this->callback; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the contraint's callback |
70
|
|
|
* |
71
|
|
|
* @param \LaraCrafts\GeoRoutes\GeoCallback $callback |
72
|
|
|
* |
73
|
|
|
* @return \LaraCrafts\GeoRoutes\GeoCallback |
74
|
|
|
*/ |
75
|
30 |
|
public function setCallback(GeoCallback $callback) |
76
|
|
|
{ |
77
|
30 |
|
$this->callback = $callback; |
78
|
|
|
|
79
|
30 |
|
return $this->callback; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Bind the constraint with a callback |
84
|
|
|
* |
85
|
|
|
* @param string $callback |
86
|
|
|
* @param array|null $arguments |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
40 |
|
public function bind(string $callback, array $arguments = null) |
91
|
|
|
{ |
92
|
40 |
|
$this->binding = CallbacksRegistrar::bind($callback, $arguments); |
93
|
40 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the countries covered by |
97
|
|
|
* the constraint |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
110 |
|
public function getCountries() |
102
|
|
|
{ |
103
|
110 |
|
return $this->countries; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the countries covered by |
108
|
|
|
* the constraint |
109
|
|
|
* |
110
|
|
|
* @param string ...$countries |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
50 |
|
public function setCountries(string ...$countries) |
115
|
|
|
{ |
116
|
50 |
|
$this->countries = $countries; |
117
|
|
|
|
118
|
50 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add a country to the list of countries |
123
|
|
|
* covered by the constraint |
124
|
|
|
* |
125
|
|
|
* @param string $country |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function addCountry(string $country) |
130
|
|
|
{ |
131
|
|
|
array_push($this->countries, $country); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the access status |
138
|
|
|
* |
139
|
|
|
* @param boolean $allowed |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
120 |
|
public function setAccess(bool $allowed) |
144
|
|
|
{ |
145
|
120 |
|
$this->allowed = $allowed; |
146
|
|
|
|
147
|
120 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine if the constraint allows |
152
|
|
|
* access |
153
|
|
|
* |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
110 |
|
public function isAllowed() |
157
|
|
|
{ |
158
|
110 |
|
return $this->allowed; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|