bab5320496f848394199f92842bb22ce5adb656f
[core.git] / framework / main / classes / request / class_BaseRequest.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Request;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
7
8 /**
9  * A general request class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30 abstract class BaseRequest extends BaseFrameworkSystem {
31         /**
32          * Array for the request data
33          */
34         private $requestData = [];
35
36         /**
37          * Whether this request is valid and can be further processed. The default is
38          * valid so make sure your intercepting filters sets this attribute to false
39          * when they need to intercept the data flow.
40          */
41         private $requestIsValid = true;
42
43         /**
44          * Protected constructor
45          *
46          * @param       $className      Name of the class
47          * @return      void
48          */
49         protected function __construct (string $className) {
50                 // Call parent constructor
51                 parent::__construct($className);
52         }
53
54         /**
55          * Checks whether a request element is set
56          * @param       $element        Name of the request element we want to check
57          * @return      $isSet          Whether the request element is set
58          */
59         public function isRequestElementSet (string $element) {
60                 // Is this element found?
61                 $isSet = isset($this->requestData[$element]);
62
63                 // Return result
64                 return $isSet;
65         }
66
67         /**
68          * Getter for request element or 'null' if element was not found
69          *
70          * @param       $element        Name of the request element we want to check
71          * @return      $value          Value of the found request element or 'null' if the
72          *                                              element was not found
73          */
74         public function getRequestElement (string $element) {
75                 // Initialize value
76                 $value = NULL;
77
78                 // Is the element set?
79                 if ($this->isRequestElementSet($element)) {
80                         // Get the bare value
81                         $value = $this->requestData[$element];
82
83                         // Secure it against attacks
84                         $value = htmlentities(strip_tags($value), ENT_QUOTES);
85                 } // END - if
86
87                 // Return the element's value
88                 return $value;
89         }
90
91         /**
92          * Setter for request elements
93          *
94          * @param       $element        Request element to se
95          * @param       $value          Value to set
96          * @return      void
97          */
98         public function setRequestElement (string $element, $value) {
99                 $this->requestData[$element] = $value;
100         }
101
102         /**
103          * Setter for request data array
104          *
105          * @param       $requestData    Request element to se
106          * @return      void
107          */
108         public function setRequestData (array $requestData) {
109                 $this->requestData = $requestData;
110         }
111
112         /**
113          * Wrapper method for array_key() function for the request data array
114          *
115          * @return      $array  An array containing all array keys to return
116          */
117         public function getParameterNames () {
118                 return array_keys($this->requestData);
119         }
120
121         /**
122          * Sets whether the request was valid (default: true)
123          *
124          * @param       $isValid        Whether the request is valid
125          * @return      void
126          */
127         public final function requestIsValid (bool $isValid = true) {
128                 $this->requestIsValid = $isValid;
129         }
130
131         /**
132          * Returns whether this request is valid
133          *
134          * @return      $requestIsValid         Whether this request is valid
135          */
136         public final function isRequestValid () {
137                 return $this->requestIsValid;
138         }
139
140 }