|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
|
4
|
|
|
* @date 17/08/2016 13:44 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Yandex\Direct\Transport; |
|
8
|
|
|
|
|
9
|
|
|
use Yandex\Direct\ConfigurableTrait; |
|
10
|
|
|
use Yandex\Direct\Exception\InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class TransportRequest |
|
14
|
|
|
* @package Yandex\Direct |
|
15
|
|
|
*/ |
|
16
|
|
|
class Response implements ResponseInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use ConfigurableTrait; |
|
19
|
|
|
|
|
20
|
|
|
const UNITS_TYPE_DEBIT = 0; |
|
21
|
|
|
const UNITS_TYPE_REST = 1; |
|
22
|
|
|
const UNITS_TYPE_LIMIT = 2; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Response constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $responseAttributes |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function __construct(array $responseAttributes) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$this->setOptions($responseAttributes); |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $service; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $method; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $units = [null, null, null]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $requestId; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $body; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $code; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $headers = []; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function getUnits() |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->units; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $rawUnits |
|
79
|
|
|
* @throws InvalidArgumentException |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function setUnits($rawUnits) |
|
82
|
|
|
{ |
|
83
|
2 |
|
$units = explode('/', $rawUnits); |
|
84
|
2 |
|
if (is_array($units) |
|
85
|
2 |
|
&& count($units) > 0 |
|
86
|
2 |
|
) { |
|
87
|
2 |
|
$this->units = $units; |
|
88
|
2 |
|
} |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritdoc |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getRequestId() |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->requestId; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function getUnitsDebit() |
|
103
|
|
|
{ |
|
104
|
1 |
|
return $this->getUnits()[self::UNITS_TYPE_DEBIT]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritdoc |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function getUnitsRest() |
|
111
|
|
|
{ |
|
112
|
1 |
|
return $this->getUnits()[self::UNITS_TYPE_REST]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritdoc |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getUnitsLimit() |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->getUnits()[self::UNITS_TYPE_LIMIT]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritdoc |
|
125
|
|
|
*/ |
|
126
|
2 |
|
public function getBody() |
|
127
|
|
|
{ |
|
128
|
2 |
|
return $this->body; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @inheritdoc |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function getHeaders() |
|
135
|
|
|
{ |
|
136
|
1 |
|
return $this->headers; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritdoc |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function getService() |
|
143
|
|
|
{ |
|
144
|
1 |
|
return $this->service; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @inheritdoc |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getMethod() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->headers; |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getCode() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->code; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.