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 (#77)
by Brent
02:38 queued 01:32
created

DataTransferObject::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
    protected bool $ignoreMissing = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
13
14
    protected array $exceptKeys = [];
15
16
    protected array $onlyKeys = [];
17
18
    /**
19
     * @param array $parameters
20
     *
21
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static
22
     */
23
    public static function immutable(array $parameters = []): ImmutableDataTransferObject
24
    {
25
        return new ImmutableDataTransferObject(new static($parameters));
26
    }
27
28
    /**
29
     * @param array $arrayOfParameters
30
     *
31
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject[]|static[]
32
     */
33
    public static function arrayOf(array $arrayOfParameters): array
34
    {
35
        return array_map(
36
            function ($parameters) {
37
                return new static($parameters);
38
            },
39
            $arrayOfParameters
40
        );
41
    }
42
43
    public function __construct(array $parameters = [])
44
    {
45
        $validators = $this->getFieldValidators();
46
47
        $valueCaster = $this->getValueCaster();
48
49
        foreach ($validators as $field => $validator) {
50
            if (
51
                ! isset($parameters[$field])
52
                && ! $validator->hasDefaultValue
53
                && ! $validator->isNullable
54
            ) {
55
                throw DataTransferObjectError::uninitialized(
56
                    static::class,
57
                    $field
58
                );
59
            }
60
61
            $value = $parameters[$field] ?? $this->{$field} ?? null;
62
63
            $value = $this->castValue($valueCaster, $validator, $value);
64
65
            if (! $validator->isValidType($value)) {
66
                throw DataTransferObjectError::invalidType(
67
                    static::class,
68
                    $field,
69
                    $validator->allowedTypes,
70
                    $value
71
                );
72
            }
73
74
            $this->{$field} = $value;
75
76
            unset($parameters[$field]);
77
        }
78
79
        if (! $this->ignoreMissing && count($parameters)) {
80
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class);
81
        }
82
    }
83
84
    public function all(): array
85
    {
86
        $data = [];
87
88
        $class = new ReflectionClass(static::class);
89
90
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
91
92
        foreach ($properties as $reflectionProperty) {
93
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
94
        }
95
96
        return $data;
97
    }
98
99
    /**
100
     * @param string ...$keys
101
     *
102
     * @return static
103
     */
104
    public function only(string ...$keys): DataTransferObject
105
    {
106
        $dataTransferObject = clone $this;
107
108
        $dataTransferObject->onlyKeys = [...$this->onlyKeys, ...$keys];
109
110
        return $dataTransferObject;
111
    }
112
113
    /**
114
     * @param string ...$keys
115
     *
116
     * @return static
117
     */
118
    public function except(string ...$keys): DataTransferObject
119
    {
120
        $dataTransferObject = clone $this;
121
122
        $dataTransferObject->exceptKeys = [...$this->exceptKeys, ...$keys];
123
124
        return $dataTransferObject;
125
    }
126
127
    public function toArray(): array
128
    {
129
        if (count($this->onlyKeys)) {
130
            $array = Arr::only($this->all(), $this->onlyKeys);
131
        } else {
132
            $array = Arr::except($this->all(), $this->exceptKeys);
133
        }
134
135
        $array = $this->parseArray($array);
136
137
        return $array;
138
    }
139
140
    protected function parseArray(array $array): array
141
    {
142
        foreach ($array as $key => $value) {
143
            if (
144
                $value instanceof DataTransferObject
145
                || $value instanceof DataTransferObjectCollection
146
            ) {
147
                $array[$key] = $value->toArray();
148
149
                continue;
150
            }
151
152
            if (! is_array($value)) {
153
                continue;
154
            }
155
156
            $array[$key] = $this->parseArray($value);
157
        }
158
159
        return $array;
160
    }
161
162
    /**
163
     * @param \ReflectionClass $class
164
     *
165
     * @return \Spatie\DataTransferObject\FieldValidator[]
166
     */
167
    protected function getFieldValidators(): array
168
    {
169
        return DTOCache::resolve(static::class, function () {
170
            $class = new ReflectionClass(static::class);
171
172
            $properties = [];
173
174
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
175
                // Skip static properties
176
                if ($reflectionProperty->isStatic()) {
177
                    continue;
178
                }
179
180
                $field = $reflectionProperty->getName();
181
182
                $properties[$field] = FieldValidator::fromReflection($reflectionProperty);
183
            }
184
185
            return $properties;
186
        });
187
    }
188
189
    /**
190
     * @param \Spatie\DataTransferObject\ValueCaster $valueCaster
191
     * @param \Spatie\DataTransferObject\FieldValidator $fieldValidator
192
     * @param mixed $value
193
     *
194
     * @return mixed
195
     */
196
    protected function castValue(ValueCaster $valueCaster, FieldValidator $fieldValidator, $value)
197
    {
198
        if (is_array($value)) {
199
            return $valueCaster->cast($value, $fieldValidator);
200
        }
201
202
        return $value;
203
    }
204
205
    protected function getValueCaster(): ValueCaster
206
    {
207
        return new ValueCaster();
208
    }
209
}
210