Passed
Push — master ( d0cbe7...bbf3e3 )
by Michael
03:29
created

ArrayResourceContext::user()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 33
    public static function create(array $context = []): self
26
    {
27 33
        return new self($context);
28
    }
29
30 33
    public function __construct(array $context = [])
31
    {
32 33
        $context = $this->validateContext($context);
33
34
        /** @var array */
35 33
        $this->context = $context;
36 33
    }
37
38 33
    public function validateContext(array $context): array
39
    {
40 33
        $valid_keys = self::$context_keys;
41 33
        return Arr::only(
42 33
            $context,
43 33
            array_intersect(
44 33
                $valid_keys,
45 33
                array_keys($context)
46
            )
47
        );
48
    }
49
50 30
    public function validateKey(string $key): string
51
    {
52 30
        [$head] = $this->splitKey($key);
53
54 30
        if (!in_array($head, self::$context_keys)) {
55 1
            throw new InvalidArgumentException("Invalid key '$head'");
56
        }
57
58 29
        return $key;
59
    }
60
61 19
    public function filters(): array
62
    {
63 19
        return $this->get('filters', []);
64
    }
65
66 15
    public function page(): int
67
    {
68 15
        return $this->get('page', 1);
69
    }
70
71 15
    public function perPage(): int
72
    {
73 15
        return $this->get('per_page', 15);
74
    }
75
76 18
    public function paginate(): bool
77
    {
78 18
        return (bool) $this->get('page', false);
79
    }
80
81 20
    public function sortBy(): array
82
    {
83
        return [
84 20
            $this->get('sort_by', null),
85 20
            $this->get('sort_order', 'asc'),
86
        ];
87
    }
88
89 22
    public function with(): array
90
    {
91 22
        return $this->get('with', []);
92
    }
93
94 1
    public function user(): ?Model
95
    {
96 1
        return $this->get('user');
97
    }
98
99 1
    public function merge(array $values)
100
    {
101 1
        $values = $this->validateContext($values);
102
103 1
        $this->context = array_merge_recursive($this->context, $values);
104
105 1
        return $this;
106
    }
107
108 29
    public function get($key, $default = null)
109
    {
110 29
        $this->validateKey($key);
111
112 29
        return data_get($this->context, $key, $default);
113
    }
114
115 3
    public function set(string $key, $value)
116
    {
117 3
        $this->validateKey($key);
118
119 2
        [$head, $tail] = $this->splitKey($key);
120
121 2
        if ($tail) {
122 2
            $this->context[$head][$tail] = $value;
123
        } else {
124 1
            $this->context[$head] = $value;
125
        }
126
127
128 2
        return $this;
129
    }
130
131 2
    public function exclude(array $keys)
132
    {
133 2
        $this->context = Arr::except($this->context, $keys);
134
135 2
        return $this;
136
    }
137
138 1
    public function toArray(): array
139
    {
140 1
        return array_merge($this->context, ['paginate' => $this->paginate()]);
141
    }
142
143 30
    private function splitKey($key)
144
    {
145 30
        $dos_position = strpos($key, '.');
146 30
        $head = $dos_position !== false
147 4
            ? substr($key, 0, $dos_position)
148 30
            : $key;
149 30
        $tail = $dos_position !== false
150 4
            ? substr($key, $dos_position + 1)
151 30
            : null;
152 30
        return [$head, $tail];
153
    }
154
}
155