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

BlockEntity::fromDatabaseArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 12
loc 12
rs 9.4285
1
<?php
2
3
namespace Oc\Page;
4
5
use DateTime;
6
7
/**
8
 * Class BlockEntity
9
 *
10
 * @package Oc\Page
11
 * @author Nick Lubisch <[email protected]>
12
 */
13
class BlockEntity
14
{
15
    /**
16
     * @var int
17
     */
18
    public $id;
19
20
    /**
21
     * @var int
22
     */
23
    public $pageId;
24
25
    /**
26
     * @var string
27
     */
28
    public $title;
29
30
    /**
31
     * @var string
32
     */
33
    public $html;
34
35
    /**
36
     * @var int
37
     */
38
    public $position;
39
40
    /**
41
     * @var DateTime
42
     */
43
    public $updatedAt;
44
45
    /**
46
     * @var bool
47
     */
48
    public $active;
49
50
    /**
51
     * Checks if the entity is new.
52
     *
53
     * @return bool
54
     */
55
    public function isNew()
56
    {
57
        return $this->id === null;
58
    }
59
60
    /**
61
     * Sets properties from the database.
62
     *
63
     * @return array
64
     */
65 View Code Duplication
    public function toDatabaseArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        return [
68
            'id' => $this->id,
69
            'page_id' => $this->pageId,
70
            'title' => $this->title,
71
            'html' => $this->html,
72
            'position' => (int) $this->position,
73
            'updated_at' => $this->updatedAt->format(DateTime::ATOM),
74
            'active' => $this->active
75
        ];
76
    }
77
78
    /**
79
     * Prepares database array from properties.
80
     *
81
     * @param array $data
82
     *
83
     * @return self
84
     */
85 View Code Duplication
    public function fromDatabaseArray(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $this->id = (int) $data['id'];
88
        $this->pageId = (int) $data['page_id'];
89
        $this->title = (string) $data['title'];
90
        $this->html = (string) $data['html'];
91
        $this->position = (int) $data['position'];
92
        $this->updatedAt = new DateTime($data['updated_at']);
93
        $this->active = (bool) $data['active'];
94
95
        return $this;
96
    }
97
98
    /**
99
     * Sets all properties from array.
100
     *
101
     * @param array $data
102
     */
103
    public function fromArray(array $data)
104
    {
105
        foreach ($data as $key => $value) {
106
            if (!property_exists($this, $key)) {
107
                continue;
108
            }
109
110
            $this->{$key} = $value;
111
        }
112
    }
113
114
    /**
115
     * Returns all properties as array.
116
     *
117
     * @return array
118
     */
119
    public function toArray()
120
    {
121
        return get_object_vars($this);
122
    }
123
}
124