1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use Modules\Entitizer; |
6
|
|
|
|
7
|
|
|
abstract class Dataset { |
8
|
|
|
|
9
|
|
|
protected $definition = null, $handlers = [], $data = []; |
10
|
|
|
|
11
|
|
|
# Add handler |
12
|
|
|
|
13
|
|
|
protected function addHandler(string $name, callable $handler) { |
14
|
|
|
|
15
|
|
|
if ((false !== $this->definition->param($name)) || isset($this->handlers[$name])) return; |
16
|
|
|
|
17
|
|
|
$this->handlers[$name] = $handler; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
# Constructor |
21
|
|
|
|
22
|
|
|
public function __construct() { |
23
|
|
|
|
24
|
|
|
$this->definition = Entitizer::definition(static::$table); |
25
|
|
|
|
26
|
|
|
$this->init(); $this->reset(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
# Reset data |
30
|
|
|
|
31
|
|
|
public function reset() { |
32
|
|
|
|
33
|
|
|
# Reset params |
34
|
|
|
|
35
|
|
|
foreach ($this->definition->params() as $name => $param) $this->data[$name] = $param->cast(null); |
36
|
|
|
|
37
|
|
|
# Reset extras |
38
|
|
|
|
39
|
|
|
foreach ($this->handlers as $name => $handler) $this->data[$name] = $handler($this->data); |
40
|
|
|
|
41
|
|
|
# Reset parent id |
42
|
|
|
|
43
|
|
|
if (static::$nesting) $this->data['parent_id'] = 0; |
44
|
|
|
|
45
|
|
|
# ------------------------ |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
# Update data |
51
|
|
|
|
52
|
|
|
public function update(array $data) { |
53
|
|
|
|
54
|
|
|
# Update params |
55
|
|
|
|
56
|
|
View Code Duplication |
foreach ($data as $name => $value) { |
|
|
|
|
57
|
|
|
|
58
|
|
|
if (false === ($param = $this->definition->param($name))) continue; |
59
|
|
|
|
60
|
|
|
$this->data[$name] = $param->cast($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
# Update extras |
64
|
|
|
|
65
|
|
|
foreach ($this->handlers as $name => $handler) $this->data[$name] = $handler($this->data); |
66
|
|
|
|
67
|
|
|
# Update parent id |
68
|
|
|
|
69
|
|
|
if (static::$nesting && isset($data['parent_id'])) { |
70
|
|
|
|
71
|
|
|
$this->data['parent_id'] = $this->definition->param('id')->cast($data['parent_id']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
# ------------------------ |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
# Cast data |
80
|
|
|
|
81
|
|
|
public function cast(array $data) { |
82
|
|
|
|
83
|
|
|
$cast = []; |
84
|
|
|
|
85
|
|
View Code Duplication |
foreach ($data as $name => $value) { |
|
|
|
|
86
|
|
|
|
87
|
|
|
if (false === ($param = $this->definition->param($name))) continue; |
88
|
|
|
|
89
|
|
|
$cast[$name] = $param->cast($value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
# ------------------------ |
93
|
|
|
|
94
|
|
|
return $cast; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
# Return data |
98
|
|
|
|
99
|
|
|
public function data() { |
100
|
|
|
|
101
|
|
|
return $this->data; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
# Return param value |
105
|
|
|
|
106
|
|
|
public function get(string $name) { |
107
|
|
|
|
108
|
|
|
return ($this->data[$name] ?? null); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
# Getter |
112
|
|
|
|
113
|
|
|
public function __get(string $name) { |
114
|
|
|
|
115
|
|
|
return ($this->data[$name] ?? null); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.