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
Push — master ( 9d8638...60b218 )
by Brent
16s
created

DataTransferObject::convertForArray()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.0444
c 0
b 0
f 0
cc 6
nc 4
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
        $class = new ReflectionClass(static::class);
24
25
        $properties = $this->getPublicProperties($class);
26
27
        foreach ($properties as $property) {
28
            if (
29
                ! isset($parameters[$property->getName()])
30
                && ! $property->isNullable()
31
            ) {
32
                throw DataTransferObjectError::uninitialized($property);
33
            }
34
35
            $value = $parameters[$property->getName()] ?? null;
36
37
            $property->set($value);
38
39
            unset($parameters[$property->getName()]);
40
41
            $this->allValues[$property->getName()] = $property->getValue($this);
42
        }
43
44
        if (count($parameters)) {
45
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName());
46
        }
47
    }
48
49
    public function all(): array
50
    {
51
        return $this->allValues;
52
    }
53
54
    /**
55
     * @param string ...$keys
56
     *
57
     * @return static
58
     */
59
    public function only(string ...$keys): DataTransferObject
60
    {
61
        $valueObject = clone $this;
62
63
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
64
65
        return $valueObject;
66
    }
67
68
    /**
69
     * @param string ...$keys
70
     *
71
     * @return static
72
     */
73
    public function except(string ...$keys): DataTransferObject
74
    {
75
        $valueObject = clone $this;
76
77
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
78
79
        return $valueObject;
80
    }
81
82
    public function toArray(): array
83
    {
84
        if (count($this->onlyKeys)) {
85
            $array = Arr::only($this->all(), $this->onlyKeys);
86
        } else {
87
            $array = Arr::except($this->all(), $this->exceptKeys);
88
        }
89
90
        foreach ($array as $key => $property) {
91
            $array[$key] = $this->convertForArray($property);
92
        }
93
94
        return $array;
95
    }
96
97
    /**
98
     * @param mixed $property
99
     *
100
     * @return mixed
101
     */
102
    protected function convertForArray($property)
103
    {
104
        if ($property instanceof DataTransferObject) {
105
            return $property->toArray();
106
        }
107
108
        if (is_object($property) && method_exists($property, 'toArray')) {
109
            return $property->toArray();
110
        }
111
112
        if (is_array($property)) {
113
            foreach ($property as $key => $nested) {
114
                $property[$key] = $this->convertForArray($nested);
115
            }
116
        }
117
118
        return $property;
119
    }
120
121
    /**
122
     * @param \ReflectionClass $class
123
     *
124
     * @return array|\Spatie\DataTransferObject\Property[]
125
     */
126
    protected function getPublicProperties(ReflectionClass $class): array
127
    {
128
        $properties = [];
129
130
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
131
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
132
        }
133
134
        return $properties;
135
    }
136
}
137