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 (#57)
by Brent
01:31
created

DataTransferObject   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 27
lcom 2
cbo 3
dl 0
loc 180
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A immutable() 0 10 2
B __construct() 0 30 6
A __get() 0 8 2
A __set() 0 15 4
A all() 0 14 2
A only() 0 8 1
A except() 0 8 1
A toArray() 0 12 2
A parseArray() 0 21 5
A getPublicProperties() 0 10 2
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
    private $propertyValues = [];
14
15
    /** @var \Spatie\DataTransferObject\Property[] */
16
    private $propertyDefinitions = [];
17
18
    /** @var array */
19
    protected $exceptKeys = [];
20
21
    /** @var array */
22
    protected $onlyKeys = [];
23
24
    /**
25
     * @param array $parameters
26
     *
27
     * @return static
28
     */
29
    public static function immutable(array $parameters): DataTransferObject
30
    {
31
        $dto = new static($parameters);
32
33
        foreach ($dto->propertyDefinitions as $propertyDefinition) {
34
            $propertyDefinition->markImmutable();
35
        }
36
37
        return $dto;
38
    }
39
40
    public function __construct(array $inputParameters)
41
    {
42
        $class = new ReflectionClass(static::class);
43
44
        $properties = $this->getPublicProperties($class);
45
46
        foreach ($properties as $property) {
47
            $propertyName = $property->getName();
48
49
            if (
50
                ! isset($inputParameters[$propertyName])
51
                && ! $property->isDefault()
52
                && ! $property->isNullable()
53
            ) {
54
                throw DataTransferObjectError::uninitialized($property);
55
            }
56
57
            $value = $inputParameters[$propertyName] ?? $property->getValue($this);
58
59
            unset($this->{$propertyName});
60
            unset($inputParameters[$propertyName]);
61
62
            $this->propertyDefinitions[$propertyName] = $property;
63
            $property->set($value);
64
        }
65
66
        if (count($inputParameters)) {
67
            throw DataTransferObjectError::unknownProperties(array_keys($inputParameters), $class->getName());
68
        }
69
    }
70
71
    public function __get($name)
72
    {
73
        if (! array_key_exists($name, $this->propertyDefinitions)) {
74
            throw DataTransferObjectError::fieldNotFound($name);
75
        }
76
77
        return $this->propertyValues[$name];
78
    }
79
80
    public function __set($name, $value)
81
    {
82
        if (! array_key_exists($name, $this->propertyDefinitions)) {
83
            throw DataTransferObjectError::fieldNotFound($name);
84
        }
85
86
        if (
87
            $this->propertyDefinitions[$name]->isInitialised()
88
            && $this->propertyDefinitions[$name]->isImmutable()
89
        ) {
90
            throw DataTransferObjectError::immutable($name);
91
        }
92
93
        $this->propertyValues[$name] = $value;
94
    }
95
96
    public function all(): array
97
    {
98
        $data = [];
99
100
        $class = new ReflectionClass(static::class);
101
102
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
103
104
        foreach ($properties as $reflectionProperty) {
105
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
106
        }
107
108
        return $data;
109
    }
110
111
    /**
112
     * @param string ...$keys
113
     *
114
     * @return static
115
     */
116
    public function only(string ...$keys): DataTransferObject
117
    {
118
        $valueObject = clone $this;
119
120
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
121
122
        return $valueObject;
123
    }
124
125
    /**
126
     * @param string ...$keys
127
     *
128
     * @return static
129
     */
130
    public function except(string ...$keys): DataTransferObject
131
    {
132
        $valueObject = clone $this;
133
134
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
135
136
        return $valueObject;
137
    }
138
139
    public function toArray(): array
140
    {
141
        if (count($this->onlyKeys)) {
142
            $array = Arr::only($this->all(), $this->onlyKeys);
143
        } else {
144
            $array = Arr::except($this->all(), $this->exceptKeys);
145
        }
146
147
        $array = $this->parseArray($array);
148
149
        return $array;
150
    }
151
152
    protected function parseArray(array $array): array
153
    {
154
        foreach ($array as $key => $value) {
155
            if (
156
                $value instanceof DataTransferObject
157
                || $value instanceof DataTransferObjectCollection
158
            ) {
159
                $array[$key] = $value->toArray();
160
161
                continue;
162
            }
163
164
            if (! is_array($value)) {
165
                continue;
166
            }
167
168
            $array[$key] = $this->parseArray($value);
169
        }
170
171
        return $array;
172
    }
173
174
    /**
175
     * @param \ReflectionClass $class
176
     *
177
     * @return array|\Spatie\DataTransferObject\Property[]
178
     */
179
    protected function getPublicProperties(ReflectionClass $class): array
180
    {
181
        $properties = [];
182
183
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
184
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
185
        }
186
187
        return $properties;
188
    }
189
}
190