Project

General

Profile

1
<?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: HttpRequester.php 30 2017-11-25 01:35:18Z Manuela $
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
namespace bin;
37

    
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
//        $this->aServer = \filter_input_array(INPUT_SERVER);
54
        $aServer = \filter_input_array(INPUT_SERVER);
55
        switch (\strtolower($aServer['REQUEST_METHOD'])):
56
            case 'post':
57
                $this->aParameters = \filter_input_array(INPUT_POST);
58
                break;
59
            case 'get':
60
                $this->aParameters = \filter_input_array(INPUT_GET);
61
                break;
62
            default:
63
                $this->aParameters = [];
64
                break;
65
        endswitch;
66
        foreach ($aServer as $sKey => $sValue) {
67
            if (substr_compare($sKey, 'HTTP_', 0, 5) === 0) {
68
                $this->aHeaders[$sKey] = $sValue;
69
            } else {
70
                $this->aServer[$sKey] = $sValue;
71
            }
72
        }
73
    }
74
/**
75
 * returns a list of all parameters
76
 * @return array
77
 */
78
    public function getParamNames()
79
    {
80
        return \array_keys($this->aParameters);
81
    }
82
/**
83
 * check  if parameter exists
84
 * @param string $sParamName
85
 * @return bool
86
 */
87
    public function issetParam($sParamName)
88
    {
89
        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
        $sVarname = 'HTTP_'.preg_replace('/^http_/i', '', $sHeaderName);
138
        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
}
(3-3/30)