1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\DoctrineWatcher\Changeset; |
4
|
|
|
|
5
|
|
|
abstract class PropertyChangeset |
6
|
|
|
{ |
7
|
|
|
public const CHANGESET_DEFAULT = 'default'; |
8
|
|
|
public const CHANGESET_ITERABLE = 'iterable'; |
9
|
|
|
public const INSERT = 'insert'; |
10
|
|
|
public const UPDATE = 'update'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var mixed |
14
|
|
|
*/ |
15
|
|
|
protected $newValue; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
protected $oldValue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $additions = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $removals = []; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
abstract public function getType(): string; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function getOldValue() |
39
|
|
|
{ |
40
|
|
|
return $this->oldValue; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function getNewValue() |
47
|
|
|
{ |
48
|
|
|
return $this->newValue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function hasChanges(): bool |
55
|
|
|
{ |
56
|
|
|
return $this->oldValue !== $this->newValue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return iterable |
61
|
|
|
*/ |
62
|
|
|
public function getAdditions(): iterable |
63
|
|
|
{ |
64
|
|
|
if (self::CHANGESET_ITERABLE !== $this->getType()) { |
65
|
|
|
throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__)); |
66
|
|
|
} |
67
|
|
|
return $this->additions; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return iterable |
72
|
|
|
*/ |
73
|
|
|
public function getRemovals(): iterable |
74
|
|
|
{ |
75
|
|
|
if (self::CHANGESET_ITERABLE !== $this->getType()) { |
76
|
|
|
throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__)); |
77
|
|
|
} |
78
|
|
|
return $this->removals; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function hasAdditions(): bool |
85
|
|
|
{ |
86
|
|
|
if (self::CHANGESET_ITERABLE !== $this->getType()) { |
87
|
|
|
throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__)); |
88
|
|
|
} |
89
|
|
|
return [] !== $this->additions; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function hasRemovals(): bool |
96
|
|
|
{ |
97
|
|
|
if (self::CHANGESET_ITERABLE !== $this->getType()) { |
98
|
|
|
throw new \RuntimeException(sprintf('%s can only be called on iterable properties changesets.', __METHOD__)); |
99
|
|
|
} |
100
|
|
|
return [] !== $this->removals; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|