|
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\FormObject\Service; |
|
15
|
|
|
|
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
use ReflectionProperty; |
|
18
|
|
|
use Romm\Formz\Core\Core; |
|
19
|
|
|
use Romm\Formz\Form\FormObject\FormObjectStatic; |
|
20
|
|
|
use TYPO3\CMS\Extbase\Reflection\ReflectionService; |
|
21
|
|
|
|
|
22
|
|
|
class FormObjectProperties |
|
23
|
|
|
{ |
|
24
|
|
|
const IGNORE_PROPERTY = 'formz-ignore'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $ignoredProperties = ['validationData', 'uid', 'pid', '_localizedUid', '_languageUid', '_versionedUid']; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var FormObjectStatic |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $static; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $properties; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $publicProperties; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ReflectionService |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $reflectionService; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param FormObjectStatic $static |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(FormObjectStatic $static) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->static = $static; |
|
57
|
|
|
$this->reflectionService = Core::instantiate(ReflectionService::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns all the accessible properties of the form class: the public |
|
62
|
|
|
* properties and the ones that can be accessed with a getter method. |
|
63
|
|
|
* |
|
64
|
|
|
* If a class property must be excluded from this list, the following tag |
|
65
|
|
|
* must be added to the property: `@formz-ignore`. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getProperties() |
|
70
|
|
|
{ |
|
71
|
|
|
if (null === $this->properties) { |
|
72
|
|
|
$this->properties = []; |
|
73
|
|
|
$reflectionProperties = $this->reflectionService->getClassPropertyNames($this->static->getClassName()); |
|
74
|
|
|
|
|
75
|
|
|
$properties = array_filter( |
|
76
|
|
|
$reflectionProperties, |
|
77
|
|
|
function ($propertyName) { |
|
78
|
|
|
return $this->propertyShouldBeAdded($propertyName); |
|
79
|
|
|
} |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$this->properties = array_values($properties); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->properties; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $propertyName |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function propertyShouldBeAdded($propertyName) |
|
93
|
|
|
{ |
|
94
|
|
|
return false === in_array($propertyName, self::$ignoredProperties) |
|
95
|
|
|
&& false === $this->reflectionService->isPropertyTaggedWith($this->static->getClassName(), $propertyName, self::IGNORE_PROPERTY) |
|
96
|
|
|
&& $this->propertyCanBeAccessed($propertyName); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $propertyName |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function propertyCanBeAccessed($propertyName) |
|
104
|
|
|
{ |
|
105
|
|
|
if (in_array($propertyName, $this->getPublicProperties())) { |
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$propertyName = ucfirst($propertyName); |
|
110
|
|
|
|
|
111
|
|
|
$getterMethods = [ |
|
112
|
|
|
'__call', |
|
113
|
|
|
'get' . $propertyName, |
|
114
|
|
|
'is' . $propertyName, |
|
115
|
|
|
'has' . $propertyName |
|
116
|
|
|
]; |
|
117
|
|
|
|
|
118
|
|
|
foreach ($getterMethods as $method) { |
|
119
|
|
|
if ($this->reflectionService->hasMethod($this->static->getClassName(), $method)) { |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getPublicProperties() |
|
131
|
|
|
{ |
|
132
|
|
|
if (null === $this->publicProperties) { |
|
133
|
|
|
$classReflection = new ReflectionClass($this->static->getClassName()); |
|
134
|
|
|
$publicProperties = $classReflection->getProperties(ReflectionProperty::IS_PUBLIC); |
|
135
|
|
|
|
|
136
|
|
|
$this->publicProperties = array_map( |
|
137
|
|
|
function (ReflectionProperty $property) { |
|
138
|
|
|
return $property->getName(); |
|
139
|
|
|
}, |
|
140
|
|
|
$publicProperties |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->publicProperties; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public function __sleep() |
|
151
|
|
|
{ |
|
152
|
|
|
return ['static', 'properties']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Injects dependencies. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function __wakeup() |
|
159
|
|
|
{ |
|
160
|
|
|
$this->reflectionService = Core::instantiate(ReflectionService::class); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|