Passed
Push — master ( 492756...90c7de )
by Samuel
02:59
created

ClientData::createDateTimeImmutableString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Domain\Client\Data;
4
5
use App\Domain\Client\Enum\ClientVigilanceLevel;
6
7
class ClientData implements \JsonSerializable
8
{
9
    public ?int $id;
10
    // Optional values have to be init with null as they are used even when not set in client-read template
11
    public ?string $firstName = null;
12
    public ?string $lastName = null;
13
    // DateTimeImmutable to not change original reference when modified
14
    public ?\DateTimeImmutable $birthdate = null;
15
    public ?string $location = null;
16
    public ?string $phone = null;
17
    public ?string $email = null;
18
    // https://ocelot.ca/blog/blog/2013/09/16/representing-sex-in-databases/
19
    public ?string $sex = null; // ENUM 'F' -> Female; 'M' -> Male; 'O' -> Other; NULL -> Not applicable.
20
    public ?string $clientMessage = null; // Message that client submitted via webform
21
    public ?ClientVigilanceLevel $vigilanceLevel = null;
22
    public ?int $userId;
23
    public ?int $clientStatusId;
24
    public ?\DateTimeImmutable $updatedAt;
25
    public ?\DateTimeImmutable $createdAt;
26
    public ?\DateTimeImmutable $deletedAt;
27
28
    // Not a database field but here so that age doesn't have to be calculated in view
29
    public ?int $age = null;
30
31 44
    public function __construct(?array $clientData = [])
32
    {
33 44
        $this->id = $clientData['id'] ?? null;
34 44
        $this->firstName = $clientData['first_name'] ?? null;
35 44
        $this->lastName = $clientData['last_name'] ?? null;
36 44
        $this->birthdate = $this->createDateTimeImmutableString($clientData['birthdate'] ?? null);
37 44
        $this->location = $clientData['location'] ?? null;
38 44
        $this->phone = $clientData['phone'] ?? null;
39 44
        $this->email = $clientData['email'] ?? null;
40
        // Sex is validated to be either 'F', 'M', 'O', null or an empty string. If empty string, set to it null.
41 44
        $this->sex = !empty($clientData['sex']) ? $clientData['sex'] : null;
42 44
        $this->clientMessage = $clientData['client_message'] ?? null;
43 44
        $this->vigilanceLevel = $clientData['vigilance_level'] ?? null ?
44 44
            ClientVigilanceLevel::tryFrom($clientData['vigilance_level']) : null;
45
        // Cast to int if user id is set and not empty (null / empty string)
46 44
        $this->userId = !empty($clientData['user_id']) ? (int)$clientData['user_id'] : null;
47 44
        $this->clientStatusId = !empty($clientData['client_status_id']) ? (int)$clientData['client_status_id'] : null;
48 44
        $this->updatedAt = $this->createDateTimeImmutableString($clientData['updated_at'] ?? null);
49 44
        $this->createdAt = $this->createDateTimeImmutableString($clientData['created_at'] ?? null);
50 44
        $this->deletedAt = $this->createDateTimeImmutableString($clientData['deleted_at'] ?? null);
51 44
        if ($this->birthdate) {
52 42
            $this->age = (new \DateTime())->diff($this->birthdate)->y;
53
        }
54
    }
55
56 44
    private function createDateTimeImmutableString(?string $date): ?\DateTimeImmutable
57
    {
58 44
        return isset($date) ? new \DateTimeImmutable($date) : null;
59
    }
60
61
    /**
62
     * Returns all values of object as array for the database.
63
     * The array keys should match with the database
64
     * column names.
65
     *
66
     * @return array
67
     */
68 6
    public function toArrayForDatabase(): array
69
    {
70 6
        return [
71
            // id set below
72 6
            'first_name' => $this->firstName,
73 6
            'last_name' => $this->lastName,
74
            // If birthdate not null, return given format
75 6
            'birthdate' => $this->birthdate?->format('Y-m-d'),
76 6
            'location' => $this->location,
77 6
            'phone' => $this->phone,
78 6
            'email' => $this->email,
79 6
            'sex' => $this->sex,
80 6
            'client_message' => $this->clientMessage,
81 6
            'vigilance_level' => $this->vigilanceLevel?->value,
82 6
            'user_id' => $this->userId,
83 6
            'client_status_id' => $this->clientStatusId,
84 6
        ];
85
    }
86
87
    /**
88
     * Define how json_encode() should serialize the object
89
     * camelCase according to Google recommendation https://stackoverflow.com/a/19287394/9013718.
90
     *
91
     * @return array in the format expected by the frontend
92
     */
93 12
    public function jsonSerialize(): array
94
    {
95 12
        return [
96 12
            'id' => $this->id,
97 12
            'firstName' => $this->firstName,
98 12
            'lastName' => $this->lastName,
99 12
            'birthdate' => $this->birthdate?->format('Y-m-d'),
100 12
            'location' => $this->location,
101 12
            'phone' => $this->phone,
102 12
            'email' => $this->email,
103 12
            'sex' => $this->sex,
104 12
            'clientMessage' => $this->clientMessage,
105 12
            'vigilanceLevel' => $this->vigilanceLevel?->value,
106 12
            'userId' => $this->userId,
107 12
            'clientStatusId' => $this->clientStatusId,
108 12
            'updatedAt' => $this->updatedAt?->format('Y-m-d H:i:s'),
109 12
            'createdAt' => $this->createdAt?->format('Y-m-d H:i:s'),
110 12
            'deletedAt' => $this->deletedAt?->format('Y-m-d H:i:s'),
111 12
            'age' => $this->age,
112 12
        ];
113
    }
114
}
115