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\Exceptions\MethodNotFoundException; |
17
|
|
|
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait; |
18
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait; |
19
|
|
|
|
20
|
|
|
class AbstractFormDefinition |
21
|
|
|
{ |
22
|
|
|
use MagicMethodsTrait; |
23
|
|
|
use ParentsTrait { |
24
|
|
|
attachParent as private attachParentInternal; |
25
|
|
|
attachParents as private attachParentsInternal; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $parentsAttached = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @todo |
35
|
|
|
*/ |
36
|
|
|
protected function checkDefinitionFreezeState() |
37
|
|
|
{ |
38
|
|
|
if ($this->isDefinitionFrozen()) { |
39
|
|
|
$this->throwFrozenStateException(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
protected function isDefinitionFrozen() |
47
|
|
|
{ |
48
|
|
|
return $this->getDefinitionState() |
49
|
|
|
&& $this->getDefinitionState()->isDefinitionFrozen(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return FormDefinitionState |
54
|
|
|
*/ |
55
|
|
|
protected function getDefinitionState() |
56
|
|
|
{ |
57
|
|
|
if ($this instanceof FormDefinition) { |
58
|
|
|
return $this->getState(); |
|
|
|
|
59
|
|
|
} else { |
60
|
|
|
return $this->withFirstParent(FormDefinition::class, function (FormDefinition $formDefinition) { |
61
|
|
|
return $formDefinition->getState(); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param object[] $parents |
68
|
|
|
*/ |
69
|
|
|
public function attachParents(array $parents) |
70
|
|
|
{ |
71
|
|
|
if (false === $this->parentsAttached) { |
72
|
|
|
$this->attachParentsInternal($parents); |
|
|
|
|
73
|
|
|
$this->parentsAttached = true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param object $parent |
79
|
|
|
* @param bool $direct |
80
|
|
|
*/ |
81
|
|
|
public function attachParent($parent, $direct = true) |
82
|
|
|
{ |
83
|
|
|
if (false === $this->parentsAttached) { |
84
|
|
|
$this->attachParentInternal($parent, $direct); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @todo |
90
|
|
|
* |
91
|
|
|
* @param mixed $name |
92
|
|
|
* @param array $arguments |
93
|
|
|
* @return mixed |
94
|
|
|
* @throws MethodNotFoundException |
95
|
|
|
*/ |
96
|
|
|
public function __call($name, $arguments) |
97
|
|
|
{ |
98
|
|
|
if ($this->isDefinitionFrozen()) { |
99
|
|
|
if ('get' !== substr($name, 0, 3)) { |
100
|
|
|
$this->throwFrozenStateException(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
$result = $this->handleMagicMethods($name, $arguments); |
105
|
|
|
} catch (MethodNotFoundException $exception) { |
106
|
|
|
throw $exception; |
107
|
|
|
} |
108
|
|
|
} else { |
109
|
|
|
$result = $this->handleMagicMethods($name, $arguments); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @todo |
117
|
|
|
*/ |
118
|
|
|
private function throwFrozenStateException() |
119
|
|
|
{ |
120
|
|
|
throw new \Exception('FROZEN!'); // @todo |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: