Passed
Push — master ( 2b5d19...008e72 )
by Tomasz
01:35
created

StringValueObject::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TimiTao\ValueObject\Beberlei\Standard;
6
7
use Exception;
8
use Throwable;
9
use TimiTao\ValueObject\Core\Standard\StringValueObject as StringValueObjectInterface;
10
11
abstract class StringValueObject implements StringValueObjectInterface
12
{
13
    private $value;
14
15
    /**
16
     * @throws Exception if value is invalid
17
     */
18
    public function __construct(string $value)
19
    {
20
        try {
21
            $this->guard($value);
22
        } catch (Throwable $e) {
23
            throw $this->throwException($value, $e);
24
        }
25
        $this->value = $value;
26
    }
27
28
    public function equals(StringValueObjectInterface $other): bool
29
    {
30
        if (static::class !== get_class($other)) {
31
            return false;
32
        }
33
        return $this->getValue() === $other->getValue();
34
    }
35
36
    public function getValue(): string
37
    {
38
        return $this->value;
39
    }
40
41
    /**
42
     * @throws Throwable if value is invalid
43
     */
44
    abstract protected function guard(string $value): void;
45
46
    abstract protected function throwException(string $value, Throwable $e): Exception;
47
}
48