1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Page; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PageEntity |
9
|
|
|
* |
10
|
|
|
* @package Oc\Page |
11
|
|
|
* @author Nick Lubisch <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class PageEntity |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
public $id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $slug; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $metaKeywords; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $metaDescription; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $metaSocial; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var DateTime |
42
|
|
|
*/ |
43
|
|
|
public $updatedAt; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
public $active = true; |
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() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
'id' => $this->id, |
69
|
|
|
'slug' => $this->slug, |
70
|
|
|
'meta_keywords' => $this->metaKeywords, |
71
|
|
|
'meta_description' => $this->metaDescription, |
72
|
|
|
'meta_social' => $this->metaSocial, |
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) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->id = (int) $data['id']; |
88
|
|
|
$this->slug = (string) $data['slug']; |
89
|
|
|
$this->metaKeywords = (string) $data['meta_keywords']; |
90
|
|
|
$this->metaDescription = (string) $data['meta_description']; |
91
|
|
|
$this->metaSocial = (string) $data['meta_social']; |
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
|
|
|
|
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.