1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/entity project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Entity; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assert; |
12
|
|
|
use Daikon\Interop\Assertion; |
13
|
|
|
use Daikon\ValueObject\ValueObjectInterface; |
14
|
|
|
|
15
|
|
|
final class Attribute implements AttributeInterface |
16
|
|
|
{ |
17
|
|
|
private string $name; |
18
|
|
|
|
19
|
|
|
private string $valueType; |
20
|
|
|
|
21
|
10 |
|
public static function define(string $name, string $valueType): self |
22
|
|
|
{ |
23
|
10 |
|
Assert::that($valueType) |
24
|
10 |
|
->classExists(sprintf('Unable to load value type "%s"', $valueType)) |
25
|
10 |
|
->implementsInterface(ValueObjectInterface::class, sprintf( |
26
|
10 |
|
"Given value type '%s' does not implement required interface '%s'.", |
27
|
|
|
$valueType, |
28
|
10 |
|
ValueObjectInterface::class |
29
|
|
|
)); |
30
|
|
|
|
31
|
10 |
|
return new self($name, $valueType); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @param mixed $value */ |
35
|
10 |
|
public function makeValue($value = null): ValueObjectInterface |
36
|
|
|
{ |
37
|
10 |
|
if ($value instanceof ValueObjectInterface) { |
38
|
|
|
Assertion::isInstanceOf($value, $this->valueType, sprintf( |
39
|
|
|
"Value '%s' must be instance of value type '%s'.", |
40
|
|
|
get_class($value), |
41
|
|
|
$this->valueType |
42
|
|
|
)); |
43
|
|
|
return $value; |
44
|
|
|
} |
45
|
|
|
|
46
|
10 |
|
return ($this->valueType.'::fromNative')($value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getValueType(): string |
50
|
|
|
{ |
51
|
|
|
return $this->valueType; |
52
|
|
|
} |
53
|
|
|
|
54
|
10 |
|
public function getName(): string |
55
|
|
|
{ |
56
|
10 |
|
return $this->name; |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
private function __construct(string $name, string $valueType) |
60
|
|
|
{ |
61
|
10 |
|
$this->name = $name; |
62
|
10 |
|
$this->valueType = $valueType; |
63
|
10 |
|
} |
64
|
|
|
} |
65
|
|
|
|