|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DigitalCz\DigiSign\Resource; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use DigitalCz\DigiSign\Exception\RuntimeException; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @template T |
|
13
|
|
|
* |
|
14
|
|
|
* @extends ArrayObject<int|string, T> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Collection extends ArrayObject implements ResourceInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ResponseInterface Original API response */ |
|
19
|
|
|
protected $_response; // phpcs:ignore |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var class-string<T> |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
protected $resourceClass; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param mixed[] $result |
|
28
|
|
|
* @param class-string<T> $resourceClass |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(array $result, string $resourceClass) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->resourceClass = $resourceClass; |
|
33
|
|
|
|
|
34
|
|
|
$items = array_map(static function (array $itemValue) use ($resourceClass) { |
|
35
|
|
|
return new $resourceClass($itemValue); |
|
36
|
|
|
}, $result); |
|
37
|
|
|
|
|
38
|
|
|
parent::__construct($items); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getResponse(): ResponseInterface |
|
42
|
|
|
{ |
|
43
|
|
|
if (!isset($this->_response)) { |
|
44
|
|
|
throw new RuntimeException('Only resource returned from client has API response set'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->_response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function setResponse(ResponseInterface $response): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->_response = $response; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array<T> |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getResult(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getArrayCopy(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array<int|string, array<mixed>> |
|
65
|
|
|
*/ |
|
66
|
|
|
public function toArray(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return array_map( |
|
69
|
|
|
static function (BaseResource $item): array { |
|
70
|
|
|
return $item->toArray(); |
|
71
|
|
|
}, |
|
72
|
|
|
$this->getArrayCopy() |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function self(): string |
|
77
|
|
|
{ |
|
78
|
|
|
throw new RuntimeException('Resource has no self link.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @throws RuntimeException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function id(): string |
|
85
|
|
|
{ |
|
86
|
|
|
throw new RuntimeException('Collection doesnt have ID.'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return array<int|string, array<mixed>> |
|
91
|
|
|
*/ |
|
92
|
|
|
public function jsonSerialize(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->toArray(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return class-string<T> |
|
|
|
|
|
|
99
|
|
|
*/ |
|
100
|
|
|
public function getResourceClass(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->resourceClass; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|