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\Middleware\Item\Begin\Service; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Domain\Repository\FormMetadataRepository; |
17
|
|
|
use Romm\Formz\Form\FormInterface; |
18
|
|
|
use Romm\Formz\Middleware\Processor\MiddlewareProcessor; |
19
|
|
|
use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter; |
20
|
|
|
|
21
|
|
|
class FormService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var MiddlewareProcessor |
25
|
|
|
*/ |
26
|
|
|
private $processor; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var FormMetadataRepository |
30
|
|
|
*/ |
31
|
|
|
protected $formMetadataRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param MiddlewareProcessor $processor |
35
|
|
|
*/ |
36
|
|
|
public function __construct(MiddlewareProcessor $processor) |
37
|
|
|
{ |
38
|
|
|
$this->processor = $processor; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return FormInterface |
43
|
|
|
*/ |
44
|
|
|
public function getFormInstance() |
45
|
|
|
{ |
46
|
|
|
$formName = $this->processor->getFormObject()->getName(); |
47
|
|
|
$argument = $this->processor->getRequestArguments()->getArgument($formName); |
48
|
|
|
$formArray = $this->getFormArray($formName); |
49
|
|
|
|
50
|
|
|
return $argument->setValue($formArray)->getValue(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $formName |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
protected function getFormArray($formName) |
58
|
|
|
{ |
59
|
|
|
$formArray = $this->processor->getRequest()->getArgument($formName); |
60
|
|
|
$formArray = is_array($formArray) |
61
|
|
|
? $formArray |
62
|
|
|
: []; |
63
|
|
|
|
64
|
|
|
$identifier = $this->getFormIdentifier(); |
65
|
|
|
|
66
|
|
|
if ($identifier) { |
|
|
|
|
67
|
|
|
/* |
68
|
|
|
* Forcing the identity of the object, that will be handled |
69
|
|
|
* internally by Extbase. |
70
|
|
|
*/ |
71
|
|
|
$formArray['__identity'] = $identifier; |
72
|
|
|
$propertyMappingConfiguration = $this->processor->getRequestArguments()->getArgument($formName)->getPropertyMappingConfiguration(); |
73
|
|
|
$propertyMappingConfiguration->setTypeConverterOption( |
74
|
|
|
PersistentObjectConverter::class, |
75
|
|
|
PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED, |
76
|
|
|
true |
77
|
|
|
); |
78
|
|
|
} else { |
79
|
|
|
// Making sure no identity has been forced by the user. |
80
|
|
|
unset($formArray['__identity']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $formArray; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fetches the form identifier from the metadata, using the hash passed to |
88
|
|
|
* the request. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getFormIdentifier() |
93
|
|
|
{ |
94
|
|
|
$request = $this->processor->getRequest(); |
95
|
|
|
|
96
|
|
|
if ($request->hasArgument('fz-hash')) { |
97
|
|
|
$hash = $request->getArgument('fz-hash'); |
98
|
|
|
$metaData = $this->formMetadataRepository->findOneByHash($hash); |
99
|
|
|
|
100
|
|
|
if ($metaData) { |
101
|
|
|
return $metaData->getIdentifier(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param FormMetadataRepository $formMetadataRepository |
110
|
|
|
*/ |
111
|
|
|
public function injectFormMetadataRepository(FormMetadataRepository $formMetadataRepository) |
112
|
|
|
{ |
113
|
|
|
$this->formMetadataRepository = $formMetadataRepository; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: