Completed
Push — master ( 624cd5...fb9048 )
by Brent
01:24
created

ValueObject::fillable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ValueObject;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
8
abstract class ValueObject
9
{
10
    /** @var array */
11
    protected $exceptKeys = [];
12
13
    /** @var array */
14
    protected $onlyKeys = [];
15
16
    public function __construct(array $parameters)
17
    {
18
        $class = new ReflectionClass(static::class);
19
20
        $properties = $this->getPublicProperties($class);
21
22
        foreach ($properties as $property) {
23
            $value = $parameters[$property->getName()] ?? null;
24
25
            $property->set($value);
26
        }
27
28
        foreach (array_keys($parameters) as $propertyName) {
29
            if (isset($properties[$propertyName])) {
30
                continue;
31
            }
32
33
            throw ValueObjectException::unknownPublicProperty($propertyName, $class);
34
        }
35
    }
36
37 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...
38
    {
39
        $class = new ReflectionClass(static::class);
40
41
        $values = [];
42
43
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
44
            $values[$property->getName()] = $property->getValue($this);
45
        }
46
47
        return $values;
48
    }
49
50
    /**
51
     * @param string ...$keys
52
     *
53
     * @return static
54
     */
55
    public function only(string ...$keys): ValueObject
56
    {
57
        $valueObject = clone $this;
58
59
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
60
61
        return $valueObject;
62
    }
63
64
    /**
65
     * @param string ...$keys
66
     *
67
     * @return static
68
     */
69
    public function except(string ...$keys): ValueObject
70
    {
71
        $valueObject = clone $this;
72
73
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
74
75
        return $valueObject;
76
    }
77
78
    public function toArray(): array
79
    {
80
        if (count($this->onlyKeys)) {
81
            return Arr::only($this->all(), $this->onlyKeys);
82
        }
83
84
        return Arr::except($this->all(), $this->exceptKeys);
85
    }
86
87
    /**
88
     * @param \ReflectionClass $class
89
     *
90
     * @return array|\Spatie\ValueObject\Property[]
91
     */
92 View Code Duplication
    protected 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...
93
    {
94
        $properties = [];
95
96
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
97
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
98
        }
99
100
        return $properties;
101
    }
102
}
103