Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.57% covered (warning)
69.57%
16 / 23
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
HttpRequest
69.57% covered (warning)
69.57%
16 / 23
20.00% covered (danger)
20.00%
1 / 5
31.28
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 none
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 exists
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
9.49
 getValue
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
10.37
 getFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Http;
6
7use PeServer\Core\Http\HttpHeader;
8use PeServer\Core\Http\HttpMethod;
9use PeServer\Core\Mvc\UploadedFile;
10use PeServer\Core\Store\SpecialStore;
11use PeServer\Core\Throws\KeyNotFoundException;
12
13/**
14 * HTTPリクエストデータ。
15 *
16 * GET/POST/URLパラメータの値などはこいつから取得する。
17 */
18readonly class HttpRequest
19{
20    /**
21     * 生成。
22     *
23     * @param HttpMethod $httpMethod 要求メソッド。
24     * @param HttpHeader $httpHeader 要求ヘッダ。
25     * @param array<non-empty-string,string> $urlParameters URLパラメータ。
26     */
27    public function __construct(
28        public SpecialStore $specialStore,
29        public HttpMethod $httpMethod,
30        public HttpHeader $httpHeader,
31        public array $urlParameters
32    ) {
33    }
34
35    #region function
36
37    /**
38     * 無効なリクエストを生成。
39     *
40     * パラメータ内部で使用してるけど結構どうでもいい部分で使用する(原則使用することはない)。
41     */
42    public static function none(): self
43    {
44        return new self(new SpecialStore(), HttpMethod::Get, new HttpHeader(), []);
45    }
46
47    /**
48     * 名前に対する値が存在するか。
49     *
50     * @param string $name パラメータ名。
51     * @param bool $strict メソッドを厳格に判定するか。
52     * @return HttpRequestExists
53     */
54    public function exists(string $name, bool $strict = true): HttpRequestExists
55    {
56        if (isset($this->urlParameters[$name])) {
57            return new HttpRequestExists($name, true, HttpRequestExists::KIND_URL);
58        }
59
60        if (!$strict || $this->httpMethod === HttpMethod::Get) {
61            if ($this->specialStore->containsGetName($name)) {
62                return new HttpRequestExists($name, true, HttpRequestExists::KIND_GET);
63            }
64        }
65
66        if (!$strict || $this->httpMethod === HttpMethod::Post) {
67            if ($this->specialStore->containsPostName($name)) {
68                return new HttpRequestExists($name, true, HttpRequestExists::KIND_POST);
69            }
70            if ($this->specialStore->containsFileName($name)) {
71                return new HttpRequestExists($name, true, HttpRequestExists::KIND_FILE);
72            }
73        }
74
75        return new HttpRequestExists($name, false, HttpRequestExists::KIND_NONE);
76    }
77
78    // public function isMulti(string $key): bool
79    // {
80
81    // }
82
83    /**
84     * キーに対する値を取得する。
85     *
86     * ファイルは取得できない。
87     *
88     * @param string $name
89     * @return string
90     * @throws KeyNotFoundException キーに対する値が存在しない。
91     */
92    public function getValue(string $name, bool $strict = true): string
93    {
94        if (isset($this->urlParameters[$name])) {
95            return $this->urlParameters[$name];
96        }
97
98        if (!$strict || $this->httpMethod === HttpMethod::Get) {
99            if ($this->specialStore->containsGetName($name)) {
100                return $this->specialStore->getGet($name);
101            }
102        }
103        if (!$strict || $this->httpMethod === HttpMethod::Post) {
104            if ($this->specialStore->containsPostName($name)) {
105                return $this->specialStore->getPost($name);
106            }
107        }
108
109        throw new KeyNotFoundException("parameter not found: $name");
110    }
111
112    public function getFile(string $name): UploadedFile
113    {
114        return $this->specialStore->getFile($name);
115    }
116
117    #endregion
118}