1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace roxblnfk\SmartStream\Data; |
4
|
|
|
|
5
|
|
|
class DataBucket |
6
|
|
|
{ |
7
|
|
|
/** @var mixed */ |
8
|
|
|
protected $data; |
9
|
|
|
protected ?int $code = null; |
10
|
|
|
/** @var string[] */ |
11
|
|
|
protected array $headers = []; |
12
|
|
|
protected ?string $format = null; |
13
|
|
|
protected array $params = []; |
14
|
|
|
|
15
|
|
|
protected const IS_FORMATABLE = true; |
16
|
|
|
|
17
|
|
|
public function __construct($data, string $format = null, array $params = []) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->data = $data; |
20
|
|
|
$this->format = $format; |
21
|
|
|
} |
22
|
|
|
public function getCode(): ?int |
23
|
|
|
{ |
24
|
|
|
return $this->code; |
25
|
|
|
} |
26
|
|
|
public function getHeaders(): array |
27
|
|
|
{ |
28
|
|
|
return $this->headers; |
29
|
|
|
} |
30
|
|
|
public function getFormat(): ?string |
31
|
|
|
{ |
32
|
|
|
return $this->format; |
33
|
|
|
} |
34
|
|
|
public function getParams(): array |
35
|
|
|
{ |
36
|
|
|
return $this->params; |
37
|
|
|
} |
38
|
|
|
public function getData() |
39
|
|
|
{ |
40
|
|
|
return $this->data; |
41
|
|
|
} |
42
|
|
|
public function isFormatable(): bool |
43
|
|
|
{ |
44
|
|
|
return static::IS_FORMATABLE; |
45
|
|
|
} |
46
|
|
|
public function hasFormat(): bool |
47
|
|
|
{ |
48
|
|
|
return static::IS_FORMATABLE && $this->format !== null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setCode(?int $code = 200): self |
52
|
|
|
{ |
53
|
|
|
$this->code = $code; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
public function setHeaders(array $headers = []): self |
57
|
|
|
{ |
58
|
|
|
$this->headers = $headers; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
public function addHeaders(array $headers = []): self |
62
|
|
|
{ |
63
|
|
|
$this->headers = [...$this->headers, ...$headers]; |
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
public function setHeader(string $name, string $value): self |
67
|
|
|
{ |
68
|
|
|
$this->headers[$name] = $value; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
public function withFormat(?string $format, array $params = null): self |
72
|
|
|
{ |
73
|
|
|
$clone = clone $this; |
74
|
|
|
$clone->setFormat($format, $params); |
75
|
|
|
return $clone; |
76
|
|
|
} |
77
|
|
|
public function setFormat(?string $format, array $params = null): self |
78
|
|
|
{ |
79
|
|
|
$this->format = $format; |
80
|
|
|
if ($params !== null) { |
81
|
|
|
$this->params = $params; |
82
|
|
|
} |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.