Passed
Push — master ( 6d0252...9c45b0 )
by Francis
01:39
created

Event::getContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LiveStream\Resources;
4
5
use stdClass;
6
use LiveStream\Interfaces\Resource;
7
8
class Event implements Resource
9
{
10
    /**
11
     * Resource Payload.
12
     *
13
     * @var object
14
     */
15
    private $data;
16
17
    /**
18
     * Class Constructor.
19
     *
20
     * @param string  $fullName
21
     * @param boolean $init
22
     */
23
    public function __construct(string $fullName, bool $init = true)
24
    {
25
        if ($init) {
26
            $this->data = new stdClass();
27
            $this->data->fullName  = $fullName;
28
        }
29
    }
30
31
    /**
32
     * Factory Method.
33
     *
34
     * @param  object $object
35
     * @return \LiveStream\Resources\Event|null
36
     */
37
    public static function fromObject(?object $object): ?Event
38
    {
39
        if ($object == null) return null;
40
41
        $instance = new static(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $fullName of LiveStream\Resources\Event::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $instance = new static(/** @scrutinizer ignore-type */ false);
Loading history...
42
        $instance->data = $object;
43
        return $instance;
44
    }
45
46
    /**
47
     * Magic Setter Method
48
     *
49
     * @param  string $key
50
     * @param  mixed  $value
51
     * @return void
52
     */
53
    public function __set(string $key, $value): void
54
    {
55
        $this->data->$key = $value;
56
    }
57
58
    /**
59
     * Magic Getter Method
60
     *
61
     * @param  string $key
62
     * @return void
63
     */
64
    public function __get(string $key)
65
    {
66
        return $this->data->$key ?? null;
67
    }
68
69
    /**
70
     * Magic Method Isset.
71
     *
72
     * @param  string  $key
73
     * @return boolean
74
     */
75
    public function __isset(string $key)
76
    {
77
        return isset($this->data->$key);
78
    }
79
80
    /**
81
     * Set Full Name.
82
     *
83
     * @param  string $fullName
84
     * @return \LiveStream\Resources\Event
85
     */
86
    public function setFullName(string $fullName): Event
87
    {
88
        $this->data->fullName = $fullName;
89
        return $this;
90
    }
91
92
    /**
93
     * Get Full Name.
94
     *
95
     * @return string|null
96
     */
97
    public function getFullName(): ?string
98
    {
99
        return $this->data->fullName ?? null;
100
    }
101
102
    /**
103
     * Set Event Start Time.
104
     *
105
     * @param  string $strtime
106
     * @return \LiveStream\Resources\Event
107
     */
108
    public function setStartTime(string $strtime): Event
109
    {
110
        $this->data->startTime = date('c', strtotime($strtime));
111
        return $this;
112
    }
113
114
    /**
115
     * Get Start Time
116
     *
117
     * @return string
118
     */
119
    public function getStartTime(): ?string
120
    {
121
        return $this->data->startTime ?? null;
122
    }
123
124
    /**
125
     * Set End Time
126
     *
127
     * @param  string $strtime
128
     * @return \LiveStream\Resources\Event
129
     */
130
    public function setEndTime(string $strtime): Event
131
    {
132
        $this->data->endTime = date('c', strtotime($strtime));
133
        return $this;
134
    }
135
136
    /**
137
     * Get End Time.
138
     *
139
     * @return string
140
     */
141
    public function getEndTime(): ?string
142
    {
143
        return $this->data->endTime ?? null;
144
    }
145
146
    /**
147
     * Set Is Draft.
148
     *
149
     * @param boolean $isDraft
150
     * 
151
     * @return \LiveStream\Resources\Event
152
     */
153
    public function setIsDraft(bool $isDraft = true): Event
154
    {
155
        $this->data->draft = $isDraft;
156
        return $this;
157
    }
158
159
    /**
160
     * Get Is Draft.
161
     *
162
     * @return boolean
163
     */
164
    public function isDraft(): bool
165
    {
166
        return $this->data->draft ?? true;
167
    }
168
169
    /**
170
     * Set Event Short Name.
171
     *
172
     * @param string $shortName
173
     * @return Event
174
     */
175
    public function setShortName(string $shortName): Event
176
    {
177
        $this->data->shortName = $shortName;
178
        return $this;
179
    }
180
181
    /**
182
     * Get Event Short Name.
183
     *
184
     * @return string|null
185
     */
186
    public function getShortName(): ?string
187
    {
188
        return $this->data->shortName ?? null;
189
    }
190
191
    /**
192
     * Set Event Description
193
     *
194
     * @param  string $description
195
     * @return Event
196
     */
197
    public function setDescription(string $description): Event
198
    {
199
        $this->data->description = $description;
200
        return $this;
201
    }
202
203
    /**
204
     * Get Description.
205
     *
206
     * @return string|null
207
     */
208
    public function getDescription(): ?string
209
    {
210
        return $this->data->description ?? null;
211
    }
212
213
    /**
214
     * Add Event Tag
215
     *
216
     * @param  string $tag
217
     * @return \LiveStream\Resources\Event
218
     */
219
    public function addTag(string $tag): Event
220
    {
221
        if (!isset($this->data->tags)) $this->data->tags = '';
222
223
        $this->data->tags .= rtrim($tag, ',') . ',';
224
225
        return $this;
226
    }
227
228
    /**
229
     * Get Tags.
230
     *
231
     * @return string
232
     */
233
    public function getTags(): string
234
    {
235
        return $this->data->tags ?? '';
236
    }
237
238
    /**
239
     * Resource Interface Method: Get Resource as FormURLEncoded String.
240
     *
241
     * @return string
242
     */
243
    public function getRawBody(): string
244
    {
245
        $body = ['fullName' => $this->data->fullName];
246
247
        if ($this->data->shortName ?? null) $body['shortName'] = $this->data->shortName;
248
        if ($this->data->startTime ?? null) $body['startTime'] = $this->data->startTime;
249
        if ($this->data->endTime ?? null) $body['endTime'] = $this->data->endTime;
250
        if ($this->data->draft ?? null) $body['draft'] = $this->data->draft;
251
        if ($this->data->description ?? null) $body['description'] = $this->data->description;
252
        if ($this->data->tags ?? null) $body['tags'] = rtrim($this->data->tags, ',');
253
254
        return json_encode($body);
255
    }
256
257
    /**
258
     * Undocumented function
259
     *
260
     * @return string
261
     */
262
    public function getContentType(): string
263
    {
264
        return 'application/json';
265
    }
266
}
267