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 (#101)
by
unknown
01:15
created

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