|
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
|
162 |
|
public function __construct($data, string $format = null, iterable $params = []) |
|
20
|
|
|
{ |
|
21
|
162 |
|
$this->data = $data; |
|
22
|
162 |
|
if ($format !== null) { |
|
23
|
2 |
|
$this->format = $format; |
|
24
|
2 |
|
$this->params = $params; |
|
25
|
|
|
} |
|
26
|
162 |
|
} |
|
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
|
103 |
|
public function isConvertable(): bool |
|
44
|
|
|
{ |
|
45
|
103 |
|
return static::IS_CONVERTABLE; |
|
46
|
|
|
} |
|
47
|
60 |
|
public function hasFormat(): bool |
|
48
|
|
|
{ |
|
49
|
60 |
|
return static::IS_CONVERTABLE && $this->format !== null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
12 |
|
public function hasHeader(string $name): bool |
|
53
|
|
|
{ |
|
54
|
12 |
|
return array_key_exists($name, $this->headers); |
|
55
|
|
|
} |
|
56
|
19 |
|
public function getHeaderLine(string $name): ?string |
|
57
|
|
|
{ |
|
58
|
19 |
|
return $this->headers[$name] ?? null; |
|
59
|
|
|
} |
|
60
|
9 |
|
public function getHeaders(): array |
|
61
|
|
|
{ |
|
62
|
9 |
|
return $this->headers; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
public function withStatusCode(?int $code = Status::OK): self |
|
66
|
|
|
{ |
|
67
|
5 |
|
$clone = clone $this; |
|
68
|
5 |
|
$clone->setStatusCode($code); |
|
69
|
5 |
|
return $clone; |
|
70
|
|
|
} |
|
71
|
13 |
|
public function withHeader(string $name, string $value): self |
|
72
|
|
|
{ |
|
73
|
13 |
|
$clone = clone $this; |
|
74
|
13 |
|
$clone->setHeader($name, $value); |
|
75
|
13 |
|
return $clone; |
|
76
|
|
|
} |
|
77
|
63 |
|
public function withFormat(?string $format, array $params = null): self |
|
78
|
|
|
{ |
|
79
|
63 |
|
$clone = clone $this; |
|
80
|
63 |
|
$clone->setFormat($format, $params); |
|
81
|
63 |
|
return $clone; |
|
82
|
|
|
} |
|
83
|
4 |
|
public function withoutHeader(string $name): self |
|
84
|
|
|
{ |
|
85
|
4 |
|
$clone = clone $this; |
|
86
|
4 |
|
$clone->unsetHeader($name); |
|
87
|
4 |
|
return $clone; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
protected function unsetHeader(string $name): void |
|
91
|
|
|
{ |
|
92
|
7 |
|
unset($this->headers[$name]); |
|
93
|
7 |
|
} |
|
94
|
49 |
|
protected function setHeader(string $name, ?string $value): void |
|
95
|
|
|
{ |
|
96
|
49 |
|
if ($value === null) { |
|
97
|
3 |
|
$this->unsetHeader($name); |
|
98
|
|
|
} else { |
|
99
|
49 |
|
$this->headers[$name] = $value; |
|
100
|
|
|
} |
|
101
|
49 |
|
} |
|
102
|
63 |
|
protected function setFormat(?string $format, ?iterable $params): void |
|
103
|
|
|
{ |
|
104
|
63 |
|
$this->format = $format; |
|
105
|
63 |
|
if ($params !== null) { |
|
106
|
10 |
|
$this->params = $params; |
|
107
|
|
|
} |
|
108
|
63 |
|
} |
|
109
|
14 |
|
protected function setStatusCode(?int $code): void |
|
110
|
|
|
{ |
|
111
|
14 |
|
$this->statusCode = $code; |
|
112
|
14 |
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|