Completed
Push — master ( bfaf39...202bdc )
by Michael
03:45
created

ArrayResourceContext::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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