Continued:
[core.git] / framework / main / classes / request / html / class_HtmlRequest.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Request;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Request\Requestable;
7 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
8
9 /**
10  * A concrete and secured HTML request class to make HTML requests more abstract
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  * @todo                Move out the cookie part to a seperate class, e.g. Cookie
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class HtmlRequest extends BaseRequest implements Requestable {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         private function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of this class and prepares it a little
45          *
46          * @return      $requestInstance        An instance of this class
47          */
48         public static final function createHtmlRequest () {
49                 // Create an instance
50                 $requestInstance = new HtmlRequest();
51
52                 // Prepare the HTML request data for usage
53                 $requestInstance->prepareRequestData();
54
55                 // Return the prepared instance
56                 return $requestInstance;
57         }
58
59         /**
60          * Checks if the request method is GET.
61          *
62          * @return      $isGet  Whether the request method is GET
63          */
64         public function isGetRequestMethod () {
65                 // Check it
66                 return ($this->getRequestMethod() == 'GET');
67         }
68
69         /**
70          * Checks if the request method is HEAD.
71          *
72          * @return      $isHead         Whether the request method is HEAD
73          */
74         public function isHeadRequestMethod () {
75                 // Check it
76                 return ($this->getRequestMethod() == 'HEAD');
77         }
78
79         /**
80          * Checks if the request method is POST.
81          *
82          * @return      $isPost         Whether the request method is POST
83          */
84         public function isPostRequestMethod () {
85                 // Check it
86                 return ($this->getRequestMethod() == 'POST');
87         }
88
89         /**
90          * Prepares the HTML request data for usage by currently copying
91          * $_REQUEST into a private attribute. Later on we can add more
92          * things for initialization here.
93          *
94          * @return      void
95          */
96         protected function prepareRequestData () {
97                 // Copy GET then POST data
98                 $this->setRequestData(array_merge($_GET, $_POST));
99         }
100
101         /**
102          * Getter for a header element or 'null' if header was not found
103          *
104          * @param       $headerName             Name of the header
105          * @return      $headerValue    Value of the header or 'null' if not found
106          */
107         public function getHeaderElement (string $headerName) {
108                 // Default return value on error
109                 $headerValue = NULL;
110
111                 // Construct the name
112                 $name = 'HTTP_' . strtoupper(StringUtils::convertDashesToUnderscores($headerName));
113
114                 // Does this header exist?
115                 if (isset($_SERVER[$name])) {
116                         $headerValue = $_SERVER[$name];
117                 }
118
119                 // Return the value
120                 return $headerValue;
121         }
122
123         /**
124          * Getter for request method. This getter might be useful for security filters
125          *
126          * @return      $requestMethod  Used request method
127          */
128         public final function getRequestMethod () {
129                 return $_SERVER['REQUEST_METHOD'];
130         }
131
132         /**
133          * Reads a cookie and returns it's value or null if not found
134          *
135          * @param       $cookieName             Name of cookie we shall read
136          * @return      $cookieValue    Value of cookie or null if not found
137          */
138         public final function readCookie (string $cookieName) {
139                 // Default is no cookie with that name found
140                 $cookieValue = NULL;
141
142                 // Is the cookie set?
143                 if (isset($_COOKIE[$cookieName])) {
144                         // Then get it
145                         $cookieValue = $_COOKIE[$cookieName];
146                 }
147
148                 // Return the value
149                 return $cookieValue;
150         }
151
152 }