Completed
Pull Request — master (#451)
by Anton
05:28
created

AbstractCrud   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
dl 0
loc 149
ccs 32
cts 33
cp 0.9697
c 0
b 0
f 0
rs 10
wmc 15
lcom 1
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
getPrimaryKey() 0 1 ?
A readSet() 0 4 1
A createOne() 0 4 1
A createSet() 0 4 1
A updateOne() 0 4 1
A updateSet() 0 4 1
A deleteOne() 0 4 1
A deleteSet() 0 4 1
A readOne() 0 4 1
A getFields() 0 4 1
A setFields() 0 4 1
A filterData() 0 7 2
A filterRow() 0 13 3
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