DataBucket::getParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace roxblnfk\SmartStream\Data;
4
5
use Yiisoft\Http\Status;
6
7
class DataBucket
8
{
9
    /** @var mixed */
10
    protected $data;
11
    protected ?int $statusCode = null;
12
    /** @var string[] */
13
    protected array $headers = [];
14
    protected ?string $format = null;
15
    protected iterable $params = [];
16
17
    protected const IS_CONVERTABLE = true;
18
19 169
    public function __construct($data, string $format = null, iterable $params = [])
20
    {
21 169
        $this->data = $data;
22 169
        if ($format !== null) {
23 2
            $this->format = $format;
24 2
            $this->params = $params;
25
        }
26 169
    }
27 13
    public function getStatusCode(): ?int
28
    {
29 13
        return $this->statusCode;
30
    }
31 66
    public function getFormat(): ?string
32
    {
33 66
        return $this->format;
34
    }
35 13
    public function getParams(): iterable
36
    {
37 13
        return $this->params;
38
    }
39 28
    public function getData()
40
    {
41 28
        return $this->data;
42
    }
43 106
    public function isConvertable(): bool
44
    {
45 106
        return static::IS_CONVERTABLE;
46
    }
47 63
    public function hasFormat(): bool
48
    {
49 63
        return static::IS_CONVERTABLE && $this->format !== null;
50
    }
51
52 16
    public function hasHeader(string $name): bool
53
    {
54 16
        return array_key_exists($name, $this->headers);
55
    }
56 23
    public function getHeaderLine(string $name): ?string
57
    {
58 23
        return $this->headers[$name] ?? null;
59
    }
60 13
    public function getHeaders(): array
61
    {
62 13
        return $this->headers;
63
    }
64
65 5
    public function withStatusCode(?int $code = Status::OK): self
66
    {
67 5
        $new = clone $this;
68 5
        $new->setStatusCode($code);
69 5
        return $new;
70
    }
71 17
    public function withHeader(string $name, string $value): self
72
    {
73 17
        $new = clone $this;
74 17
        $new->setHeader($name, $value);
75 17
        return $new;
76
    }
77 63
    public function withFormat(?string $format, array $params = null): self
78
    {
79 63
        $new = clone $this;
80 63
        $new->setFormat($format, $params);
81 63
        return $new;
82
    }
83 4
    public function withoutHeader(string $name): self
84
    {
85 4
        $new = clone $this;
86 4
        $new->unsetHeader($name);
87 4
        return $new;
88
    }
89 4
    public function withoutHeaders(): self
90
    {
91 4
        $new = clone $this;
92 4
        $new->headers = [];
93 4
        return $new;
94
    }
95
96 7
    protected function unsetHeader(string $name): void
97
    {
98 7
        unset($this->headers[$name]);
99 7
    }
100 53
    protected function setHeader(string $name, ?string $value): void
101
    {
102 53
        if ($value === null) {
103 3
            $this->unsetHeader($name);
104
        } else {
105 53
            $this->headers[$name] = $value;
106
        }
107 53
    }
108 63
    protected function setFormat(?string $format, ?iterable $params): void
109
    {
110 63
        $this->format = $format;
111 63
        if ($params !== null) {
112 10
            $this->params = $params;
113
        }
114 63
    }
115 15
    protected function setStatusCode(?int $code): void
116
    {
117 15
        $this->statusCode = $code;
118 15
    }
119
}
120