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

DataTransferObject::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
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
    /** @var array */
13
    protected $exceptKeys = [];
14
15
    /** @var array */
16
    protected $onlyKeys = [];
17
18
    protected $strict = true;
19
20
    /**
21
     * @param array $parameters
22
     *
23
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static
24
     */
25
    public static function immutable(array $parameters): ImmutableDataTransferObject
26
    {
27
        return new ImmutableDataTransferObject(new static($parameters));
28
    }
29
30
    public function __construct(array $parameters)
31
    {
32
        $this->hydrate($parameters);
33
    }
34
35
    public function hydrate(array $parameters)
36
    {
37
        $class = new ReflectionClass(static::class);
38
39
        $properties = $this->getPublicProperties($class);
40
41
        foreach ($properties as $property) {
42
            if (
43
                ! isset($parameters[$property->getName()])
44
                && ! $property->isDefault()
45
                && ! $property->isNullable()
46
            ) {
47
                throw DataTransferObjectError::uninitialized($property);
48
            }
49
50
            $value = $parameters[$property->getName()] ?? $property->getValue($this);
51
52
            $property->set($value);
53
54
            unset($parameters[$property->getName()]);
55
        }
56
57
        if ($this->strict && count($parameters)) {
58
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName());
59
        }
60
    }
61
62
    public function all(): array
63
    {
64
        $data = [];
65
66
        $class = new ReflectionClass(static::class);
67
68
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
69
70
        foreach ($properties as $reflectionProperty) {
71
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
72
        }
73
74
        return $data;
75
    }
76
77
    /**
78
     * @param string ...$keys
79
     *
80
     * @return static
81
     */
82
    public function only(string ...$keys): DataTransferObject
83
    {
84
        $valueObject = clone $this;
85
86
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
87
88
        return $valueObject;
89
    }
90
91
    /**
92
     * @param string ...$keys
93
     *
94
     * @return static
95
     */
96
    public function except(string ...$keys): DataTransferObject
97
    {
98
        $valueObject = clone $this;
99
100
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
101
102
        return $valueObject;
103
    }
104
105
    public function toArray(): array
106
    {
107
        if (count($this->onlyKeys)) {
108
            $array = Arr::only($this->all(), $this->onlyKeys);
109
        } else {
110
            $array = Arr::except($this->all(), $this->exceptKeys);
111
        }
112
113
        $array = $this->parseArray($array);
114
115
        return $array;
116
    }
117
118
    protected function parseArray(array $array): array
119
    {
120
        foreach ($array as $key => $value) {
121
            if (
122
                $value instanceof DataTransferObject
123
                || $value instanceof DataTransferObjectCollection
124
            ) {
125
                $array[$key] = $value->toArray();
126
127
                continue;
128
            }
129
130
            if (! is_array($value)) {
131
                continue;
132
            }
133
134
            $array[$key] = $this->parseArray($value);
135
        }
136
137
        return $array;
138
    }
139
140
    /**
141
     * @param \ReflectionClass $class
142
     *
143
     * @return array|\Spatie\DataTransferObject\Property[]
144
     */
145
    protected function getPublicProperties(ReflectionClass $class): array
146
    {
147
        $properties = [];
148
149
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
150
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
151
        }
152
153
        return $properties;
154
    }
155
}
156