1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2021-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Util\Forms;
24:
25: use SimpleID\Util\ArrayWrapper;
26:
27: /**
28: * A wrapper around an array used to store the state of a form.
29: *
30: * Form states are typically serialised through
31: * {@link SimpleID\Util\SecurityToken} and then passed around
32: */
33: class FormState extends ArrayWrapper {
34:
35: public const REQUEST_KEY = 'rq';
36: public const RESPONSE_KEY = 'rs';
37:
38: /**
39: * @param array<mixed> $data
40: */
41: public function __construct($data = []) {
42: if (!is_array($data)) $data = [];
43: parent::__construct($data);
44: }
45:
46: /**
47: * @return void
48: */
49: public function setRequest(ArrayWrapper $request) {
50: $this->offsetSet(self::REQUEST_KEY, $request);
51: }
52:
53: /**
54: * @return ArrayWrapper
55: */
56: public function getRequest() {
57: return $this->offsetGet(self::REQUEST_KEY);
58: }
59:
60: /**
61: * @return void
62: */
63: public function setResponse(ArrayWrapper $response) {
64: $this->offsetSet(self::RESPONSE_KEY, $response);
65: }
66:
67: /**
68: * @return ArrayWrapper
69: */
70: public function getResponse(): ArrayWrapper {
71: return $this->offsetGet(self::RESPONSE_KEY);
72: }
73:
74: /**
75: * Encodes the data
76: *
77: * @return array<mixed>
78: */
79: public function encode() {
80: $data = $this->container;
81:
82: if (isset($data[self::REQUEST_KEY]) && $data[self::REQUEST_KEY] instanceof ArrayWrapper) {
83: $data[self::REQUEST_KEY] = $data[self::REQUEST_KEY]->toArray();
84: }
85: if (isset($data[self::RESPONSE_KEY]) && $data[self::RESPONSE_KEY] instanceof ArrayWrapper) {
86: $data[self::RESPONSE_KEY] = $data[self::RESPONSE_KEY]->toArray();
87: }
88: return $data;
89: }
90:
91: /**
92: * Decodes the data and returns a FormState
93: *
94: * @param array<string, array<mixed>> $data
95: * @param string $request_class the name of the class representing the request
96: * @param string $response_class the name of the class representing the response
97: * @return FormState
98: */
99: public static function decode($data, $request_class = null, $response_class = null) {
100: if (!is_array($data)) return new FormState();
101:
102: if (isset($data[self::REQUEST_KEY]) && $request_class != null) {
103: $request = new $request_class($data[self::REQUEST_KEY]);
104: } else {
105: $request = null;
106: }
107: if ($request != null) $data[self::REQUEST_KEY] = $request;
108:
109: if (isset($data[self::RESPONSE_KEY]) && $response_class != null) {
110: $data[self::RESPONSE_KEY] = new $response_class($request, $data[self::RESPONSE_KEY]);
111: }
112: return new FormState($data);
113: }
114: }
115:
116: ?>