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