Passed
Push — master ( 8bcb22...ee485a )
by Peter
02:20
created

File::toData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Domain\Entities;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use DateTime;
9
10
class File implements IStringerEntity
11
{
12
    const DATE_FORMAT = 'Y-m-d';
13
14
    /** @var string */
15
    protected $id;
16
17
    /** @var string */
18
    protected $filesystemName;
19
20
    /** @var string */
21
    protected $oldFilesystemName;
22
23
    /** @var string */
24
    protected $publicName;
25
26
    /** @var string */
27
    protected $mime;
28
29
    /** @var string */
30
    protected $description;
31
32
    /** @var DateTime */
33
    protected $uploadedAt;
34
35
    /** @var FileCategory|null */
36
    protected $category;
37
38
    /** @var bool */
39
    protected $writable;
40
41
    /** @var string|null */
42
    protected $content;
43
44
    /**
45
     * File constructor.
46
     *
47
     * @param string            $id
48
     * @param string            $filesystemName
49
     * @param string            $publicName
50
     * @param string            $mime
51
     * @param string            $description
52
     * @param FileCategory|null $category
53
     * @param DateTime|null     $uploadedAt
54
     * @param bool              $writable
55
     * @param string|null       $content
56
     *
57
     * @throws \Exception
58
     */
59
    public function __construct(
60
        string $id,
61
        string $filesystemName,
62
        string $publicName,
63
        string $mime,
64
        string $description,
65
        FileCategory $category = null,
66
        DateTime $uploadedAt = null,
67
        bool $writable = false,
68
        ?string $content = null
69
    ) {
70
        $this->id                = $id;
71
        $this->filesystemName    = $filesystemName;
72
        $this->oldFilesystemName = $filesystemName;
73
        $this->publicName        = $publicName;
74
        $this->mime              = $mime;
75
        $this->description       = $description;
76
        $this->category          = $category;
77
        $this->uploadedAt        = $uploadedAt ?: new DateTime();
78
        $this->writable          = $writable;
79
        $this->content           = $content;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getId()
86
    {
87
        return $this->id;
88
    }
89
90
    /**
91
     * @param string $id
92
     */
93
    public function setId($id)
94
    {
95
        $this->id = $id;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getFilesystemName(): string
102
    {
103
        return $this->filesystemName;
104
    }
105
106
    /**
107
     * @param string $filesystemName
108
     *
109
     * @return File
110
     */
111
    public function setFilesystemName(string $filesystemName): File
112
    {
113
        $this->filesystemName = $filesystemName;
114
115
        return $this;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getOldFilesystemName(): string
122
    {
123
        return $this->oldFilesystemName;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function isFileUploaded(): bool
130
    {
131
        return $this->oldFilesystemName !== $this->filesystemName;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getPublicName(): string
138
    {
139
        return $this->publicName;
140
    }
141
142
    /**
143
     * @param string $publicName
144
     *
145
     * @return File
146
     */
147
    public function setPublicName(string $publicName): File
148
    {
149
        $this->publicName = $publicName;
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getMime(): string
158
    {
159
        return $this->mime;
160
    }
161
162
    /**
163
     * @param string $mime
164
     *
165
     * @return File
166
     */
167
    public function setMime(string $mime): File
168
    {
169
        $this->mime = $mime;
170
171
        return $this;
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    public function getDescription(): string
178
    {
179
        return $this->description;
180
    }
181
182
    /**
183
     * @param string $description
184
     *
185
     * @return File
186
     */
187
    public function setDescription(string $description): File
188
    {
189
        $this->description = $description;
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return FileCategory|null
196
     */
197
    public function getCategory(): ?FileCategory
198
    {
199
        return $this->category;
200
    }
201
202
    /**
203
     * @param FileCategory|null $category
204
     *
205
     * @return File
206
     */
207
    public function setCategory(?FileCategory $category): File
208
    {
209
        $this->category = $category;
210
211
        return $this;
212
    }
213
214
    /**
215
     * @return DateTime
216
     */
217
    public function getUploadedAt(): DateTime
218
    {
219
        return $this->uploadedAt;
220
    }
221
222
    /**
223
     * @param DateTime $uploadedAt
224
     *
225
     * @return File
226
     */
227
    public function setUploadedAt(DateTime $uploadedAt): File
228
    {
229
        $this->uploadedAt = $uploadedAt;
230
231
        return $this;
232
    }
233
234
    /**
235
     * @return bool
236
     */
237
    public function isWritable(): bool
238
    {
239
        return $this->writable;
240
    }
241
242
    /**
243
     * @param bool $writable
244
     *
245
     * @return $this
246
     */
247
    public function setWritable(bool $writable): File
248
    {
249
        $this->writable = $writable;
250
251
        return $this;
252
    }
253
254
    /**
255
     * @return bool
256
     */
257
    public function hasContent(): bool
258
    {
259
        return !($this->content === null);
260
    }
261
262
    /**
263
     * @return string|null
264
     */
265
    public function getContent(): ?string
266
    {
267
        return $this->content;
268
    }
269
270
    /**
271
     * @param string|null $content
272
     */
273
    public function setContent(?string $content): void
274
    {
275
        $this->content = $content;
276
    }
277
278
    /**
279
     * @return string
280
     */
281
    public function __toString(): string
282
    {
283
        if (!$this->publicName) {
284
            return '#' . $this->getId();
285
        }
286
287
        return $this->publicName;
288
    }
289
290
    /**
291
     * @return array|null
292
     */
293
    public function toData(): ?array
294
    {
295
        $data = [
296
            'id'          => $this->getId(),
297
            'name'        => $this->getPublicName(),
298
            'mime'        => $this->getMime(),
299
            'description' => $this->getDescription(),
300
            'category_id' => $this->getCategory()->getId(),
301
            'uploaded_at' => $this->getUploadedAt()->format(\DateTime::ISO8601),
302
        ];
303
304
        if ($this->hasContent()) {
305
            $data['data'] = $this->getContent();
306
        }
307
308
        return $data;
309
    }
310
311
    /**
312
     * @return string
313
     */
314
    public function toJSON(): string
315
    {
316
        return json_encode($this->toData());
317
    }
318
}
319