|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Document\Behaviour; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\ResourceIdentifierObject; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Collection of resource identifiers container behaviour |
|
10
|
|
|
* |
|
11
|
|
|
* @see http://jsonapi.org/format/#document-links |
|
12
|
|
|
* |
|
13
|
|
|
* @package Mikemirten\Component\JsonApi\Document\Behaviour |
|
14
|
|
|
*/ |
|
15
|
|
|
trait IdentifierCollectionContainer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Resource identifiers |
|
19
|
|
|
* |
|
20
|
|
|
* @var ResourceIdentifierObject[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $identifiers = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Add resource identifier |
|
26
|
|
|
* |
|
27
|
|
|
* @param ResourceIdentifierObject $identifier |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public function addIdentifier(ResourceIdentifierObject $identifier) |
|
30
|
|
|
{ |
|
31
|
9 |
|
$this->identifiers[] = $identifier; |
|
32
|
9 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get first identifier from collection |
|
36
|
|
|
* |
|
37
|
|
|
* @return ResourceIdentifierObject |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function getFirstIdentifier(): ResourceIdentifierObject |
|
40
|
|
|
{ |
|
41
|
2 |
|
return reset($this->identifiers); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Contains any identifiers |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function hasIdentifiers(): bool |
|
50
|
|
|
{ |
|
51
|
|
|
return count($this->identifiers) > 0; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get resource identifiers |
|
56
|
|
|
* |
|
57
|
|
|
* @return ResourceIdentifierObject[] |
|
58
|
|
|
*/ |
|
59
|
16 |
|
public function getIdentifiers(): array |
|
60
|
|
|
{ |
|
61
|
16 |
|
return $this->identifiers; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function getIterator(): \Traversable |
|
68
|
|
|
{ |
|
69
|
2 |
|
return new \ArrayIterator($this->getIdentifiers()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Cast identifiers to an array |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
9 |
|
protected function identifiersToArray(): array |
|
78
|
|
|
{ |
|
79
|
9 |
|
$data = []; |
|
80
|
|
|
|
|
81
|
9 |
|
foreach ($this->getIdentifiers() as $identifier) |
|
82
|
|
|
{ |
|
83
|
2 |
|
$data[] = $identifier->toArray(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
return $data; |
|
87
|
|
|
} |
|
88
|
|
|
} |