AbstractResourceRuleSet   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 59
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A provideUpdateRules() 0 3 1
A updateRules() 0 6 1
A provideCreationRules() 0 3 1
A getRules() 0 7 2
A rules() 0 3 1
A creationRules() 0 6 1
A provideRules() 0 3 1
A getRulesForKey() 0 5 1
A guardAgainstInvalidKey() 0 4 2
1
<?php
2
3
namespace Telkins\Validation;
4
5
use OutOfBoundsException;
6
use Telkins\Validation\Contracts\ResourceRuleSetContract;
7
8
abstract class AbstractResourceRuleSet implements ResourceRuleSetContract
9
{
10
    public function rules(?string $key = null) : array
11
    {
12
        return $this->getRules($this->provideRules(), $key);
13
    }
14
15
    public function creationRules(?string $key = null) : array
16
    {
17
        return $this->getRules(array_merge_recursive(
18
            $this->provideCreationRules(),
19
            $this->provideRules(),
20
        ), $key);
21
    }
22
23
    public function updateRules(?string $key = null) : array
24
    {
25
        return $this->getRules(array_merge_recursive(
26
            $this->provideUpdateRules(),
27
            $this->provideRules(),
28
        ), $key);
29
    }
30
31
    protected function getRules(array $rules, ?string $key) : array
32
    {
33
        if (null === $key) {
34
            return $rules;
35
        }
36
37
        return $this->getRulesForKey($rules, $key);
38
    }
39
40
    protected function getRulesForKey(array $rules, string $key) : array
41
    {
42
        $this->guardAgainstInvalidKey($key, $rules);
43
44
        return $rules[$key];
45
    }
46
47
    protected function guardAgainstInvalidKey(string $key, array $rules)
48
    {
49
        if (! array_key_exists($key, $rules)) {
50
            throw new OutOfBoundsException("invalid key, '{$key}'");
51
        }
52
    }
53
54
    protected function provideRules() : array
55
    {
56
        return [];
57
    }
58
59
    protected function provideCreationRules() : array
60
    {
61
        return [];
62
    }
63
64
    protected function provideUpdateRules() : array
65
    {
66
        return [];
67
    }
68
}
69