Ruler::fromResourceAndKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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
use Sensorario\Resources\Resource;
15
use Sensorario\Resources\Validators\Interfaces\Validator;
16
use Egulias\EmailValidator\EmailValidator;
17
use Egulias\EmailValidator\Validation\DNSCheckValidation;
18
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
19
use Egulias\EmailValidator\Validation\RFCValidation;
20
21
class Ruler
22
{
23
    private $resource;
24
25
    private $rule;
26
27
    private $fieldName;
28
29 13
    public function __construct(Resource $resource = null, $fieldName = null)
30
    {
31 13
        $this->resource = $resource;
32 13
        $this->fieldName = $fieldName;
33 13
    }
34
35 12
    public static function fromResourceAndKey(Resource $resource, $fieldName) : Ruler
36
    {
37 12
        return new static($resource, $fieldName);
38
    }
39
40 13
    public function getRuleFromResource()
41
    {
42 13
        if (!$this->resource) {
43 1
            throw new \Sensorario\Resources\Exceptions\UndefinedResourceException(
44 1
                'Oops! Cant get rule without resource'
45
            );
46
        }
47
48 12
        $this->rule = $this->resource->getRule($this->fieldName);
49 12
    }
50
51 10
    private function isNotCustom()
52
    {
53 10
        $this->getRuleFromResource();
54
55 10
        return $this->rule->isNotCustom();
56
    }
57
58 10
    public function typeMismatch()
59
    {
60 10
        return $this->isNotCustom()
61 10
            && $this->resource->getFieldType($this->fieldName) !== $this->rule->getRuleType();
62
    }
63
64 8
    public function classMismatch()
65
    {
66 8
        return $this->isNotCustom()
67 8
            && !($this->resource->get($this->fieldName) instanceof \Sensorario\Resources\Resource)
68
            && (
69 3
                is_object($this->resource->get($this->fieldName)) && 
70 8
                get_class($this->resource->get($this->fieldName)) != $this->rule->getValue()
71
            );
72
    }
73
74 10
    public function ensureTypeIsValid()
75
    {
76 10
        if ($this->typeMismatch()) {
77 2
            if ($this->resource->isFieldNumericButString($this->fieldName)) {
78 1
                throw new \Sensorario\Resources\Exceptions\WrongPropertyValueException(
79 1
                    'Property `' . $this->fieldName . '` must be an integer!'
80
                );
81
            }
82
83 1
            if ($this->rule->getExpectedType() == 'array') {
84 1
                if (gettype($this->resource->get($this->fieldName)) != 'array') {
85 1
                    throw new \Sensorario\Resources\Exceptions\AttributeTypeException(
86 1
                        'Attribute `' . $this->fieldName
87 1
                        . '` must be of type '
88 1
                        . '`' . $this->rule->getExpectedType() . '`'
89
                    );
90
                }
91
            }
92
        }
93 8
    }
94
95 8
    public function ensureClassIsValid()
96
    {
97 8
        if ($this->classMismatch()) {
98 1
            if ($this->rule->isValueNotAnObject()) {
99 1
                throw new \Sensorario\Resources\Exceptions\NotObjectTypeFoundException(
100 1
                    'Attribute `' . $this->fieldName
101 1
                    . '` must be an object of type ' . $this->rule->getValue()
102
                );
103
            }
104
        }
105 7
    }
106
107 6
    public function ensureRuleTypeIsEmail()
108
    {
109 6
        if (!$this->rule) {
110 6
            $this->getRuleFromResource();
111
        }
112
113 6
        if ($this->rule->isNotMail()) {
114 1
            throw new \Sensorario\Resources\Exceptions\InvalidCustomValidatorException(
115 1
                'Oops! `' . $this->rule->getRuleType() . '` custom validator is not available. Only email is.'
116
            );
117
        }
118 5
    }
119
}
120