Completed
Push — master ( 5e61a0...2e1bfd )
by Michael
04:48 queued 02:16
created

ArrayResourceContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 18
c 3
b 0
f 1
dl 0
loc 71
ccs 28
cts 28
cp 1
rs 10
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A get() 0 3 1
A __construct() 0 4 1
A merge() 0 5 1
A paginate() 0 3 1
A sortBy() 0 5 1
A with() 0 3 1
A user() 0 3 1
A filters() 0 3 1
A page() 0 3 1
A perPage() 0 3 1
A exclude() 0 5 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 */
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