Completed
Push — master ( 12085b...d1c235 )
by Rudolph
01:28
created

Attribute::withDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Midnight\Block\Type\Attribute;
4
5
class Attribute implements AttributeInterface
6
{
7
    /** @var string */
8
    private $name;
9
    /** @var string|null */
10
    private $default;
11
12
    public function __construct(string $name)
13
    {
14
        $this->name = $name;
15
    }
16
17
    public function getName(): string
18
    {
19
        return $this->name;
20
    }
21
22
    public function getDefault(): ?string
23
    {
24
        return $this->default;
25
    }
26
27
    public function withDefault(string $value): self
28
    {
29
        $clone = clone $this;
30
        $clone->name = $value;
31
        return $clone;
32
    }
33
34
    /**
35
     * @throws \Midnight\Block\Type\Attribute\Exception\InvalidAttributeValueException
36
     */
37
    public function validate(?string $value): void
38
    {
39
        if ($this->default === null) {
40
            return;
41
        }
42
        if ($value === null) {
43
            throw new Exception\InvalidAttributeValueException(sprintf('Attribute %s is not optional', $this->name));
44
        }
45
    }
46
}
47