-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportableList.php
141 lines (126 loc) · 3.11 KB
/
ExportableList.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace GrizzIt\Exportable\Component;
use GrizzIt\Exportable\Exception\UnexpectedTypeException;
use GrizzIt\Exportable\Common\ExportableComponentInterface;
class ExportableList implements ExportableComponentInterface
{
/**
* Contains the items registered to the exportable component.
*
* @var ExportableComponentInterface[]
*/
private array $items;
/**
* Whether or not the type of the input should be restricted.
*
* @var string|null
*/
private ?string $restrict;
/**
* Constructor.
*
* @param string|null $restrict
* @param ExportableComponentInterface ...$items
*/
public function __construct(
?string $restrict,
ExportableComponentInterface ...$items
) {
$this->restrict = $restrict;
if ($this->restrictInputs(...$items)) {
$this->items = $items;
}
}
/**
* Restrict multiple inputs.
*
* @param ExportableComponentInterface ...$items
*
* @return bool
*/
private function restrictInputs(
ExportableComponentInterface ...$items
): bool {
if ($this->restrict !== null) {
foreach ($items as $item) {
$this->restrictInput($item);
}
}
return true;
}
/**
* Restricts the input when it is set-up.
*
* @param ExportableComponentInterface $item
*
* @return bool
*
* @throws UnexpectedTypeException When the passed class is not of the correct type.
*/
private function restrictInput(ExportableComponentInterface $item): bool
{
if (
$this->restrict !== null &&
!($item instanceof $this->restrict)
) {
throw new UnexpectedTypeException(
static::class,
$this->restrict,
get_class($item)
);
}
return true;
}
/**
* Retrieves all registered items.
*
* @return ExportableComponentInterface[]
*/
public function getItems(): array
{
return $this->items;
}
/**
* Overwrites all exportable components.
*
* @param ExportableComponentInterface ...$items
*
* @return void
*/
public function setItems(ExportableComponentInterface ...$items): void
{
if ($this->restrictInputs(...$items)) {
$this->items = $items;
}
}
/**
* Adds an exportable item to the list.
*
* @param ExportableComponentInterface $item
*
* @return void
*/
public function addItem(ExportableComponentInterface $item): void
{
if ($this->restrictInput($item)) {
$this->items[] = $item;
}
}
/**
* Exports the object to a public facing definition.
*
* @return mixed
*/
public function export(): mixed
{
$export = [];
foreach ($this->items as $item) {
$export[] = $item->export();
}
return $export;
}
}