Completed
Pull Request — master (#117)
by Simone
02:15
created

Rule::isNot()   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\Rulers;
13
14
class Rule
15
{
16
    const TYPE_OBJECT = 'object';
17
18
    const TYPE_SCALAR = 'scalar';
19
20
    const TYPE_CUSTOM = 'custom-validator';
21
22
    private static $avalableRuleTypes = [
23
        self::TYPE_OBJECT,
24
        self::TYPE_SCALAR,
25
        self::TYPE_CUSTOM,
26
    ];
27
28
    private $rule;
29
30
    private function __construct(array $rule) 
31
    {
32
        $this->rule = $rule;
33
    }
34
35
    public static function fromArray(array $rule)
36
    {
37
        if ([] === $rule) {
38
            throw new \LogicException(
39
                'rule type is not defined'
40
            );
41
        }
42
43
        return new self($rule);
44
    }
45
46
    public function ensureRuleNameIsValid()
47
    {
48
        if (!$this->isValid()) {
49
            throw new \Sensorario\Resources\Exceptions\InvalidTypeException(
50
                'Oops! Invalid configuration!!!'
51
                . 'Type `' . key($this->rule) . '` is not valid. '
52
                . 'Available types are ' . var_export(self::$avalableRuleTypes, true)
53
            );
54
        }
55
    }
56
57
    public function asArray() : array
58
    {
59
         return $this->rule;
60
    }
61
62
    public function isValid()
63
    {
64
        return in_array(key($this->rule), self::$avalableRuleTypes);
65
    }
66
67
    public function is($type)
68
    {
69
        return key($this->rule) === $type;
70
    }
71
72
    public function isNot($type)
73
    {
74
        return !$this->is($type);
75
    }
76
77
    public function isCustom()
78
    {
79
        return $this->is(Rule::TYPE_CUSTOM);
80
    }
81
82
    public function isNotCustom()
83
    {
84
        return !$this->isCustom();
85
    }
86
87
    public function isNotMail()
88
    {
89
        return 'email' != $this->getValue();
90
    }
91
92
    public function getRuleType()
93
    {
94
        return key($this->rule);
95
    }
96
97
    public function isObject()
98
    {
99
        return isset($this->rule[self::TYPE_OBJECT]);
100
    }
101
102
    public function getObjectType()
103
    {
104
        return $this->rule[self::TYPE_OBJECT];
105
    }
106
107
    public function getValue()
108
    {
109
        return current($this->rule);
110
    }
111
112
    public function getExpectedType()
113
    {
114
        $expectedType = $this->isObject()
115
            ? $this->getObjectType()
116
            : 'undefined';
117
118
        return $this->getRuleType() == self::TYPE_SCALAR
119
            ? $this->getValue()
120
            : $expectedType;
121
    }
122
123
    public function isValueNotAnObject()
124
    {
125
        return 'array' !== $this->getValue();
126
    }
127
}
128