1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Poloniex PHP SDK. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2017-2018 Chasovskih Grisha <[email protected]> |
9
|
|
|
* @license https://github.com/signulls/poloniex-php-sdk/blob/master/LICENSE MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Poloniex\Api\Traits; |
13
|
|
|
|
14
|
|
|
use Poloniex\Exception\PoloniexException; |
15
|
|
|
use Poloniex\Response\ResponseInterface; |
16
|
|
|
use Symfony\Component\Serializer\{Serializer, SerializerInterface}; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait ResponseFactoryTrait |
20
|
|
|
* |
21
|
|
|
* @author Grisha Chasovskih <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait ResponseFactoryTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Serializer |
27
|
|
|
*/ |
28
|
|
|
protected $serializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets the serializer. |
32
|
|
|
* |
33
|
|
|
* @param SerializerInterface $serializer A SerializerInterface instance |
34
|
|
|
*/ |
35
|
|
|
public function setSerializer(SerializerInterface $serializer): void |
36
|
|
|
{ |
37
|
|
|
$this->serializer = $serializer; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $responseClass |
42
|
|
|
* @param array $data |
43
|
|
|
* |
44
|
|
|
* @return ResponseInterface |
45
|
|
|
*/ |
46
|
|
|
protected function factory(string $responseClass, array $data): ResponseInterface |
47
|
|
|
{ |
48
|
|
|
/* @var $response ResponseInterface */ |
49
|
|
|
$response = $this->serializer->denormalize($data, $responseClass); |
50
|
|
|
$this->throwExceptionIf(!$response instanceof $responseClass, 'Poloniex response is not valid.'); |
51
|
|
|
|
52
|
|
|
if (isset($data['globalTradeID'])) { |
53
|
|
|
$response->globalTradeId = (int) $data['globalTradeID']; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (isset($data['tradeID'])) { |
57
|
|
|
$response->tradeId = (int) $data['tradeID']; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (isset($data['orderID'])) { |
61
|
|
|
$response->orderId = (int) $data['orderID']; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Throw exception by condition |
69
|
|
|
* |
70
|
|
|
* @param bool $condition |
71
|
|
|
* @param string $message |
72
|
|
|
* |
73
|
|
|
* @throws PoloniexException |
74
|
|
|
*/ |
75
|
|
|
protected function throwExceptionIf(bool $condition, string $message): void |
76
|
|
|
{ |
77
|
|
|
if ($condition) { |
78
|
|
|
throw new PoloniexException($message); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.