1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 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\Base;
24:
25: use JsonSerializable;
26:
27: /**
28: * Stores the state of a request.
29: *
30: * Request states are typically serialised through
31: * {@link SimpleID\Util\SecurityToken} and then passed in a URL,
32: * to be consumed by the {@link IndexModule::continue()} function.
33: */
34: class RequestState implements JsonSerializable {
35: /**
36: * The HTTP method.
37: *
38: * @var string
39: */
40: private $method = null;
41:
42: /**
43: * the FatFree routing path
44: *
45: * @var string
46: */
47: private $route = null;
48:
49: /**
50: * The request parameters
51: *
52: * @var array<string, mixed>
53: */
54: private $params = null;
55:
56: /**
57: * Creates a new request state.
58: *
59: * Call the constructor with an empty array to create a blank request
60: * state. Alternatively, the constructor can be supplied with the payload
61: * from a security token to populate the request state.
62: *
63: * The security token payload can contain the following keys
64: *
65: * - mt the HTTP method (e.g. GET, POST)
66: * - rt the FatFree routing path
67: * - rq an array containing the request parameters
68: *
69: * @param array<string, mixed> $payload the payload from the security token
70: */
71: public function __construct(array $payload = []) {
72: if (isset($payload['mt'])) $this->method = $payload['mt'];
73: if (isset($payload['rt'])) $this->route = $payload['rt'];
74: if (isset($payload['rq'])) $this->params = $payload['rq'];
75: }
76:
77: /**
78: * Gets the HTTP method. If the HTTP method is not set, this method
79: * returns `GET`.
80: *
81: * @return string the HTTP method.
82: */
83: public function getMethod(): string {
84: return ($this->method == null) ? 'GET' : $this->method;
85: }
86:
87: /**
88: * Sets the HTTP method.
89: *
90: * @param string $method the HTTP method
91: */
92: public function setMethod(string $method): self {
93: $this->method = $method;
94: return $this;
95: }
96:
97: /**
98: * Gets the FatFree routing path. If the routing path is not set,
99: * this method returns `/`.
100: *
101: * @return string the routing path
102: */
103: public function getRoute(): string {
104: return ($this->route == null) ? '/' : $this->route;
105: }
106:
107: /**
108: * Sets the routing path
109: *
110: * @param string $route the routing path
111: */
112: public function setRoute(string $route): self {
113: $this->route = $route;
114: return $this;
115: }
116:
117: /**
118: * Gets the request parameters.
119: *
120: * @return array<string, mixed>
121: */
122: public function getParams(): array {
123: return ($this->params == null) ? [] : $this->params;
124: }
125:
126: /**
127: * Sets the request parameters.
128: *
129: * @param array<string, mixed> $params
130: */
131: public function setParams(array $params): self {
132: $this->params = $params;
133: return $this;
134: }
135:
136: /**
137: * Returns the FatFree route pattern.
138: *
139: * The FatFree route pattern contains the HTTP method and the
140: * routing path.
141: *
142: * The pattern can be used with the `mock()` method in the
143: * FatFree Framework.
144: *
145: * @return string the routing pattern
146: */
147: public function toF3RoutePattern(): string {
148: return $this->getMethod() . ' ' . $this->getRoute();
149: }
150:
151: /**
152: * {@inheritdoc}
153: */
154: public function jsonSerialize(): mixed {
155: $data = [];
156: if ($this->method != null) $data['mt'] = $this->method;
157: if ($this->route != null) $data['rt'] = $this->route;
158: if ($this->params != null) $data['rq'] = $this->params;
159: return $data;
160: }
161: }
162:
163: ?>