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 $code = null; |
12
|
|
|
/** @var string[] */ |
13
|
|
|
protected array $headers = []; |
14
|
|
|
protected ?string $format = null; |
15
|
|
|
protected array $params = []; |
16
|
|
|
|
17
|
|
|
protected const IS_CONVERTABLE = true; |
18
|
|
|
|
19
|
|
|
public function __construct($data, string $format = null, array $params = []) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$this->data = $data; |
22
|
|
|
$this->format = $format; |
23
|
|
|
} |
24
|
|
|
public function getCode(): ?int |
25
|
|
|
{ |
26
|
|
|
return $this->code; |
27
|
|
|
} |
28
|
|
|
public function getHeaders(): array |
29
|
|
|
{ |
30
|
|
|
return $this->headers; |
31
|
|
|
} |
32
|
|
|
public function getFormat(): ?string |
33
|
|
|
{ |
34
|
|
|
return $this->format; |
35
|
|
|
} |
36
|
|
|
public function getParams(): array |
37
|
|
|
{ |
38
|
|
|
return $this->params; |
39
|
|
|
} |
40
|
|
|
public function getData() |
41
|
|
|
{ |
42
|
|
|
return $this->data; |
43
|
|
|
} |
44
|
|
|
public function isConvertable(): bool |
45
|
|
|
{ |
46
|
|
|
return static::IS_CONVERTABLE; |
47
|
|
|
} |
48
|
|
|
public function hasFormat(): bool |
49
|
|
|
{ |
50
|
|
|
return static::IS_CONVERTABLE && $this->format !== null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function withStatus(?int $code = Status::OK): self |
54
|
|
|
{ |
55
|
|
|
$clone = clone $this; |
56
|
|
|
$clone->setStatus($code); |
57
|
|
|
return $clone; |
58
|
|
|
} |
59
|
|
|
public function withHeader(string $name, string $value): self |
60
|
|
|
{ |
61
|
|
|
$clone = clone $this; |
62
|
|
|
$clone->setHeader($name, $value); |
63
|
|
|
return $clone; |
64
|
|
|
} |
65
|
|
|
public function withFormat(?string $format, array $params = null): self |
66
|
|
|
{ |
67
|
|
|
$clone = clone $this; |
68
|
|
|
$clone->setFormat($format, $params); |
69
|
|
|
return $clone; |
70
|
|
|
} |
71
|
|
|
public function withoutHeader(string $name): self |
72
|
|
|
{ |
73
|
|
|
$clone = clone $this; |
74
|
|
|
$clone->unsetHeader($name); |
75
|
|
|
return $clone; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
protected function unsetHeader(string $name): void |
80
|
|
|
{ |
81
|
|
|
unset($this->headers[$name]); |
82
|
|
|
} |
83
|
|
|
protected function setHeader(string $name, ?string $value): void |
84
|
|
|
{ |
85
|
|
|
if ($value === null) { |
86
|
|
|
$this->unsetHeader($name); |
87
|
|
|
} else { |
88
|
|
|
$this->headers[$name] = $value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
protected function setFormat(?string $format, ?array $params): void |
92
|
|
|
{ |
93
|
|
|
$this->format = $format; |
94
|
|
|
if ($params !== null) { |
|
|
|
|
95
|
|
|
$this->params = $params; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
protected function setStatus(?int $code): void |
99
|
|
|
{ |
100
|
|
|
$this->code = $code; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.