1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Midnight\Block\Type; |
4
|
|
|
|
5
|
|
|
use Midnight\Block\Exception\MissingAttributeException; |
6
|
|
|
use Midnight\Block\Exception\UnknownAttributeException; |
7
|
|
|
use Midnight\Block\Type\Attribute\AttributeInterface; |
8
|
|
|
|
9
|
|
|
class BlockType implements BlockTypeInterface |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $name; |
13
|
|
|
/** @var AttributeInterface[] */ |
14
|
|
|
private $attributes = []; |
15
|
|
|
|
16
|
|
|
public function __construct(string $name, AttributeInterface ...$attributes) |
17
|
|
|
{ |
18
|
|
|
$this->name = $name; |
19
|
|
|
$this->attributes = $attributes; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return AttributeInterface[] |
24
|
|
|
*/ |
25
|
|
|
public function getAttributes(): array |
26
|
|
|
{ |
27
|
|
|
return $this->attributes; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws UnknownAttributeException |
32
|
|
|
*/ |
33
|
|
|
public function getAttribute(string $name): AttributeInterface |
34
|
|
|
{ |
35
|
|
|
foreach ($this->attributes as $attribute) { |
36
|
|
|
if ($attribute->getName() === $name) { |
37
|
|
|
return $attribute; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
throw UnknownAttributeException::fromName($name); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getName(): string |
44
|
|
|
{ |
45
|
|
|
return $this->name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string[] $values |
50
|
|
|
* @throws UnknownAttributeException |
51
|
|
|
* @throws MissingAttributeException |
52
|
|
|
*/ |
53
|
|
|
public function validateAttributeValues(array $values): void |
54
|
|
|
{ |
55
|
|
|
foreach ($values as $name => $value) { |
56
|
|
|
$this->validateAttributeValue($name, $value); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws UnknownAttributeException |
62
|
|
|
*/ |
63
|
|
|
public function validateAttributeValue(string $name, ?string $value): void |
64
|
|
|
{ |
65
|
|
|
$this->getAttribute($name)->validate($value); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|