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 ( b4985c...2d21f4 )
by Ruben
03:45
created

ComponentPayload::property()   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 2
1
<?php
2
3
namespace Spatie\IcalendarGenerator;
4
5
use Closure;
6
use Exception;
7
use DateTimeInterface;
8
use Spatie\IcalendarGenerator\Components\Component;
9
use Spatie\IcalendarGenerator\PropertyTypes\PropertyType;
10
use Spatie\IcalendarGenerator\PropertyTypes\TextPropertyType;
11
use Spatie\IcalendarGenerator\PropertyTypes\DateTimePropertyType;
12
13
final class ComponentPayload
14
{
15
    /** @var string */
16
    private $type;
17
18
    /** @var array */
19
    private $properties = [];
20
21
    /** @var array */
22
    private $subComponents = [];
23
24
    /** @var array */
25
    private $aliases = [];
26
27
    public static function create(string $type): ComponentPayload
28
    {
29
        return new self($type);
30
    }
31
32
    public function __construct(string $type)
33
    {
34
        $this->type = $type;
35
    }
36
37
    public function property(PropertyType $property, array $parameters = null): ComponentPayload
38
    {
39
        $property->addParameters($parameters ?? []);
40
41
        $this->properties[] = $property;
42
43
        return $this;
44
    }
45
46
    public function dateTimeProperty(
47
        string $name,
48
        ?DateTimeInterface $value,
49
        bool $withTime = false,
50
        bool $withTimeZone = false
51
    ): ComponentPayload {
52
        if ($value === null) {
53
            return $this;
54
        }
55
56
        return $this->property(new DateTimePropertyType($name, $value, $withTime, $withTimeZone));
57
    }
58
59
    public function textProperty(
60
        string $name,
61
        ?string $value
62
    ): ComponentPayload {
63
        if ($value === null) {
64
            return $this;
65
        }
66
67
        return $this->property(new TextPropertyType($name, $value));
68
    }
69
70
    public function subComponent(Component ...$components): ComponentPayload
71
    {
72
        foreach ($components as $component) {
73
            $this->subComponents[] = $component;
74
        }
75
76
        return $this;
77
    }
78
79
    public function alias(string $propertyName, array $aliases): ComponentPayload
80
    {
81
        $this->aliases[$propertyName] = $aliases;
82
83
        return $this;
84
    }
85
86
    public function when(bool $condition, Closure $closure): ComponentPayload
87
    {
88
        if ($condition) {
89
            $closure($this);
90
        }
91
92
        return $this;
93
    }
94
95
    public function getType(): string
96
    {
97
        return $this->type;
98
    }
99
100
    public function getProperties(): array
101
    {
102
        return $this->properties;
103
    }
104
105
    public function getProperty(string $name): PropertyType
106
    {
107
        $filteredProperties = array_filter(
108
            $this->properties,
109
            function (PropertyType $property) use ($name) {
110
                return $property->getName() === $name;
111
            }
112
        );
113
114
        $properties = array_values($filteredProperties);
115
116
        if (count($properties) === 0) {
117
            throw new Exception("Property `{$name}` does not exist in the payload");
118
        }
119
120
        return $properties[0];
121
    }
122
123
    public function getAliasesForProperty(string $name): array
124
    {
125
        foreach ($this->aliases as $propertyName => $aliases) {
126
            if ($name === $propertyName) {
127
                return $aliases;
128
            }
129
        }
130
131
        return [];
132
    }
133
134
    public function getSubComponents(): array
135
    {
136
        return $this->subComponents;
137
    }
138
}
139