Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractStringValueObject
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
8 / 8
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 equals
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validateRules
n/a
0 / 0
n/a
0 / 0
0
 getExceptionClass
n/a
0 / 0
n/a
0 / 0
0
 validate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Domain;
11
12use InvalidArgumentException;
13use Override;
14
15abstract readonly class AbstractStringValueObject implements ValueObjectInterface
16{
17    private string $value;
18
19    private function __construct(string $value)
20    {
21        $normalized = $this->normalize($value);
22        $this->validate($normalized);
23        $this->value = $normalized;
24    }
25
26    #[Override]
27    public function __toString(): string
28    {
29        return $this->value;
30    }
31
32    final public static function fromString(string $value): static
33    {
34        return new static($value);
35    }
36
37    final public function getValue(): string
38    {
39        return $this->value;
40    }
41
42    #[Override]
43    final public function equals(ValueObjectInterface $other): bool
44    {
45        if (!$other instanceof static) {
46            return false;
47        }
48
49        return $this->value === $other->value;
50    }
51
52    #[Override]
53    final public function jsonSerialize(): string
54    {
55        return $this->value;
56    }
57
58    protected function normalize(string $value): string
59    {
60        return trim($value);
61    }
62
63    abstract protected function validateRules(string $value): void;
64
65    abstract protected static function getExceptionClass(): callable;
66
67    private function validate(string $value): void
68    {
69        try {
70            $this->validateRules($value);
71        } catch (InvalidArgumentException $invalidArgumentException) {
72            $callback = static::getExceptionClass();
73            $arguments = $invalidArgumentException->getMessage();
74            throw call_user_func($callback, $arguments);
75        }
76    }
77}