GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#37)
by Christopher
01:25
created

DataTransferObject::except()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\DataTransferObject;
6
7
use ReflectionClass;
8
use ReflectionProperty;
9
10
abstract class DataTransferObject
11
{
12
    /** @var array */
13
    protected $allValues = [];
14
15
    /** @var array */
16
    protected $exceptKeys = [];
17
18
    /** @var array */
19
    protected $onlyKeys = [];
20
21
    public function __construct(array $parameters)
22
    {
23
        // see http://php.net/manual/en/ini.core.php#ini.zend.assertions
24
        assert($this->isValidWith($parameters));
25
    }
26
27
    private function isValidWith(array $parameters): bool
28
    {
29
        $class = new ReflectionClass(static::class);
30
31
        $properties = $this->getPublicProperties($class);
32
33
        foreach ($properties as $property) {
34
            if (
35
                ! isset($parameters[$property->getName()])
36
                && ! $property->isNullable()
37
            ) {
38
                throw DataTransferObjectError::uninitialized($property);
39
            }
40
41
            $value = $parameters[$property->getName()] ?? null;
42
43
            $property->set($value);
44
45
            unset($parameters[$property->getName()]);
46
47
            $this->allValues[$property->getName()] = $property->getValue($this);
48
        }
49
50
        if (count($parameters)) {
51
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName());
52
        }
53
54
        return true;
55
    }
56
57
    public function all(): array
58
    {
59
        return $this->allValues;
60
    }
61
62
    /**
63
     * @param string ...$keys
64
     *
65
     * @return static
66
     */
67
    public function only(string ...$keys): DataTransferObject
68
    {
69
        $valueObject = clone $this;
70
71
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
72
73
        return $valueObject;
74
    }
75
76
    /**
77
     * @param string ...$keys
78
     *
79
     * @return static
80
     */
81
    public function except(string ...$keys): DataTransferObject
82
    {
83
        $valueObject = clone $this;
84
85
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
86
87
        return $valueObject;
88
    }
89
90
    public function toArray(): array
91
    {
92
        if (count($this->onlyKeys)) {
93
            return Arr::only($this->all(), $this->onlyKeys);
94
        }
95
96
        return Arr::except($this->all(), $this->exceptKeys);
97
    }
98
99
    /**
100
     * @param \ReflectionClass $class
101
     *
102
     * @return array|\Spatie\DataTransferObject\Property[]
103
     */
104
    protected function getPublicProperties(ReflectionClass $class): array
105
    {
106
        $properties = [];
107
108
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
109
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
110
        }
111
112
        return $properties;
113
    }
114
}
115