Completed
Pull Request — master (#109)
by Simone
02:43
created

Resource::isRuleDefinedFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 RuntimeException;
15
use Sensorario\Resources\Validators\ResourcesValidator;
16
17
class Resource
18
    extends MagicResource
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "MagicResource" and comma; 1 found
Loading history...
19
    implements Interfaces\ResourceInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
20
{
21
    protected $allowed = [];
22
23
    protected $allowedValues = [];
24
25
    protected $mandatory = [];
26
27
    protected $defaults = [];
28
29
    protected $rules = [];
30
31
    protected $rewrites = [];
32
33
    protected $ranges = [];
34
35
    public function mandatory()
36
    {
37
        return $this->mandatory;
38
    }
39
40
    public function allowed()
41
    {
42
        return $this->allowed;
43
    }
44
45
    public function allowedValues()
46
    {
47
        return $this->allowedValues;
48
    }
49
50
    public function rules()
51
    {
52
        return $this->rules;
53
    }
54
55
    public function defaults()
56
    {
57
        return $this->defaults;
58
    }
59
60
    public function rewrites()
61
    {
62
        return $this->rewrites;
63
    }
64
65
    public function ranges()
66
    {
67
        return $this->ranges;
68
    }
69
70
    public function applyConfiguration(
71
        Configurator $configurator
72
    ) {
73
        $this->allowed       = $configurator->allowed();
0 ignored issues
show
Documentation Bug introduced by
The method allowed does not exist on object<Sensorario\Resources\Configurator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
        $this->mandatory     = $configurator->mandatory();
0 ignored issues
show
Documentation Bug introduced by
The method mandatory does not exist on object<Sensorario\Resources\Configurator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
        $this->defaults      = $configurator->defaults();
0 ignored issues
show
Documentation Bug introduced by
The method defaults does not exist on object<Sensorario\Resources\Configurator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
        $this->rules         = $configurator->rules();
0 ignored issues
show
Documentation Bug introduced by
The method rules does not exist on object<Sensorario\Resources\Configurator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
        $this->allowedValues = $configurator->allowedValues();
0 ignored issues
show
Documentation Bug introduced by
The method allowedValues does not exist on object<Sensorario\Resources\Configurator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
        $this->rewrites      = $configurator->rewrites();
79
        $this->ranges        = $configurator->ranges();
0 ignored issues
show
Documentation Bug introduced by
The method ranges does not exist on object<Sensorario\Resources\Configurator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
81
        if ($configurator->globals()) {
82
            $globals = $configurator->globals();
83
            if (isset($globals['allowed'])) {
84
                $this->allowed = array_merge(
85
                    $this->allowed,
86
                    $globals['allowed']
87
                );
88
            }
89
        }
90
    }
91
92
    public function isRuleDefinedFor($ruleName)
93
    {
94
        return isset($this->rules()[$ruleName]);
95
    }
96
97
    public function isRuleNotDefinedFor($ruleName)
98
    {
99
        return !$this->isRuleDefinedFor($ruleName);
100
    }
101
102
    public function getRule($ruleName) : Rule
103
    {
104
        return Rule::fromArray(
105
            $this->rules()[$ruleName]
106
        );
107
    }
108
109
    public function isFieldNumeric($fieldName)
110
    {
111
        return is_numeric($this->get($fieldName));
112
    }
113
114
    public function isFieldString($fieldName)
115
    {
116
        return is_string($this->get($fieldName));
117
    }
118
119
    public function isFieldNumericButString($fieldName)
120
    {
121
        return $this->isFieldNumeric($fieldName)
122
            && $this->isFieldString($fieldName);
123
    }
124
125
    public function getFieldType($fieldName)
126
    {
127
        return gettype($this->get($fieldName));
128
    }
129
}
130