|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
|
4
|
|
|
|
|
5
|
|
|
use Gedmo\Exception\InvalidMappingException; |
|
6
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractTrackingExtension |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var ExtensibleClassMetadata |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $classMetadata; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $fieldName; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $on; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string|array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $trackedFields; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $value; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return the name of the actual extension. |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract protected function getExtensionName(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
|
44
|
|
|
* @param string $fieldName |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->classMetadata = $classMetadata; |
|
49
|
|
|
$this->fieldName = $fieldName; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function onCreate() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->on('create'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function onUpdate() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->on('update'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array|string|null $fields |
|
70
|
|
|
* @param string|null $value |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function onChange($fields = null, $value = null) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->on('change', $fields, $value); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Execute the build process |
|
81
|
|
|
*/ |
|
82
|
|
|
public function build() |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->on === null) { |
|
85
|
|
|
throw new InvalidMappingException( |
|
86
|
|
|
"Field - [{$this->fieldName}] trigger 'on' is not one of [update, create, change] in class - {$this->classMetadata->name}"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (is_array($this->trackedFields) && $this->value !== null) { |
|
90
|
|
|
throw new InvalidMappingException("Extension does not support multiple value change-set detection yet."); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->classMetadata->appendExtension($this->getExtensionName(), [ |
|
94
|
|
|
$this->on => [ |
|
95
|
|
|
$this->makeConfiguration(), |
|
96
|
|
|
], |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $on |
|
102
|
|
|
* @param array|string|null $fields |
|
103
|
|
|
* @param string|null $value |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function on($on, $fields = null, $value = null) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->on = $on; |
|
110
|
|
|
$this->trackedFields = $fields; |
|
111
|
|
|
$this->value = $value; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns either the field name on "create" and "update", or the array configuration on "change". |
|
118
|
|
|
* |
|
119
|
|
|
* @return array|string |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function makeConfiguration() |
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->on == 'create' || $this->on == 'update') { |
|
124
|
|
|
return $this->fieldName; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return [ |
|
128
|
|
|
'field' => $this->fieldName, |
|
129
|
|
|
'trackedField' => $this->trackedFields, |
|
130
|
|
|
'value' => $this->value, |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|