Completed
Pull Request — development (#546)
by Nick
06:41
created

UserEntity::toDatabaseArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\User;
4
5
/**
6
 * Class UserEntity
7
 *
8
 * @package Oc\User
9
 */
10
class UserEntity
11
{
12
    /**
13
     * @var int
14
     */
15
    public $id;
16
17
    /**
18
     * @var string
19
     */
20
    public $username;
21
22
    /**
23
     * @var string
24
     */
25
    public $password;
26
27
    /**
28
     * @var string
29
     */
30
    public $email;
31
32
    /**
33
     * @var double
34
     */
35
    public $latitude;
36
37
    /**
38
     * @var double
39
     */
40
    public $longitude;
41
42
    /**
43
     * @var bool
44
     */
45
    public $isActive;
46
47
    /**
48
     * @var string
49
     */
50
    public $firstname;
51
52
    /**
53
     * @var string
54
     */
55
    public $lastname;
56
57
    /**
58
     * @var string
59
     */
60
    public $country;
61
62
    /**
63
     * @var string
64
     */
65
    public $language;
66
67
    /**
68
     * Checks if the entity is new.
69
     *
70
     * @return bool
71
     */
72
    public function isNew()
73
    {
74
        return $this->id === null;
75
    }
76
77
    /**
78
     * Sets properties from the database.
79
     *
80
     * @return array
81
     */
82
    public function toDatabaseArray()
83
    {
84
        return [
85
            'user_id' => $this->id,
86
            'username' => $this->username,
87
            'password' => $this->password,
88
            'email' => $this->email,
89
            'latitude' => $this->latitude,
90
            'longitude' => $this->longitude,
91
            'is_active_flag' => $this->isActive,
92
            'first_name' => $this->firstname,
93
            'last_name' => $this->lastname,
94
            'country' => $this->country,
95
            'language' => $this->language,
96
        ];
97
    }
98
99
    /**
100
     * Prepares database array from properties.
101
     *
102
     * @param array $data
103
     *
104
     * @return self
105
     */
106
    public function fromDatabaseArray(array $data)
107
    {
108
        $this->id = (int) $data['user_id'];
109
        $this->username = (string) $data['username'];
110
        $this->password = (string) $data['password'];
111
        $this->email = (string) $data['email'];
112
        $this->latitude = (double) $data['latitude'];
113
        $this->longitude = (double) $data['longitude'];
114
        $this->isActive = (bool) $data['is_active_flag'];
115
        $this->firstname = (string) $data['first_name'];
116
        $this->lastname = (string) $data['last_name'];
117
        $this->country = (string) $data['country'];
118
        $this->language = strtolower($data['language']);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Sets all properties from array.
125
     *
126
     * @param array $data
127
     */
128
    public function fromArray(array $data)
129
    {
130
        foreach ($data as $key => $value) {
131
            if (!property_exists($this, $key)) {
132
                continue;
133
            }
134
135
            $this->{$key} = $value;
136
        }
137
    }
138
139
    /**
140
     * Returns all properties as array.
141
     *
142
     * @return array
143
     */
144
    public function toArray()
145
    {
146
        return get_object_vars($this);
147
    }
148
}
149