[BUGFIX] Exception in EXT:form due to invalid array lookup
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Runtime / FormState.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Runtime;
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
18 use TYPO3\CMS\Core\Utility\ArrayUtility;
19
20 /**
21 * The current state of the form which is attached to the {@link FormRuntime}
22 * and saved in a session or the client.
23 *
24 * Scope: frontend
25 * **This class is NOT meant to be sub classed by developers.**
26 * @internal
27 */
28 class FormState
29 {
30
31 /**
32 * Constant which means that we are currently not on any page; i.e. the form
33 * has never rendered before.
34 */
35 const NOPAGE = -1;
36
37 /**
38 * The last displayed page index
39 *
40 * @var int
41 */
42 protected $lastDisplayedPageIndex = self::NOPAGE;
43
44 /**
45 * @var array
46 */
47 protected $formValues = [];
48
49 /**
50 * @return bool FALSE if the form has never been submitted before, TRUE otherwise
51 */
52 public function isFormSubmitted(): bool
53 {
54 return $this->lastDisplayedPageIndex !== self::NOPAGE;
55 }
56
57 /**
58 * @return int
59 */
60 public function getLastDisplayedPageIndex(): int
61 {
62 return $this->lastDisplayedPageIndex;
63 }
64
65 /**
66 * @param int $lastDisplayedPageIndex
67 * @return void
68 */
69 public function setLastDisplayedPageIndex(int $lastDisplayedPageIndex)
70 {
71 $this->lastDisplayedPageIndex = $lastDisplayedPageIndex;
72 }
73
74 /**
75 * @return array
76 */
77 public function getFormValues(): array
78 {
79 return $this->formValues;
80 }
81
82 /**
83 * @param string $propertyPath
84 * @param mixed $value
85 * @return void
86 */
87 public function setFormValue(string $propertyPath, $value)
88 {
89 $this->formValues = ArrayUtility::setValueByPath(
90 $this->formValues,
91 $propertyPath,
92 $value,
93 '.'
94 );
95 }
96
97 /**
98 * @param string $propertyPath
99 * @return mixed
100 */
101 public function getFormValue(string $propertyPath)
102 {
103 try {
104 return ArrayUtility::getValueByPath($this->formValues, $propertyPath, '.');
105 } catch (\RuntimeException $exception) {
106 return null;
107 }
108 }
109 }