Completed
Pull Request — master (#437)
by Anton
04:37
created

AbstractCrud::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Application\Exception\NotImplementedException;
14
use Bluz\Db\Row;
15
16
/**
17
 * Crud
18
 *
19
 * @package  Bluz\Crud
20
 * @author   Anton Shevchuk
21
 * @link     https://github.com/bluzphp/framework/wiki/Crud
22
 */
23
abstract class AbstractCrud
24
{
25
    /**
26
     * Default limit for READ SET of elements
27
     */
28
    const DEFAULT_LIMIT = 10;
29
30
    /**
31
     * @var array Fields for action
32
     */
33
    protected $fields = [];
34
35
    /**
36
     * Get CRUD Instance
37
     *
38
     * @return static
39
     */
40 16
    public static function getInstance()
41
    {
42 16
        static $instance;
43 16
        if (null === $instance) {
44 1
            $instance = new static();
45
        }
46
47 16
        return $instance;
48
    }
49
50
    /**
51
     * Return primary key signature
52
     *
53
     * @return array
54
     */
55
    abstract public function getPrimaryKey();
56
57
    /**
58
     * Get item by primary key(s)
59
     *
60
     * @param  mixed $primary
61
     *
62
     * @return mixed
63
     * @throws NotImplementedException
64
     */
65 1
    public function readOne($primary)
66
    {
67 1
        throw new NotImplementedException;
68
    }
69
70
    /**
71
     * Get collection of items
72
     *
73
     * @param  integer $offset
74
     * @param  integer $limit
75
     * @param  array   $params
76
     * @param  null    $total
77
     *
78
     * @return mixed
79
     * @throws NotImplementedException
80
     */
81 1
    public function readSet($offset = 0, $limit = self::DEFAULT_LIMIT, $params = [], &$total = null)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83 1
        throw new NotImplementedException;
84
    }
85
86
    /**
87
     * Create new item
88
     *
89
     * @param  array $data
90
     *
91
     * @return mixed
92
     * @throws NotImplementedException
93
     */
94 1
    public function createOne($data)
95
    {
96 1
        throw new NotImplementedException;
97
    }
98
99
    /**
100
     * Create items
101
     *
102
     * @param  array $data
103
     *
104
     * @return mixed
105
     * @throws NotImplementedException
106
     */
107 1
    public function createSet($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109 1
        throw new NotImplementedException;
110
    }
111
112
    /**
113
     * Update item
114
     *
115
     * @param  mixed $primary
116
     * @param  array $data
117
     *
118
     * @return integer
119
     * @throws NotImplementedException
120
     */
121 1
    public function updateOne($primary, $data)
122
    {
123 1
        throw new NotImplementedException;
124
    }
125
126
    /**
127
     * Update items
128
     *
129
     * @param  array $data
130
     *
131
     * @return integer
132
     * @throws NotImplementedException
133
     */
134 1
    public function updateSet($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136 1
        throw new NotImplementedException;
137
    }
138
139
    /**
140
     * Delete item
141
     *
142
     * @param  mixed $primary
143
     *
144
     * @return integer
145
     * @throws NotImplementedException
146
     */
147 1
    public function deleteOne($primary)
148
    {
149 1
        throw new NotImplementedException;
150
    }
151
152
    /**
153
     * Delete items
154
     *
155
     * @param  array $data
156
     *
157
     * @return integer
158
     * @throws NotImplementedException
159
     */
160 1
    public function deleteSet($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162 1
        throw new NotImplementedException;
163
    }
164
165
    /**
166
     * @return array
167
     */
168 7
    public function getFields(): array
169
    {
170 7
        return $this->fields;
171
    }
172
173
    /**
174
     * Setup data filters
175
     *
176
     * @param array $fields
177
     */
178 15
    public function setFields(array $fields)
179
    {
180 15
        $this->fields = $fields;
181 15
    }
182
183
    /**
184
     * Filter input Fields
185
     *
186
     * @param array $data Request
187
     *
188
     * @return array
189
     */
190 2
    protected function filterData($data) : array
191
    {
192 2
        if (empty($this->getFields())) {
193 2
            return $data;
194
        }
195
        return array_intersect_key($data, array_flip($this->getFields()));
196
    }
197
198
    /**
199
     * Filter output Row
200
     *
201
     * @param Row $row from database
202
     *
203
     * @return Row
204
     */
205 3
    protected function filterRow($row)
206
    {
207 3
        if (empty($this->getFields())) {
208 2
            return $row;
209
        }
210 1
        $fields = array_keys($row->toArray());
211 1
        $toDelete = array_diff($fields, $this->getFields());
212
213 1
        foreach ($toDelete as $field) {
214 1
            unset($row->$field);
215
        }
216 1
        return $row;
217
    }
218
}
219