1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Form\Definition; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectFactory; |
17
|
|
|
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait; |
18
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait; |
19
|
|
|
use Romm\Formz\Configuration\Configuration; |
20
|
|
|
use Romm\Formz\Configuration\ConfigurationState; |
21
|
|
|
use Romm\Formz\Exceptions\PropertyNotAccessibleException; |
22
|
|
|
|
23
|
|
|
class AbstractFormDefinitionComponent |
24
|
|
|
{ |
25
|
|
|
use MagicMethodsTrait { |
26
|
|
|
handlePropertyMagicMethod as handlePropertyMagicMethodInternal; |
27
|
|
|
} |
28
|
|
|
use ParentsTrait { |
29
|
|
|
attachParent as private attachParentInternal; |
30
|
|
|
attachParents as private attachParentsInternal; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $parentsAttached = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This method is used by setter methods, and other methods which goal is to |
40
|
|
|
* modify a property value. |
41
|
|
|
* |
42
|
|
|
* It checks that the definition is not frozen, and if it is actually frozen |
43
|
|
|
* an exception is thrown. |
44
|
|
|
* |
45
|
|
|
* @throws PropertyNotAccessibleException |
46
|
|
|
*/ |
47
|
|
|
protected function checkDefinitionFreezeState() |
48
|
|
|
{ |
49
|
|
|
if ($this->isDefinitionFrozen()) { |
50
|
|
|
$methodName = debug_backtrace()[1]['function']; |
51
|
|
|
|
52
|
|
|
throw PropertyNotAccessibleException::formDefinitionFrozenMethod(get_class($this), $methodName); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
protected function isDefinitionFrozen() |
60
|
|
|
{ |
61
|
|
|
return $this->getState() |
62
|
|
|
&& $this->getState()->isFrozen(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ConfigurationState |
67
|
|
|
*/ |
68
|
|
|
protected function getState() |
69
|
|
|
{ |
70
|
|
|
if ($this->hasParent(FormDefinition::class)) { |
71
|
|
|
return $this->getFirstParent(FormDefinition::class)->getState(); |
72
|
|
|
} elseif ($this->hasParent(Configuration::class)) { |
73
|
|
|
return $this->getFirstParent(Configuration::class)->getState(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Overrides the magic methods handling from the Configuration Object API. |
81
|
|
|
* |
82
|
|
|
* Blocks the parents feature once it has been used. |
83
|
|
|
* |
84
|
|
|
* @param object[] $parents |
85
|
|
|
*/ |
86
|
|
|
public function attachParents(array $parents) |
87
|
|
|
{ |
88
|
|
|
if (false === $this->parentsAttached) { |
89
|
|
|
$this->attachParentsInternal($parents); |
90
|
|
|
$this->parentsAttached = true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @see attachParents() |
96
|
|
|
* |
97
|
|
|
* @param object $parent |
98
|
|
|
* @param bool $direct |
99
|
|
|
*/ |
100
|
|
|
public function attachParent($parent, $direct = true) |
101
|
|
|
{ |
102
|
|
|
$this->checkDefinitionFreezeState(); |
103
|
|
|
|
104
|
|
|
if (false === $this->parentsAttached) { |
105
|
|
|
$this->attachParentInternal($parent, $direct); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Overrides the magic methods handling from the Configuration Object API: a |
111
|
|
|
* magic setter method must be accessible only for this API, otherwise an |
112
|
|
|
* exception must be thrown. |
113
|
|
|
* |
114
|
|
|
* @param string $property |
115
|
|
|
* @param string $type |
116
|
|
|
* @param array $arguments |
117
|
|
|
* @return mixed |
118
|
|
|
* @throws PropertyNotAccessibleException |
119
|
|
|
*/ |
120
|
|
|
protected function handlePropertyMagicMethod($property, $type, array $arguments) |
121
|
|
|
{ |
122
|
|
|
if ($type === 'set' |
123
|
|
|
&& $this->isPropertyAccessible($property) |
124
|
|
|
&& false === ConfigurationObjectFactory::getInstance()->isRunning() |
125
|
|
|
) { |
126
|
|
|
throw PropertyNotAccessibleException::formDefinitionFrozenProperty(get_class($this), $property); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->handlePropertyMagicMethodInternal($property, $type, $arguments); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $data |
134
|
|
|
* @param string $property |
135
|
|
|
* @param string $name |
136
|
|
|
*/ |
137
|
|
|
protected static function forceNameForProperty(&$data, $property, $name = 'name') |
138
|
|
|
{ |
139
|
|
|
if (isset($data[$property]) |
140
|
|
|
&& is_array($data[$property]) |
141
|
|
|
) { |
142
|
|
|
foreach ($data[$property] as $key => $entry) { |
143
|
|
|
$data[$property][$key][$name] = $key; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|