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