Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
10 / 12
CRAP
42.22% covered (danger)
42.22%
19 / 45
Request
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
10 / 12
135.10
42.22% covered (danger)
42.22%
19 / 45
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 secure
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 make
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 query
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 args
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 request
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 server
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 file
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 cookie
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 initialize
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
7 / 7
 ip
0.00% covered (danger)
0.00%
0 / 1
126.31
7.41% covered (danger)
7.41%
2 / 27
 local
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
declare(strict_types=1);
namespace Eywa\Http\Request {
    use Eywa\Http\Parameter\Bag;
    use Eywa\Http\Parameter\Uploaded\UploadedFile;
    class Request
    {
        /**
         *
         * $_GET values
         *
         */
        private Bag $query;
        /**
         *
         * $_POST values
         *
         */
        private Bag $request;
        /**
         *
         * $_COOKIE values
         *
         */
        private Bag $cookie;
        /**
         *
         * $_FILES value
         *
         */
        private UploadedFile $file;
        /**
         *
         * $_SERVER value
         *
         */
        private Bag $server;
        /**
         *
         * All router args
         *
         */
        private Bag $args;
        /**
         * Request constructor.
         *
         * @param array<mixed> $request
         * @param array<mixed> $query
         * @param array<mixed> $cookies
         * @param array<mixed> $files
         * @param array<mixed> $server
         * @param array<mixed> $router_args
         *
         *
         */
        public function __construct(
            array $request = [],
            array $query = [],
            array $cookies = [],
            array $files = [],
            array $server = [],
            array $router_args = []
        ) {
            $this->initialize($query, $request, $cookies, $files, $server, $router_args);
        }
        /**
         * @return bool
         *
         */
        public function secure(): bool
        {
            return cli() ? false : intval($this->server->get('SERVER_PORT')) === 443;
        }
        /**
         *
         * Creates a new request with values from PHP's super globals.
         *
         * @param array<mixed> $args
         *
         * @return Request
         *
         *
         */
        public static function make(array $args = []): Request
        {
            return new self($_POST, $_GET, $_COOKIE, $_FILES, $_SERVER, $args);
        }
        /**
         *
         * Get $_GET value
         *
         * @return Bag
         *
         */
        public function query(): Bag
        {
            return $this->query;
        }
        /**
         *
         * Get routes args
         *
         * @return Bag
         *
         */
        public function args(): Bag
        {
            return $this->args;
        }
        /**
         *
         * Get $_GET value
         *
         * @return Bag
         *
         */
        public function request(): Bag
        {
            return $this->request;
        }
        /**
         *
         * Get $_GET value
         *
         * @return Bag
         *
         */
        public function server(): Bag
        {
            return $this->server;
        }
        /**
         *
         * Get $_GET value
         *
         * @return UploadedFile
         *
         */
        public function file(): UploadedFile
        {
            return $this->file;
        }
        /**
         *
         * Get $_GET value
         *
         * @return Bag
         *
         */
        public function cookie(): Bag
        {
            return $this->cookie;
        }
        /**
         * @param array<mixed> $query
         * @param array $request
         * @param array<mixed> $cookies
         * @param array<mixed> $files
         * @param array<mixed> $server
         * @param array<mixed> $router_args
         *
         */
        private function initialize(
            array $query,
            array $request,
            array $cookies,
            array $files,
            array $server,
            array $router_args
        ): void {
            $this->query = new Bag($query);
            $this->request = new Bag($request);
            $this->cookie = new Bag($cookies);
            $this->file = new UploadedFile($files);
            $this->server = new Bag($server);
            $this->args = new Bag($router_args);
        }
        /**
         * @return mixed
         */
        public function ip()
        {
            if (cli()) {
                return LOCALHOST_IP;
            }
            $ip = $_SERVER['REMOTE_ADDR'];
            if (
                isset($_SERVER['HTTP_X_FORWARDED_FOR'])
                && preg_match_all(
                    '#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s',
                    $_SERVER['HTTP_X_FORWARDED_FOR'],
                    $matches
                )
            ) {
                foreach ($matches[0] as $xip) {
                    if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                        $ip = $xip;
                        break;
                    }
                }
            } elseif (
                isset($_SERVER['HTTP_CLIENT_IP'])
                && preg_match(
                    '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',
                    $_SERVER['HTTP_CLIENT_IP']
                )
            ) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (
                isset($_SERVER['HTTP_CF_CONNECTING_IP'])
                && preg_match(
                    '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',
                    $_SERVER['HTTP_CF_CONNECTING_IP']
                )
            ) {
                $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
            } elseif (
                isset($_SERVER['HTTP_X_REAL_IP']) &&
                preg_match(
                    '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',
                    $_SERVER['HTTP_X_REAL_IP']
                )
            ) {
                $ip = $_SERVER['HTTP_X_REAL_IP'];
            }
            return $ip;
        }
        public function local(): bool
        {
            return $this->ip() === LOCALHOST_IP;
        }
    }
}