1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mblarsen\LaravelRepository; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
final class ArrayResourceContext implements ResourceContext |
10
|
|
|
{ |
11
|
|
|
/** @var array $context_keys */ |
12
|
|
|
protected static $context_keys = [ |
13
|
|
|
'filters', |
14
|
|
|
'page', |
15
|
|
|
'per_page', |
16
|
|
|
'sort_by', |
17
|
|
|
'sort_order', |
18
|
|
|
'user', |
19
|
|
|
'with', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
protected $context; |
24
|
|
|
|
25
|
32 |
|
public static function create(array $context = []): self |
26
|
|
|
{ |
27
|
32 |
|
return new self($context); |
28
|
|
|
} |
29
|
|
|
|
30
|
32 |
|
public function __construct(array $context = []) |
31
|
|
|
{ |
32
|
32 |
|
$context = $this->validateContext($context); |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
32 |
|
$this->context = $context; |
36
|
32 |
|
} |
37
|
|
|
|
38
|
32 |
|
public function validateContext(array $context): array |
39
|
|
|
{ |
40
|
32 |
|
$valid_keys = self::$context_keys; |
41
|
32 |
|
return Arr::only( |
42
|
32 |
|
$context, |
43
|
32 |
|
array_intersect( |
44
|
32 |
|
$valid_keys, |
45
|
32 |
|
array_keys($context) |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
29 |
|
public function validateKey(string $key): string |
51
|
|
|
{ |
52
|
29 |
|
$dos_position = strpos($key, '.'); |
53
|
29 |
|
$head_key = $dos_position === false |
54
|
27 |
|
? $key |
55
|
29 |
|
: substr($key, 0, $dos_position); |
56
|
|
|
|
57
|
29 |
|
if (!in_array($head_key, self::$context_keys)) { |
58
|
1 |
|
throw new InvalidArgumentException("Invalid key '$head_key'"); |
59
|
|
|
} |
60
|
|
|
|
61
|
28 |
|
return $key; |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
public function filters(): array |
65
|
|
|
{ |
66
|
18 |
|
return $this->get('filters', []); |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
public function page(): int |
70
|
|
|
{ |
71
|
15 |
|
return $this->get('page', 1); |
72
|
|
|
} |
73
|
|
|
|
74
|
15 |
|
public function perPage(): int |
75
|
|
|
{ |
76
|
15 |
|
return $this->get('per_page', 15); |
77
|
|
|
} |
78
|
|
|
|
79
|
18 |
|
public function paginate(): bool |
80
|
|
|
{ |
81
|
18 |
|
return (bool) $this->get('page', false); |
82
|
|
|
} |
83
|
|
|
|
84
|
19 |
|
public function sortBy(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
19 |
|
$this->get('sort_by', null), |
88
|
19 |
|
$this->get('sort_order', 'asc'), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
22 |
|
public function with(): array |
93
|
|
|
{ |
94
|
22 |
|
return $this->get('with', []); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function user(): ?Model |
98
|
|
|
{ |
99
|
1 |
|
return $this->get('user'); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function merge(array $values) |
103
|
|
|
{ |
104
|
1 |
|
$values = $this->validateContext($values); |
105
|
|
|
|
106
|
1 |
|
$this->context = array_merge_recursive($this->context, $values); |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
28 |
|
public function get($key, $default = null) |
112
|
|
|
{ |
113
|
28 |
|
$this->validateKey($key); |
114
|
|
|
|
115
|
28 |
|
return data_get($this->context, $key, $default); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
public function set(string $key, $value, $overwrite = true) |
119
|
|
|
{ |
120
|
2 |
|
$this->validateKey($key); |
121
|
|
|
|
122
|
1 |
|
data_set($this->context, $key, $value, $overwrite); |
123
|
|
|
|
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
public function exclude(array $keys) |
128
|
|
|
{ |
129
|
2 |
|
$this->context = Arr::except($this->context, $keys); |
130
|
|
|
|
131
|
2 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function toArray(): array |
135
|
|
|
{ |
136
|
1 |
|
return array_merge($this->context, ['paginate' => $this->paginate()]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|