Passed
Push — master ( 0756fe...706dc7 )
by Arthur
14:47
created

Property::assertTypeEquals()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 2
dl 0
loc 12
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Larapie\DataTransferObject;
6
7
use Larapie\DataTransferObject\Casters\TypeCaster;
8
use Larapie\DataTransferObject\Violations\InvalidPropertyTypeViolation;
9
use Larapie\DataTransferObject\Violations\PropertyRequiredViolation;
10
use ReflectionProperty;
11
use Symfony\Component\Validator\ConstraintViolationList;
12
use Symfony\Component\Validator\ConstraintViolationListInterface;
13
use Symfony\Component\Validator\ValidatorBuilder;
14
15
class Property
16
{
17
    /** @var PropertyData */
18
    protected $data;
19
20
    /** @var mixed */
21
    public $value;
22
23
    /** @var bool */
24
    protected $initialized = false;
25
26
    /** @var bool */
27
    protected $visible = true;
28
29
    /** @var ConstraintViolationListInterface|null */
30
    protected $violations;
31
32
    /**
33
     * PropertyValue constructor.
34
     * @param ReflectionProperty $reflection
35
     */
36
    public function __construct(ReflectionProperty $reflection)
37
    {
38
        $this->boot($reflection);
39
    }
40
41
    public function boot(ReflectionProperty $property)
42
    {
43
        $this->data = new PropertyData($property);
44
        $this->violations = new ConstraintViolationList();
45
        $this->initViolations();
46
    }
47
48
    protected function initViolations(){
49
        if (!$this->data->isOptional())
50
            $this->violations->add(new PropertyRequiredViolation());
0 ignored issues
show
Bug introduced by
The method add() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            $this->violations->/** @scrutinizer ignore-call */ 
51
                               add(new PropertyRequiredViolation());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
    }
52
53
    public function set($value): void
54
    {
55
        $value = (new TypeCaster($this->data->getType()))->cast($value);
56
        $this->value = $value;
57
        $this->initialized = true;
58
        $this->violations = $this->validate($value);
59
    }
60
61
    public function reset()
62
    {
63
        $this->value = null;
64
        $this->initialized = false;
65
        $this->initViolations();
66
    }
67
68
    public function isInitialized()
69
    {
70
        return $this->initialized;
71
    }
72
73
    public function validate($value): ?ConstraintViolationListInterface
74
    {
75
        $constraints = $this->data->getConstraints();
76
77
        $violations = (new ValidatorBuilder())->getValidator()->validate($value, $constraints);
78
79
        if (!$this->isInitialized() && !$this->data->isOptional()) {
80
            $violations->add(new PropertyRequiredViolation());
81
        }
82
        if (!$this->data->getType()->isValid($value)) {
83
            $violations->add(new InvalidPropertyTypeViolation($this->data->getType()->getTypes()));
84
        }
85
86
        return $violations;
87
    }
88
89
    public function isValid()
90
    {
91
        return $this->violations === null || $this->violations->count() <= 0;
92
    }
93
94
    /**
95
     * @return ConstraintViolationListInterface|null
96
     */
97
    public function getViolations(): ?ConstraintViolationListInterface
98
    {
99
        return $this->violations;
100
    }
101
102
    public function isImmutable()
103
    {
104
        return $this->data->isImmutable();
105
    }
106
107
    public function getValue()
108
    {
109
        return $this->value;
110
    }
111
112
    public function getName()
113
    {
114
        return $this->data->getName();
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function isVisible(): bool
121
    {
122
        return $this->visible;
123
    }
124
125
    /**
126
     * @param bool $visible
127
     */
128
    public function setVisible(bool $visible): void
129
    {
130
        $this->visible = $visible;
131
    }
132
133
134
}
135