Passed
Push — master ( 373c52...ed53b1 )
by Thorsten
03:30
created

Attribute::getValueType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
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
declare(strict_types=1);
10
11
namespace Daikon\Entity\Entity;
12
13
use Daikon\Entity\Assert\Assertion;
14
use Daikon\Entity\Exception\ClassNotExists;
15
use Daikon\Entity\Exception\UnexpectedType;
16
use Daikon\Entity\ValueObject\ValueObjectInterface;
17
18
final class Attribute implements AttributeInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @var string
27
     */
28
    private $valueImplementor;
29
30 10
    public static function define(string $name, $valueImplementor): AttributeInterface
31
    {
32 10
        if (!class_exists($valueImplementor)) {
33
            throw new ClassNotExists(sprintf('Unable to load VO class "%s"', $valueImplementor));
34
        }
35 10
        if (!is_subclass_of($valueImplementor, ValueObjectInterface::class)) {
36
            throw new UnexpectedType(sprintf(
37
                'Given VO class "%s" does not implement required interface: %s',
38
                $valueImplementor,
39
                ValueObjectInterface::class
40
            ));
41
        }
42 10
        return new static($name, $valueImplementor);
43
    }
44
45 10
    public function makeValue($value = null, EntityInterface $parent = null): ValueObjectInterface
46
    {
47 10
        if (is_object($value)) {
48 1
            Assertion::isInstanceOf($value, $this->valueImplementor);
49 1
            return $value;
50
        }
51 10
        return call_user_func([$this->valueImplementor, 'fromNative'], $value);
52
    }
53
54
    public function getValueType(): string
55
    {
56
        return $this->valueImplementor;
57
    }
58
59 10
    public function getName(): string
60
    {
61 10
        return $this->name;
62
    }
63
64 10
    private function __construct(string $name, string $valueImplementor)
65
    {
66 10
        $this->name = $name;
67 10
        $this->valueImplementor = $valueImplementor;
68 10
    }
69
}
70