summaryrefslogtreecommitdiff
path: root/src/Http/Request.php
blob: fcfc2600f5a0e34539315ce0c00cd4ca16731e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace Engelsystem\Http;

use ErrorException;

class Request
{
    /** @var self */
    protected static $instance;

    /** @var array of POST data */
    protected $request;

    /** @var array of GET data */
    protected $query;

    /**
     * Initialize request
     */
    public function create()
    {
        $this->request = $_POST;
        $this->query = $_GET;
    }

    /**
     * Get GET input
     *
     * @param string $key
     * @param mixed  $default
     * @return mixed
     */
    public function get($key, $default = null)
    {
        if (!empty($this->query[$key])) {
            return $this->query[$key];
        }

        return $default;
    }

    /**
     * Get POST input
     *
     * @param string $key
     * @param mixed  $default
     * @return mixed
     */
    public function post($key, $default = null)
    {
        if (!empty($this->request[$key])) {
            return $this->request[$key];
        }

        return $default;
    }

    /**
     * Get input data
     *
     * @param string $key
     * @param mixed  $default
     * @return mixed
     */
    public function input($key, $default = null)
    {
        $data = $this->request + $this->query;

        if (isset($data[$key])) {
            return $data[$key];
        }

        return $default;
    }

    /**
     * Checks if the input exists
     *
     * @param string $key
     * @return bool
     */
    public function has($key)
    {
        $data = $this->request + $this->query;

        return isset($data[$key]);
    }

    /**
     * @return self
     * @throws ErrorException
     */
    public static function getInstance()
    {
        if (!self::$instance instanceof self) {
            throw new ErrorException('Request not initialized');
        }

        return self::$instance;
    }

    /**
     * @param self $instance
     */
    public static function setInstance($instance)
    {
        self::$instance = $instance;
    }
}