‪TYPO3CMS  10.4
FormData.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
22 
27 final class ‪FormData
28 {
32  private ‪$with = [];
33 
37  private ‪$withNoPrefix = [];
38 
42  private ‪$without = [];
43 
47  private ‪$withoutNoPrefix = [];
48 
52  private ‪$withChash = true;
53 
57  private ‪$formData;
58 
59  public function ‪__construct(array ‪$formData)
60  {
61  $this->formData = ‪$formData;
62  }
63 
64  public function ‪with(string $identifier, string $value): ‪FormData
65  {
66  $this->‪with[$identifier] = $value;
67  return $this;
68  }
69 
70  public function ‪withNoPrefix(string $identifier, string $value): ‪FormData
71  {
72  $this->‪withNoPrefix[$identifier] = $value;
73  return $this;
74  }
75 
76  public function ‪without(string $identifier): ‪FormData
77  {
78  $this->‪without[$identifier] = $identifier;
79  return $this;
80  }
81 
82  public function ‪withoutNoPrefix(string $identifier): ‪FormData
83  {
84  $this->‪withoutNoPrefix[$identifier] = $identifier;
85  return $this;
86  }
87 
88  public function ‪withChash(bool ‪$withChash): ‪FormData
89  {
90  $this->‪withChash = ‪$withChash;
91  return $this;
92  }
93 
94  public function ‪toPostRequest(InternalRequest $request): InternalRequest
95  {
96  $parsedBody = [];
97  $postStructure = $this->‪getPostStructure();
98  parse_str($postStructure, $parsedBody);
99  $request->getBody()->write($postStructure);
100  return $request
101  ->withMethod('POST')
102  ->withHeader('Content-Type', 'application/x-www-form-urlencoded')
103  ->withQueryParameters($this->‪getQueryStructure())
104  ->withParsedBody($parsedBody);
105  }
106 
107  public function ‪toGetRequest(InternalRequest $request, bool $withPostData = true): InternalRequest
108  {
109  $postStructure = [];
110  if ($withPostData) {
111  foreach (explode('&', urldecode($this->‪getPostStructure())) as $queryPart) {
112  [$key, $value] = explode('=', $queryPart, 2);
113  $postStructure[$key] = $value;
114  }
115  }
116  $queryParameters = array_replace_recursive(
117  $this->‪getQueryStructure(),
118  $postStructure
119  );
120 
121  $request->getBody()->write($this->‪getPostStructure());
122  return $request
123  ->withMethod('GET')
124  ->withQueryParameters($queryParameters);
125  }
126 
127  public function ‪toArray(): array
128  {
129  return ‪$this->formData;
130  }
131 
132  public function ‪getFormMarkup(): string
133  {
134  return $this->formData['DOMDocument']->saveHTML();
135  }
136 
137  public function ‪getHoneypotId(): ?string
138  {
139  return array_values(
140  array_filter(
141  $this->formData['elementData'],
142  function (array $elementData) {
143  return $elementData['__isHoneypot'];
144  }
145  )
146  )[0]['autocomplete'] ?? null;
147  }
148 
149  public function ‪getSessionId(): ?string
150  {
151  return array_values(
152  array_filter(
153  $this->formData['elementData'],
154  function (array $elementData) {
155  return str_ends_with($elementData['name'], '[__session]');
156  }
157  )
158  )[0]['value'] ?? null;
159  }
160 
161  private function ‪getQueryStructure(): array
162  {
163  $queryStructure = [];
164  $actionQueryData = $this->formData['actionQueryData'];
165  if ($this->‪withChash === false) {
166  unset($actionQueryData['cHash']);
167  }
168  $actionQuery = http_build_query($actionQueryData);
169 
170  foreach (explode('&', urldecode($actionQuery)) as $queryPart) {
171  [$key, $value] = explode('=', $queryPart, 2);
172  $queryStructure[$key] = $value;
173  }
174 
175  return $queryStructure;
176  }
177 
178  private function ‪getPostStructure(): string
179  {
180  $dataPrefix = '';
181  $postStructure = [];
182  foreach ($this->formData['elementData'] as $elementData) {
183  $nameStruct = [];
184  parse_str(sprintf('%s=%s', $elementData['name'], $elementData['value'] ?? ''), $nameStruct);
185  $postStructure = array_replace_recursive($postStructure, $nameStruct);
186 
187  if (str_ends_with($elementData['name'], '[__state]')) {
188  $prefix = key(‪ArrayUtility::flatten($nameStruct));
189  $prefixItems = explode('.', $prefix);
190  array_pop($prefixItems);
191  $dataPrefix = implode('.', $prefixItems) . '.';
192  }
193  }
194 
195  foreach ($this->‪with as $identifier => $value) {
196  $postStructure = ‪ArrayUtility::setValueByPath($postStructure, $dataPrefix . $identifier, $value, '.');
197  }
198 
199  foreach ($this->‪without as $identifier) {
200  $postStructure = ‪ArrayUtility::removeByPath($postStructure, $dataPrefix . $identifier, '.');
201  }
202 
203  $postStructure = array_replace_recursive(
204  $postStructure,
205  $this->‪withNoPrefix
206  );
207 
208  foreach ($this->‪withoutNoPrefix as $identifier) {
209  unset($postStructure[$identifier]);
210  }
211 
212  return http_build_query($postStructure);
213  }
214 }
‪TYPO3\CMS\Core\Utility\ArrayUtility\flatten
‪static array flatten(array $array, $prefix='', bool $keepDots=false)
Definition: ArrayUtility.php:486
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\with
‪with(string $identifier, string $value)
Definition: FormData.php:58
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\withoutNoPrefix
‪withoutNoPrefix(string $identifier)
Definition: FormData.php:76
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$with
‪array $with
Definition: FormData.php:31
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getSessionId
‪getSessionId()
Definition: FormData.php:143
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\__construct
‪__construct(array $formData)
Definition: FormData.php:53
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getQueryStructure
‪getQueryStructure()
Definition: FormData.php:155
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\toArray
‪toArray()
Definition: FormData.php:121
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$formData
‪array $formData
Definition: FormData.php:51
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getPostStructure
‪getPostStructure()
Definition: FormData.php:172
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$withChash
‪bool $withChash
Definition: FormData.php:47
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$withNoPrefix
‪array $withNoPrefix
Definition: FormData.php:35
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling
Definition: FormData.php:18
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$without
‪array $without
Definition: FormData.php:39
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\withNoPrefix
‪withNoPrefix(string $identifier, string $value)
Definition: FormData.php:64
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getFormMarkup
‪getFormMarkup()
Definition: FormData.php:126
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeByPath
‪static array removeByPath(array $array, $path, $delimiter='/')
Definition: ArrayUtility.php:316
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, $path, $value, $delimiter='/')
Definition: ArrayUtility.php:272
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\withChash
‪withChash(bool $withChash)
Definition: FormData.php:82
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData
Definition: FormData.php:28
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getHoneypotId
‪getHoneypotId()
Definition: FormData.php:131
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\toPostRequest
‪toPostRequest(InternalRequest $request)
Definition: FormData.php:88
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\without
‪without(string $identifier)
Definition: FormData.php:70
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$withoutNoPrefix
‪array $withoutNoPrefix
Definition: FormData.php:43
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\toGetRequest
‪toGetRequest(InternalRequest $request, bool $withPostData=true)
Definition: FormData.php:101