‪TYPO3CMS  ‪main
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 {
29  private array ‪$with = [];
30  private array ‪$withNoPrefix = [];
31  private array ‪$without = [];
32  private array ‪$withoutNoPrefix = [];
33  private bool ‪$withChash = true;
34 
35  public function ‪__construct(private array $formData) {}
36 
37  public function ‪with(string ‪$identifier, string $value): ‪FormData
38  {
39  $this->‪with[‪$identifier] = $value;
40  return $this;
41  }
42 
43  public function ‪withNoPrefix(string ‪$identifier, string $value): ‪FormData
44  {
45  $this->‪withNoPrefix[‪$identifier] = $value;
46  return $this;
47  }
48 
49  public function ‪without(string ‪$identifier): ‪FormData
50  {
52  return $this;
53  }
54 
56  {
58  return $this;
59  }
60 
61  public function ‪withChash(bool ‪$withChash): ‪FormData
62  {
63  $this->‪withChash = ‪$withChash;
64  return $this;
65  }
66 
67  public function ‪toPostRequest(InternalRequest $request): InternalRequest
68  {
69  $parsedBody = [];
70  $postStructure = $this->‪getPostStructure();
71  parse_str($postStructure, $parsedBody);
72  $request->getBody()->write($postStructure);
73  return $request
74  ->withMethod('POST')
75  ->withHeader('Content-Type', 'application/x-www-form-urlencoded')
76  ->withQueryParameters($this->‪getQueryStructure())
77  ->withParsedBody($parsedBody);
78  }
79 
80  public function ‪toGetRequest(InternalRequest $request, bool $withPostData = true): InternalRequest
81  {
82  $postStructure = [];
83  if ($withPostData) {
84  foreach (explode('&', urldecode($this->‪getPostStructure())) as $queryPart) {
85  [$key, $value] = explode('=', $queryPart, 2);
86  $postStructure[$key] = $value;
87  }
88  }
89  $queryParameters = array_replace_recursive(
90  $this->‪getQueryStructure(),
91  $postStructure
92  );
93 
94  $request->getBody()->write($this->‪getPostStructure());
95  return $request
96  ->withMethod('GET')
97  ->withQueryParameters($queryParameters);
98  }
99 
100  public function ‪toArray(): array
101  {
102  return $this->formData;
103  }
104 
105  public function ‪getFormMarkup(): string
106  {
107  return $this->formData['DOMDocument']->saveHTML();
108  }
109 
110  public function ‪getHoneypotId(): ?string
111  {
112  return array_values(
113  array_filter(
114  $this->formData['elementData'],
115  fn($elementData) => $elementData['__isHoneypot']
116  )
117  )[0]['autocomplete'] ?? null;
118  }
119 
120  public function ‪getSessionId(): ?string
121  {
122  return array_values(
123  array_filter(
124  $this->formData['elementData'],
125  fn($elementData) => str_ends_with($elementData['name'], '[__session]')
126  )
127  )[0]['value'] ?? null;
128  }
129 
130  private function ‪getQueryStructure(): array
131  {
132  $queryStructure = [];
133  $actionQueryData = $this->formData['actionQueryData'];
134  if ($this->‪withChash === false) {
135  unset($actionQueryData['cHash']);
136  }
137  $actionQuery = http_build_query($actionQueryData);
138 
139  foreach (explode('&', urldecode($actionQuery)) as $queryPart) {
140  [$key, $value] = explode('=', $queryPart, 2);
141  $queryStructure[$key] = $value;
142  }
143 
144  return $queryStructure;
145  }
146 
147  private function ‪getPostStructure(): string
148  {
149  $dataPrefix = '';
150  $postStructure = [];
151  foreach ($this->formData['elementData'] as $elementData) {
152  $nameStruct = [];
153  parse_str(sprintf('%s=%s', $elementData['name'], $elementData['value'] ?? ''), $nameStruct);
154  $postStructure = array_replace_recursive($postStructure, $nameStruct);
155 
156  if (str_ends_with($elementData['name'], '[__state]')) {
157  $prefix = key(‪ArrayUtility::flatten($nameStruct));
158  $prefixItems = explode('.', $prefix);
159  array_pop($prefixItems);
160  $dataPrefix = implode('.', $prefixItems) . '.';
161  }
162  }
163 
164  foreach ($this->‪with as ‪$identifier => $value) {
165  $postStructure = ‪ArrayUtility::setValueByPath($postStructure, $dataPrefix . ‪$identifier, $value, '.');
166  }
167 
168  foreach ($this->‪without as ‪$identifier) {
169  $postStructure = ‪ArrayUtility::removeByPath($postStructure, $dataPrefix . ‪$identifier, '.');
170  }
171 
172  $postStructure = array_replace_recursive(
173  $postStructure,
174  $this->‪withNoPrefix
175  );
176 
177  foreach ($this->‪withoutNoPrefix as ‪$identifier) {
178  unset($postStructure[‪$identifier]);
179  }
180 
181  return http_build_query($postStructure);
182  }
183 }
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\with
‪with(string $identifier, string $value)
Definition: FormData.php:37
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\withoutNoPrefix
‪withoutNoPrefix(string $identifier)
Definition: FormData.php:55
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeByPath
‪static array removeByPath(array $array, string $path, string $delimiter='/')
Definition: ArrayUtility.php:303
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$with
‪array $with
Definition: FormData.php:29
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getSessionId
‪getSessionId()
Definition: FormData.php:120
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getQueryStructure
‪getQueryStructure()
Definition: FormData.php:130
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\toArray
‪toArray()
Definition: FormData.php:100
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getPostStructure
‪getPostStructure()
Definition: FormData.php:147
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$withChash
‪bool $withChash
Definition: FormData.php:33
‪TYPO3\CMS\Core\Utility\ArrayUtility\flatten
‪static flatten(array $array, string $prefix='', bool $keepDots=false)
Definition: ArrayUtility.php:469
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$withNoPrefix
‪array $withNoPrefix
Definition: FormData.php:30
‪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:31
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\withNoPrefix
‪withNoPrefix(string $identifier, string $value)
Definition: FormData.php:43
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getFormMarkup
‪getFormMarkup()
Definition: FormData.php:105
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\withChash
‪withChash(bool $withChash)
Definition: FormData.php:61
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData
Definition: FormData.php:28
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getHoneypotId
‪getHoneypotId()
Definition: FormData.php:110
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\toPostRequest
‪toPostRequest(InternalRequest $request)
Definition: FormData.php:67
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\__construct
‪__construct(private array $formData)
Definition: FormData.php:35
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, string|array|\ArrayAccess $path, mixed $value, string $delimiter='/')
Definition: ArrayUtility.php:261
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\without
‪without(string $identifier)
Definition: FormData.php:49
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\$withoutNoPrefix
‪array $withoutNoPrefix
Definition: FormData.php:32
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\toGetRequest
‪toGetRequest(InternalRequest $request, bool $withPostData=true)
Definition: FormData.php:80