1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Resource; |
15
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\Action\ActionCollection; |
17
|
|
|
use FiveLab\Component\Resource\Resource\Action\ActionInterface; |
18
|
|
|
use FiveLab\Component\Resource\Resource\Relation\RelationCollection; |
19
|
|
|
use FiveLab\Component\Resource\Resource\Relation\RelationInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The abstract class for support resource. |
23
|
|
|
* |
24
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractResourceSupport implements ResourceInterface, RelatedResourceInterface, ActionedResourceInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \SplObjectStorage|RelationInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $relations; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \SplObjectStorage|ActionInterface[] |
35
|
|
|
*/ |
36
|
|
|
private $actions; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
*/ |
41
|
26 |
|
public function __construct() |
42
|
|
|
{ |
43
|
26 |
|
$this->relations = new \SplObjectStorage(); |
44
|
26 |
|
$this->actions = new \SplObjectStorage(); |
45
|
26 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
8 |
|
public function addRelation(RelationInterface $relation): void |
51
|
|
|
{ |
52
|
8 |
|
$this->relations->attach($relation); |
53
|
8 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
9 |
|
public function getRelations(): RelationCollection |
59
|
|
|
{ |
60
|
9 |
|
return new RelationCollection(...iterator_to_array($this->relations)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function removeRelation(RelationInterface $relation): void |
67
|
|
|
{ |
68
|
1 |
|
$this->relations->detach($relation); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
4 |
|
public function addAction(ActionInterface $action): void |
75
|
|
|
{ |
76
|
4 |
|
$this->actions->attach($action); |
77
|
4 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
6 |
|
public function getActions(): ActionCollection |
83
|
|
|
{ |
84
|
6 |
|
return new ActionCollection(...iterator_to_array($this->actions)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function removeAction(ActionInterface $action): void |
91
|
|
|
{ |
92
|
1 |
|
$this->actions->detach($action); |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|