Project

General

Profile

1 30 Manuela
<?php
2
3
/*
4
 * Copyright (C) 2017 Manuela v.d.Decken <manuela@isteam.de>
5
 *
6
 * DO NOT ALTER OR REMOVE COPYRIGHT OR THIS HEADER
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, version 2 of the License.
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
15
 * GNU General Public License 2 for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License 2
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
/**
21
 * Description of HttpRequester
22
 *
23
 * @package      Core package
24
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
25
 * @author       Manuela v.d.Decken <manuela@isteam.de>
26
 * @license      GNU General Public License 2.0
27
 * @version      0.0.1
28
 * @revision     $Id$
29
 * @since        File available since 04.10.2017
30
 * @deprecated   no / since 0000/00/00
31
 * @description  xxx
32
 */
33
//declare(strict_types = 1);
34
//declare(encoding = 'UTF-8');
35
36 31 Manuela
namespace bin\requester;
37 30 Manuela
38
use bin\interfaces\RequesterInterface;
39
40
/**
41
 * short description of class
42
 */
43
class HttpRequester implements RequesterInterface
44
{
45
    private $aParameters = [];
46
    private $aServer     = [];
47
    private $aHeaders    = [];
48
/**
49
 * construct and initialize the class
50
 */
51
    public function __construct()
52
    {
53
        $aServer = \filter_input_array(INPUT_SERVER);
54
        switch (\strtolower($aServer['REQUEST_METHOD'])):
55
            case 'post':
56
                $this->aParameters = \filter_input_array(INPUT_POST);
57
                break;
58
            case 'get':
59
                $this->aParameters = \filter_input_array(INPUT_GET);
60
                break;
61
            default:
62
                break;
63
        endswitch;
64 31 Manuela
        if ($this->aParameters == null) { $this->aParameters = []; }
65 30 Manuela
        foreach ($aServer as $sKey => $sValue) {
66 31 Manuela
            if (\substr_compare($sKey, 'HTTP_', 0, 5) === 0) {
67 30 Manuela
                $this->aHeaders[$sKey] = $sValue;
68
            } else {
69
                $this->aServer[$sKey] = $sValue;
70
            }
71
        }
72
    }
73
/**
74
 * returns a list of all parameters
75
 * @return array
76
 */
77
    public function getParamNames()
78
    {
79
        return \array_keys($this->aParameters);
80
    }
81
/**
82
 * check  if parameter exists
83
 * @param string $sParamName
84
 * @return bool
85
 */
86
    public function issetParam($sParamName)
87
    {
88 31 Manuela
89 30 Manuela
        return \array_key_exists($sParamName, $this->aParameters);
90
    }
91
/**
92
 * read a variable from commandline
93
 * @param string $sParamName
94
 * @param int $iFilterType
95
 * @param mixed $mOptions
96
 * @return mixed | null on error
97
 * @throws \InvalidArgumentException
98
 * @description the method is 100% compatible to the PHP function filter_var()
99
 */
100
    public function getParam($sParamName, $iFilterType = null, $mOptions = null)
101
    {
102
        $mRetval = null;
103
        try {
104
            if (!$this->issetParam($sParamName)) { throw new \Exception('error on getParam()'); }
105
            $mRetval = $this->aParameters[$sParamName];
106
            if (!\is_null($iFilterType)) {
107
                if (!\is_null($mOptions)) {
108
                    $mRetval = \filter_var($mRetval, $iFilterType, $mOptions);
109
                } else {
110
                    $mRetval = \filter_var($mRetval, $iFilterType);
111
                }
112
            }
113
        } catch (\Exception $ex) {
114
            $mRetval = null;
115
//            throw new \InvalidArgumentException('error on getParam()', $ex->getCode(), $ex);
116
        }
117
        return $mRetval;
118
    }
119
/**
120
 *
121
 * @param string $sHeaderName
122
 * @return mixed | null on error
123
 */
124
    public function issetHeader($sHeaderName)
125
    {
126
        $sVarname = 'HTTP_'.preg_replace('/^http_/i', '', $sHeaderName);
127
        return \array_key_exists($sVarname, $this->aHeaders);
128
    }
129
/**
130
 * get header vars ($_SERVER['HTTP_'*])
131
 * @param string $sHeaderName
132
 * @return mixed | null on error
133
 */
134
    public function getHeader($sHeaderName)
135
    {
136
        $sRetval = null;
137 31 Manuela
        $sVarname = 'HTTP_'.\preg_replace('/^http_/i', '', $sHeaderName);
138 30 Manuela
        if ($this->issetHeader($sVarname)) {
139
            $sRetval = $this->aHeaders($sVarname);
140
        }
141
        return $sRetval;
142
    }
143
/**
144
 *
145
 * @param string $sVarName
146
 * @return type
147
 */
148
    public function issetServerVar($sVarName)
149
    {
150
        return \array_key_exists($sVarName, $this->aServer);
151
    }
152
/**
153
 * get server vars excluding $_SERVER['HTTP_'*]
154
 * @param string $sVarName
155
 * @return mixed | null on error
156
 */
157
    public function getServerVar($sVarName)
158
    {
159
        $sRetval = null;
160
        if ($this->issetHeader($sVarName)) {
161
            $sRetval = $this->aHeaders($sVarName);
162
        }
163
        return $sRetval;
164
    }
165
166
}