Passed
Push — 1.x ( e3781f...255b78 )
by Kevin
02:10
created

HttpOptions   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 153
rs 10
wmc 23

17 Methods

Rating   Name   Duplication   Size   Complexity  
A server() 0 15 3
A withFiles() 0 5 1
A create() 0 7 2
A jsonAjax() 0 3 1
A json() 0 3 1
A ajax() 0 3 1
A withHeader() 0 5 1
A parameters() 0 3 1
A withBody() 0 5 1
A body() 0 3 1
A files() 0 3 1
A withServer() 0 5 1
A asAjax() 0 3 1
A withParameters() 0 5 1
A withHeaders() 0 5 1
A asJson() 0 5 2
A __construct() 0 10 3
1
<?php
2
3
namespace Zenstruck\Browser\Extension\Http;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
class HttpOptions
9
{
10
    private const DEFAULT_OPTIONS = [
11
        'headers' => [],
12
        'parameters' => [],
13
        'files' => [],
14
        'server' => [],
15
        'body' => null,
16
        'json' => null,
17
        'ajax' => false,
18
    ];
19
20
    private array $options;
21
22
    final public function __construct(array $options = [])
23
    {
24
        $this->options = \array_merge(self::DEFAULT_OPTIONS, $options);
25
26
        if ($this->options['json']) {
27
            $this->asJson($this->options['json']);
28
        }
29
30
        if ($this->options['ajax']) {
31
            $this->asAjax();
32
        }
33
    }
34
35
    /**
36
     * @param self|array $value
37
     */
38
    final public static function create($value = []): self
39
    {
40
        if ($value instanceof static) {
41
            return $value;
42
        }
43
44
        return new static($value);
45
    }
46
47
    final public static function json($body = null): self
48
    {
49
        return static::create()->asJson($body);
50
    }
51
52
    final public static function ajax(): self
53
    {
54
        return static::create()->asAjax();
55
    }
56
57
    final public static function jsonAjax($body = null): self
58
    {
59
        return static::json($body)->asAjax();
60
    }
61
62
    final public function withHeader(string $header, string $value): self
63
    {
64
        $this->options['headers'][$header] = $value;
65
66
        return $this;
67
    }
68
69
    final public function withHeaders(array $headers): self
70
    {
71
        $this->options['headers'] = $headers;
72
73
        return $this;
74
    }
75
76
    final public function withParameters(array $parameters): self
77
    {
78
        $this->options['parameters'] = $parameters;
79
80
        return $this;
81
    }
82
83
    final public function withServer(array $server): self
84
    {
85
        $this->options['server'] = $server;
86
87
        return $this;
88
    }
89
90
    final public function withFiles(array $files): self
91
    {
92
        $this->options['files'] = $files;
93
94
        return $this;
95
    }
96
97
    final public function withBody(?string $body): self
98
    {
99
        $this->options['body'] = $body;
100
101
        return $this;
102
    }
103
104
    final public function asJson($body = null): self
105
    {
106
        return $this->withBody(null !== $body ? \json_encode($body, JSON_THROW_ON_ERROR) : null)
107
            ->withHeader('Content-Type', 'application/json')
108
            ->withHeader('Accept', 'application/json')
109
        ;
110
    }
111
112
    final public function asAjax(): self
113
    {
114
        return $this->withHeader('X-Requested-With', 'XMLHttpRequest');
115
    }
116
117
    /**
118
     * @internal
119
     */
120
    final public function parameters(): array
121
    {
122
        return $this->options['parameters'];
123
    }
124
125
    /**
126
     * @internal
127
     */
128
    final public function files(): array
129
    {
130
        return $this->options['files'];
131
    }
132
133
    /**
134
     * @author Kévin Dunglas <[email protected]>
135
     *
136
     * @internal
137
     */
138
    final public function server(): array
139
    {
140
        $server = $this->options['server'];
141
142
        foreach ($this->options['headers'] as $header => $value) {
143
            $header = \mb_strtoupper(\str_replace('-', '_', $header));
144
145
            if ('CONTENT_TYPE' !== $header) {
146
                $header = "HTTP_{$header}";
147
            }
148
149
            $server[$header] = $value;
150
        }
151
152
        return $server;
153
    }
154
155
    /**
156
     * @internal
157
     */
158
    final public function body(): ?string
159
    {
160
        return $this->options['body'];
161
    }
162
}
163