-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathToMany.php
42 lines (35 loc) · 1.01 KB
/
ToMany.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace JsonApiPhp\JsonApi;
use JsonApiPhp\JsonApi\Internal\Identifier;
use JsonApiPhp\JsonApi\Internal\ResourceField;
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;
use JsonApiPhp\JsonApi\Internal\ToManyMember;
final class ToMany implements Identifier, ResourceField {
use ResourceFieldTrait;
/**
* @var ToManyMember[]
*/
private readonly array $members;
public function __construct(
string $name,
private readonly ResourceIdentifierCollection $collection,
ToManyMember ...$members
) {
$this->validateFieldName($name);
$this->name = $name;
$this->members = $members;
}
/**
* @param object $o
* @internal
*/
public function attachTo(object $o): void {
$rel = child(child($o, 'relationships'), $this->name);
$rel->data = [];
$this->collection->attachTo($rel);
foreach ($this->members as $member) {
$member->attachTo($rel);
}
}
}