Passed
Push — master ( 52dac5...27e93f )
by BENOIT
02:09
created

PropertyChangeset::hasChanges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\DoctrineWatcher\Changeset;
4
5
abstract class PropertyChangeset
6
{
7
    public const CHANGESET_DEFAULT = 'scalar';
8
    public const CHANGESET_ITERABLE = 'iterable';
9
10
    /**
11
     * @var object
12
     */
13
    protected $entity;
14
15
    /**
16
     * @var string
17
     */
18
    protected $property;
19
20
    /**
21
     * @var mixed
22
     */
23
    protected $newValue;
24
25
    /**
26
     * @var mixed
27
     */
28
    protected $oldValue;
29
30
    /**
31
     * @var array
32
     */
33
    protected $additions = [];
34
35
    /**
36
     * @var array
37
     */
38
    protected $removals = [];
39
40
    /**
41
     * Changeset constructor.
42
     * @param object $entity
43
     * @param string $property
44
     */
45
    public function __construct($entity, string $property)
46
    {
47
        $this->entity = $entity;
48
        $this->property = $property;
49
    }
50
51
52
    abstract public function getType(): string;
53
54
    /**
55
     * @return object
56
     */
57
    public function getEntity()
58
    {
59
        return $this->entity;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getProperty(): string
66
    {
67
        return $this->property;
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73
    public function getOldValue()
74
    {
75
        return $this->oldValue;
76
    }
77
78
    /**
79
     * @return mixed
80
     */
81
    public function getNewValue()
82
    {
83
        return $this->newValue;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function hasChanges(): bool
90
    {
91
        return $this->oldValue !== $this->newValue;
92
    }
93
94
    /**
95
     * @return iterable
96
     */
97
    public function getAdditions(): iterable
98
    {
99
        if (self::CHANGESET_ITERABLE !== $this->getType()) {
100
            throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__));
101
        }
102
        return $this->additions;
103
    }
104
105
    /**
106
     * @return iterable
107
     */
108
    public function getRemovals(): iterable
109
    {
110
        if (self::CHANGESET_ITERABLE !== $this->getType()) {
111
            throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__));
112
        }
113
        return $this->removals;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    public function hasAdditions(): bool
120
    {
121
        if (self::CHANGESET_ITERABLE !== $this->getType()) {
122
            throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__));
123
        }
124
        return [] !== $this->additions;
125
    }
126
127
    /**
128
     * @return bool
129
     */
130
    public function hasRemovals(): bool
131
    {
132
        if (self::CHANGESET_ITERABLE !== $this->getType()) {
133
            throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__));
134
        }
135
        return [] !== $this->removals;
136
    }
137
}
138