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