|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Bluz Framework Component |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Bluz PHP Team |
|
7
|
|
|
* @link https://github.com/bluzphp/framework |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Bluz\Crud; |
|
13
|
|
|
|
|
14
|
|
|
use Bluz\Common\Instance; |
|
15
|
|
|
use Bluz\Db\RowInterface; |
|
16
|
|
|
use Bluz\Http\Exception\NotImplementedException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Crud |
|
20
|
|
|
* |
|
21
|
|
|
* @package Bluz\Crud |
|
22
|
|
|
* @author Anton Shevchuk |
|
23
|
|
|
* @link https://github.com/bluzphp/framework/wiki/Crud |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AbstractCrud implements CrudInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use Instance; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array Fields for action |
|
31
|
|
|
* @todo should be different for Create, Read and Update |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fields = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return primary key signature |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract public function getPrimaryKey(): array; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
* |
|
45
|
|
|
* @throws NotImplementedException |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function readOne($primary) |
|
48
|
|
|
{ |
|
49
|
1 |
|
throw new NotImplementedException(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
* |
|
55
|
|
|
* @throws NotImplementedException |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function readSet(int $offset = 0, int $limit = self::DEFAULT_LIMIT, array $params = []) |
|
58
|
|
|
{ |
|
59
|
1 |
|
throw new NotImplementedException(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
* |
|
65
|
|
|
* @throws NotImplementedException |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function createOne(array $data) |
|
68
|
|
|
{ |
|
69
|
1 |
|
throw new NotImplementedException(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @throws NotImplementedException |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function createSet(array $data) |
|
78
|
|
|
{ |
|
79
|
1 |
|
throw new NotImplementedException(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
* |
|
85
|
|
|
* @throws NotImplementedException |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function updateOne($primary, array $data) |
|
88
|
|
|
{ |
|
89
|
1 |
|
throw new NotImplementedException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
* |
|
95
|
|
|
* @throws NotImplementedException |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function updateSet(array $data) |
|
98
|
|
|
{ |
|
99
|
1 |
|
throw new NotImplementedException(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
* |
|
105
|
|
|
* @throws NotImplementedException |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function deleteOne($primary) |
|
108
|
|
|
{ |
|
109
|
1 |
|
throw new NotImplementedException(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
* |
|
115
|
|
|
* @throws NotImplementedException |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function deleteSet(array $data) |
|
118
|
|
|
{ |
|
119
|
1 |
|
throw new NotImplementedException(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
6 |
|
public function getFields(): array |
|
126
|
|
|
{ |
|
127
|
6 |
|
return $this->fields; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param array $fields |
|
132
|
|
|
*/ |
|
133
|
24 |
|
public function setFields(array $fields): void |
|
134
|
|
|
{ |
|
135
|
24 |
|
$this->fields = $fields; |
|
136
|
24 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Filter input Fields |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $data Request |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
2 |
|
protected function filterData($data): array |
|
146
|
|
|
{ |
|
147
|
2 |
|
if (empty($this->getFields())) { |
|
148
|
|
|
return $data; |
|
149
|
|
|
} |
|
150
|
2 |
|
return array_intersect_key($data, array_flip($this->getFields())); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Filter output Row |
|
155
|
|
|
* |
|
156
|
|
|
* @param RowInterface $row from database |
|
157
|
|
|
* |
|
158
|
|
|
* @return RowInterface |
|
159
|
|
|
*/ |
|
160
|
2 |
|
protected function filterRow(RowInterface $row): RowInterface |
|
161
|
|
|
{ |
|
162
|
2 |
|
if (empty($this->getFields())) { |
|
163
|
1 |
|
return $row; |
|
164
|
|
|
} |
|
165
|
1 |
|
$fields = array_keys($row->toArray()); |
|
166
|
1 |
|
$toDelete = array_diff($fields, $this->getFields()); |
|
167
|
|
|
|
|
168
|
1 |
|
foreach ($toDelete as $field) { |
|
169
|
1 |
|
unset($row->$field); |
|
170
|
|
|
} |
|
171
|
1 |
|
return $row; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|