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