Passed
Push — master ( 3e2ea2...b63ef3 )
by Samuel
02:59
created

NoteData   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 69.23%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
eloc 47
c 2
b 1
f 0
dl 0
loc 81
ccs 27
cts 39
cp 0.6923
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A toArray() 0 20 4
A jsonSerialize() 0 17 4
1
<?php
2
3
namespace App\Domain\Note\Data;
4
5
use App\Domain\Authorization\Privilege;
6
use App\Domain\User\Data\UserData;
7
use IntlDateFormatter;
8
9
class NoteData implements \JsonSerializable
10
{
11
    public ?int $id;
12
    public ?int $userId;
13
    public ?int $clientId;
14
    public ?string $message;
15
    public ?int $isMain; // int 1 or 0
16
    public ?int $hidden; // int 1 or 0
17
    public ?\DateTimeImmutable $createdAt;
18
    public ?\DateTimeImmutable $updatedAt;
19
    public ?\DateTimeImmutable $deletedAt;
20
21
    // Not in database
22
    public ?UserData $user;
23
24
    // User mutation rights from authenticated user
25
    public string $privilege = Privilege::N->name; // json_encode automatically takes $enum->value
26
27 31
    public function __construct(?array $noteValues = null)
28
    {
29 31
        $this->id = $noteValues['id'] ?? null;
30 31
        $this->userId = $noteValues['user_id'] ?? null;
31 31
        $this->clientId = $noteValues['client_id'] ?? null;
32 31
        $this->message = $noteValues['message'] ?? null;
33 31
        $this->isMain = $noteValues['is_main'] ?? null;
34 31
        $this->hidden = $noteValues['hidden'] ?? null;
35 31
        $this->createdAt = $noteValues['created_at'] ?? null
36 31
            ? new \DateTimeImmutable($noteValues['created_at']) : null;
37 31
        $this->updatedAt = $noteValues['updated_at'] ?? null
38 31
            ? new \DateTimeImmutable($noteValues['updated_at']) : null;
39 31
        $this->deletedAt = $noteValues['deleted_at'] ?? null
40 31
            ? new \DateTimeImmutable($noteValues['deleted_at']) : null;
41
    }
42
43
    /**
44
     * Returns all values of object as array.
45
     * The array keys should match with the database
46
     * column names since it is likely used to
47
     * modify a database table.
48
     *
49
     * @return array
50
     */
51
    public function toArray(): array
52
    {
53
        $note = [];
54
        // Not include required, from db non-nullable values if they are null -> for update
55
        if ($this->id !== null) {
56
            $note['id'] = $this->id;
57
        }
58
        if ($this->userId !== null) {
59
            $note['user_id'] = $this->userId;
60
        }
61
        if ($this->clientId !== null) {
62
            $note['client_id'] = $this->clientId;
63
        }
64
65
        // Message is nullable and null is a valid value, so it has to be included
66
        $note['message'] = $this->message;
67
        $note['is_main'] = $this->isMain;
68
        $note['hidden'] = $this->hidden;
69
70
        return $note;
71
    }
72
73 8
    public function jsonSerialize(): array
74
    {
75 8
        $dateFormatter = new IntlDateFormatter(
76 8
            setlocale(LC_ALL, 0) ?: null,
77 8
            IntlDateFormatter::LONG,
78 8
            IntlDateFormatter::SHORT
79 8
        );
80
81 8
        return [
82 8
            'id' => $this->id,
83 8
            'userId' => $this->userId,
84 8
            'clientId' => $this->clientId,
85 8
            'message' => $this->message,
86
            // 'isMain' => $this->isMain,
87 8
            'hidden' => $this->hidden,
88 8
            'createdAt' => $this->createdAt ? $dateFormatter->format($this->createdAt) : null,
89 8
            'updatedAt' => $this->updatedAt ? $dateFormatter->format($this->updatedAt) : null,
90 8
        ];
91
    }
92
}
93