Passed
Pull Request — master (#10)
by Anatoly
02:53
created

EntrySerializer::serializeList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace App\Bundle\Example\Service;
4
5
/**
6
 * Import classes
7
 */
8
use App\Bundle\Example\Entity\Entry;
9
use DateTime;
10
use DateTimeInterface;
11
12
/**
13
 * EntrySerializer
14
 */
15
final class EntrySerializer
16
{
17
18
    /**
19
     * @OpenApi\Schema(
20
     *   refName="Entry",
21
     *   type="object",
22
     *   properties={
23
     *     "id": @OpenApi\Schema(
24
     *       type="string",
25
     *       format="uuid",
26
     *       nullable=false,
27
     *     ),
28
     *     "name": @OpenApi\SchemaReference(
29
     *       class="App\Bundle\Example\Entity\Entry",
30
     *       property="name",
31
     *     ),
32
     *     "slug": @OpenApi\SchemaReference(
33
     *       class="App\Bundle\Example\Entity\Entry",
34
     *       property="slug",
35
     *     ),
36
     *     "createdAt": @OpenApi\Schema(
37
     *       type="string",
38
     *       format="date-time",
39
     *       nullable=false,
40
     *     ),
41
     *     "updatedAt": @OpenApi\Schema(
42
     *       type="string",
43
     *       format="date-time",
44
     *       nullable=true,
45
     *     ),
46
     *   },
47
     * )
48
     *
49
     * @param Entry $entry
50
     *
51
     * @return array
52
     */
53 4
    public function serialize(Entry $entry) : array
54
    {
55
        return [
56 4
            'id' => (string) $entry->getId(),
57 4
            'name' => $entry->getName(),
58 4
            'slug' => $entry->getSlug(),
59 4
            'createdAt' => $this->dateTimeSerialize($entry->getCreatedAt()),
60 4
            'updatedAt' => $this->dateTimeSerialize($entry->getUpdatedAt()),
61
        ];
62
    }
63
64
    /**
65
     * @OpenApi\Schema(
66
     *   refName="EntryList",
67
     *   type="array",
68
     *   items=@OpenApi\SchemaReference(
69
     *     class="App\Bundle\Example\Service\EntrySerializer",
70
     *     method="serialize",
71
     *   ),
72
     * )
73
     *
74
     * @param Entry ...$entries
75
     *
76
     * @return array
77
     */
78 1
    public function serializeList(Entry ...$entries) : array
79
    {
80 1
        $result = [];
81 1
        foreach ($entries as $entry) {
82 1
            $result[] = $this->serialize($entry);
83
        }
84
85 1
        return $result;
86
    }
87
88
    /**
89
     * Use the nullable operator if the application runned on PHP8 (see below)
90
     *
91
     * @param null|DateTimeInterface $dateTime
92
     *
93
     * @return null|string
94
     */
95 4
    public function dateTimeSerialize(?DateTimeInterface $dateTime) : ?string
96
    {
97 4
        if (null === $dateTime) {
98 3
            return null;
99
        }
100
101 2
        return $dateTime->format(DateTime::W3C);
102
    }
103
}
104