|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ArinaSystems\JsonResponse; |
|
4
|
|
|
|
|
5
|
|
|
use ArinaSystems\JsonResponse\Status\Status as StatusInterface; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
class Status |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $status = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new instance. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $status |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(array $status) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->loadStatus($status); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get a status from an array using "dot" notation. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $key |
|
29
|
|
|
* @param mixed|null $default |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public function get(string $key, $default = null) |
|
33
|
|
|
{ |
|
34
|
|
|
return Arr::get($this->status, $key, $default); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Forget a status from by given name. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $key |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function forget(string $key) |
|
44
|
|
|
{ |
|
45
|
|
|
Arr::forget($this->status, $key); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set a status to a given value using "dot" notation. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string|array $keys |
|
52
|
|
|
* @param null|mixed $value |
|
53
|
|
|
* @return self |
|
54
|
|
|
*/ |
|
55
|
|
|
public function set($keys, $value = null) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_array($keys)) { |
|
58
|
|
|
foreach ($keys as $key => $value) { |
|
59
|
|
|
$this->set($key, $value); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (is_string($keys)) { |
|
64
|
|
|
Arr::set($this->status, $keys, $value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if an status is exist in the array. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array|string $key |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function has($key) |
|
77
|
|
|
{ |
|
78
|
|
|
return Arr::has($this->all(), $key); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Retrieve all response status. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function all() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->status; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Call the response status. |
|
93
|
|
|
* |
|
94
|
|
|
* @return \ArinaSystems\JsonResponse\JsonResponse |
|
95
|
|
|
*/ |
|
96
|
|
|
public function call(string $status, $builder) |
|
97
|
|
|
{ |
|
98
|
|
|
$status = $this->get($status); |
|
99
|
|
|
|
|
100
|
|
|
if (! (new $status()) instanceof StatusInterface) { |
|
101
|
|
|
throw new \Exception("The class '{$status}' must implement 'ArinaSystems\JsonResponse\Status\Status' interface.", 1); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return (new $status())->handle($builder); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Load the given status array to response status object. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $status |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function loadStatus(array $status): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->status = $status; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
|
|
public function instance() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|