|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mblarsen\LaravelRepository; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
final class ArrayResourceContext implements ResourceContext |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var array */ |
|
11
|
|
|
protected $context; |
|
12
|
|
|
|
|
13
|
26 |
|
public static function create(array $context = []): self |
|
14
|
|
|
{ |
|
15
|
26 |
|
return new self($context); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
26 |
|
public function __construct(array $context = []) |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var array */ |
|
21
|
26 |
|
$this->context = $context; |
|
22
|
26 |
|
} |
|
23
|
|
|
|
|
24
|
16 |
|
public function filters(): array |
|
25
|
|
|
{ |
|
26
|
16 |
|
return $this->get('filters', []); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
15 |
|
public function page(): int |
|
30
|
|
|
{ |
|
31
|
15 |
|
return $this->get('page', 1); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
15 |
|
public function perPage(): int |
|
35
|
|
|
{ |
|
36
|
15 |
|
return $this->get('per_page', 15); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
17 |
|
public function paginate(): bool |
|
40
|
|
|
{ |
|
41
|
17 |
|
return (bool) $this->get('page', false); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
19 |
|
public function sortBy(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
19 |
|
$this->get('sort_by', null), |
|
48
|
19 |
|
$this->get('sort_order', 'asc'), |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
21 |
|
public function with(): array |
|
53
|
|
|
{ |
|
54
|
21 |
|
return $this->get('with', []); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function user(): ?Model |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->get('user'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
24 |
|
protected function get($key, $default = null) |
|
63
|
|
|
{ |
|
64
|
24 |
|
return $this->context[$key] ?? $default; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function merge(array $values) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$this->context = array_merge($this->context, $values); |
|
70
|
|
|
|
|
71
|
1 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function exclude(array $keys) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$this->context = Arr::except($this->context, $keys); |
|
77
|
|
|
|
|
78
|
2 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|