Completed
Push — master ( 91cd10...dc7d5f )
by Brent
09:16
created

ValueObject::validateType()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.6417
c 0
b 0
f 0
cc 6
nc 8
nop 2
1
<?php
2
3
namespace Spatie\ValueObject;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
8
abstract class ValueObject
9
{
10
11
    /** @var array */
12
    protected $exceptKeys = [];
13
14
    /** @var array */
15
    protected $onlyKeys = [];
16
17
    /** @var array */
18
    protected $fillable = [];
19
20
    public function __construct(array $parameters)
21
    {
22
        $class = new ReflectionClass(static::class);
23
24
        $properties = $this->getPublicProperties($class);
25
26
        foreach ($properties as $property) {
27
            $value = $parameters[$property->getName()] ?? null;
28
29
            $property->set($value);
30
        }
31
32
        foreach (array_keys($parameters) as $propertyName) {
33
            if (isset($properties[$propertyName])) {
34
                continue;
35
            }
36
37
            throw ValueObjectException::unknownPublicProperty($propertyName, $class);
38
        }
39
    }
40
41 View Code Duplication
    public function all(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $class = new ReflectionClass(static::class);
44
45
        $values = [];
46
47
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
48
            $values[$property->getName()] = $property->getValue($this);
49
        }
50
51
        return $values;
52
    }
53
54
    /**
55
     * @param string ...$keys
56
     *
57
     * @return static
58
     */
59
    public function only(string ...$keys)
60
    {
61
        $this->onlyKeys = array_merge($this->onlyKeys, $keys);
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string ...$keys
68
     *
69
     * @return static
70
     */
71
    public function except(string ...$keys)
72
    {
73
        $this->exceptKeys = array_merge($this->exceptKeys, $keys);
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return static
80
     */
81
    public function fillable()
82
    {
83
        $this->only(...$this->fillable);
84
85
        return $this;
86
    }
87
88
    public function toArray(): array
89
    {
90
        if (count($this->onlyKeys)) {
91
            return Arr::only($this->all(), $this->onlyKeys);
92
        }
93
94
        return Arr::except($this->all(), $this->exceptKeys);
95
    }
96
97
    /**
98
     * @param \ReflectionClass $class
99
     *
100
     * @return array|\Spatie\ValueObject\Property[]
101
     */
102 View Code Duplication
    private function getPublicProperties(ReflectionClass $class): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $properties = [];
105
106
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
107
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
108
        }
109
110
        return $properties;
111
    }
112
}
113