|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BecosoftApi\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use BecosoftApi\Api; |
|
6
|
|
|
use BecosoftApi\ApiInterface; |
|
7
|
|
|
use Webmozart\Assert\Assert; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractEntity |
|
11
|
|
|
* @package BecosoftApi\Entity |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractEntity implements EntityInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const PER_CALL = 50; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $endpoint; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $idField; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Api |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $api; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* AbstractEntity constructor. |
|
34
|
|
|
* @param ApiInterface $api |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function __construct(ApiInterface $api) |
|
37
|
|
|
{ |
|
38
|
4 |
|
$this->api = $api; |
|
|
|
|
|
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getById($id, array $options = []) |
|
45
|
|
|
{ |
|
46
|
|
|
Assert::notNull(self::$idField); |
|
47
|
|
|
|
|
48
|
|
|
return $this->get([self::$idField => $id], $options)->getBody()->getContents(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function post($data, array $options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
if (array_key_exists('body', $options) && !empty($data)) { |
|
57
|
|
|
throw new \Exception('BODY key already exists in options array, supplied data is not empty'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$options['body'] = $data; |
|
61
|
|
|
|
|
62
|
|
|
if (!array_key_exists('Content-Type', $options)) { |
|
63
|
|
|
$options['Content-Type'] = 'application/json'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$response = $this->api->request('POST', static::$endpoint, $options); |
|
67
|
|
|
return $response->getBody()->getContents(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function get(array $query = [], array $options = []) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!empty($query)) { |
|
76
|
|
|
$options = $this->setOptions($query, $options); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->api->request('GET', static::$endpoint, $options); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getAll(array $query = [], array $options = []) |
|
86
|
|
|
{ |
|
87
|
|
|
$options['query']['take'] = self::PER_CALL; |
|
88
|
|
|
|
|
89
|
|
|
if (!empty($query)) { |
|
90
|
|
|
$options = $this->setOptions($query, $options); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$entities = []; |
|
94
|
|
|
|
|
95
|
|
|
$skip = 0; |
|
96
|
|
|
do { |
|
97
|
|
|
$options['query']['skip'] = $skip; |
|
98
|
|
|
|
|
99
|
|
|
$result = $this->api->request('GET', static::$endpoint, $options); |
|
100
|
|
|
$decoded = json_decode($result->getBody()->getContents()); |
|
101
|
|
|
$entities = array_merge($entities, $decoded); |
|
102
|
|
|
|
|
103
|
|
|
$skip++; |
|
104
|
|
|
} while (count($decoded) === self::PER_CALL); |
|
105
|
|
|
|
|
106
|
|
|
return $entities; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $query |
|
111
|
|
|
* @param array $options |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
private function setOptions(array $query, array $options) |
|
115
|
|
|
{ |
|
116
|
|
|
if (array_key_exists('query', $options)) { |
|
117
|
|
|
$query = array_merge($options['query'], $query); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$options['query'] = $query; |
|
121
|
|
|
|
|
122
|
|
|
return $options; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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.