Resource   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 2
cbo 3
dl 0
loc 111
ccs 45
cts 45
cp 1
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A mandatory() 0 4 1
A allowed() 0 4 1
A allowedValues() 0 4 1
A rules() 0 4 1
A defaults() 0 4 1
A rewrites() 0 4 1
A ranges() 0 4 1
A applyConfiguration() 0 21 3
A isRuleDefinedFor() 0 4 1
A isRuleNotDefinedFor() 0 4 1
A getRule() 0 6 1
A isFieldNumeric() 0 4 1
A isFieldString() 0 4 1
A isFieldNumericButString() 0 5 2
A getFieldType() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources;
13
14
use Sensorario\Resources\Interfaces\ResourceInterface;
15
use Sensorario\Resources\Rulers\Rule;
16
17
class Resource extends MagicResource implements ResourceInterface
18
{
19
    protected $allowed = [];
20
21
    protected $allowedValues = [];
22
23
    protected $mandatory = [];
24
25
    protected $defaults = [];
26
27
    protected $rules = [];
28
29
    protected $rewrites = [];
30
31
    protected $ranges = [];
32
33 28
    public function mandatory()
34
    {
35 28
        return $this->mandatory;
36
    }
37
38 22
    public function allowed()
39
    {
40 22
        return $this->allowed;
41
    }
42
43 17
    public function allowedValues()
44
    {
45 17
        return $this->allowedValues;
46
    }
47
48 28
    public function rules()
49
    {
50 28
        return $this->rules;
51
    }
52
53 4
    public function defaults()
54
    {
55 4
        return $this->defaults;
56
    }
57
58 19
    public function rewrites()
59
    {
60 19
        return $this->rewrites;
61
    }
62
63 19
    public function ranges()
64
    {
65 19
        return $this->ranges;
66
    }
67
68 25
    public function applyConfiguration(
69
        Configurator $configurator
70
    ) {
71 25
        $this->allowed       = $configurator->allowed();
72 25
        $this->mandatory     = $configurator->mandatory();
73 25
        $this->defaults      = $configurator->defaults();
74 25
        $this->rules         = $configurator->rules();
75 25
        $this->allowedValues = $configurator->allowedValues();
76 25
        $this->rewrites      = $configurator->rewrites();
77 25
        $this->ranges        = $configurator->ranges();
78
79 25
        if ($configurator->globals()) {
80 1
            $globals = $configurator->globals();
81 1
            if (isset($globals['allowed'])) {
82 1
                $this->allowed = array_merge(
83 1
                    $this->allowed,
84 1
                    $globals['allowed']
85
                );
86
            }
87
        }
88 25
    }
89
90 32
    public function isRuleDefinedFor($ruleName)
91
    {
92 32
        return isset($this->rules()[$ruleName]);
93
    }
94
95 32
    public function isRuleNotDefinedFor($ruleName)
96
    {
97 32
        return !$this->isRuleDefinedFor($ruleName);
98
    }
99
100 12
    public function getRule($ruleName) : Rule
101
    {
102 12
        return Rule::fromArray(
103 12
            $this->rules()[$ruleName]
104
        );
105
    }
106
107 2
    public function isFieldNumeric($fieldName)
108
    {
109 2
        return is_numeric($this->get($fieldName));
110
    }
111
112 1
    public function isFieldString($fieldName)
113
    {
114 1
        return is_string($this->get($fieldName));
115
    }
116
117 2
    public function isFieldNumericButString($fieldName)
118
    {
119 2
        return $this->isFieldNumeric($fieldName)
120 2
            && $this->isFieldString($fieldName);
121
    }
122
123 6
    public function getFieldType($fieldName)
124
    {
125 6
        return gettype($this->get($fieldName));
126
    }
127
}
128