Completed
Push — master ( a6a8d5...bfaf39 )
by Michael
03:48
created

ArrayResourceContext   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 35
dl 0
loc 103
ccs 39
cts 39
cp 1
rs 10
c 4
b 0
f 1
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A create() 0 3 1
A __construct() 0 6 1
A get() 0 3 1
A merge() 0 7 1
A paginate() 0 3 1
A validateContext() 0 8 1
A sortBy() 0 5 1
A with() 0 3 1
A user() 0 3 1
A filters() 0 3 1
A exclude() 0 5 1
A page() 0 3 1
A perPage() 0 3 1
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 $context_keys */
11
    protected static $context_keys = [
12
        'filters',
13
        'page',
14
        'per_page',
15
        'sort_by',
16
        'sort_order',
17
        'user',
18
        'with',
19
    ];
20
21
    /** @var array */
22
    protected $context;
23
24 27
    public static function create(array $context = []): self
25
    {
26 27
        return new self($context);
27
    }
28
29 27
    public function __construct(array $context = [])
30
    {
31 27
        $context = $this->validateContext($context);
32
33
        /** @var array */
34 27
        $this->context = $context;
35 27
    }
36
37 27
    public function validateContext(array $context)
38
    {
39 27
        $valid_keys = self::$context_keys;
40 27
        return Arr::only(
41 27
            $context,
42 27
            array_intersect(
43 27
                $valid_keys,
44 27
                array_keys($context)
45
            )
46
        );
47
    }
48
49 16
    public function filters(): array
50
    {
51 16
        return $this->get('filters', []);
52
    }
53
54 15
    public function page(): int
55
    {
56 15
        return $this->get('page', 1);
57
    }
58
59 15
    public function perPage(): int
60
    {
61 15
        return $this->get('per_page', 15);
62
    }
63
64 18
    public function paginate(): bool
65
    {
66 18
        return (bool) $this->get('page', false);
67
    }
68
69 19
    public function sortBy(): array
70
    {
71
        return [
72 19
            $this->get('sort_by', null),
73 19
            $this->get('sort_order', 'asc'),
74
        ];
75
    }
76
77 21
    public function with(): array
78
    {
79 21
        return $this->get('with', []);
80
    }
81
82 1
    public function user(): ?Model
83
    {
84 1
        return $this->get('user');
85
    }
86
87 1
    public function merge(array $values)
88
    {
89 1
        $values = $this->validateContext($values);
90
91 1
        $this->context = array_merge($this->context, $values);
92
93 1
        return $this;
94
    }
95
96 2
    public function exclude(array $keys)
97
    {
98 2
        $this->context = Arr::except($this->context, $keys);
99
100 2
        return $this;
101
    }
102
103 1
    public function toArray(): array
104
    {
105 1
        return array_merge($this->context, ['paginate' => $this->paginate()]);
106
    }
107
108 25
    protected function get($key, $default = null)
109
    {
110 25
        return $this->context[$key] ?? $default;
111
    }
112
}
113