Completed
Pull Request — master (#451)
by Anton
13:12
created

AbstractCrud::getPrimaryKey()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
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\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 37
     *
44
     * @throws NotImplementedException
45 37
     */
46 37
    public function readOne($primary)
47 1
    {
48
        throw new NotImplementedException;
49 37
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @throws NotImplementedException
55
     */
56
    public function readSet($offset = 0, $limit = self::DEFAULT_LIMIT, $params = [])
57 1
    {
58
        throw new NotImplementedException;
59 1
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @throws NotImplementedException
65
     */
66
    public function createOne($data)
67 1
    {
68
        throw new NotImplementedException;
69 1
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @throws NotImplementedException
75
     */
76
    public function createSet($data)
77 1
    {
78
        throw new NotImplementedException;
79 1
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @throws NotImplementedException
85
     */
86
    public function updateOne($primary, $data)
87 1
    {
88
        throw new NotImplementedException;
89 1
    }
90
91
    /**
92
     * {@inheritdoc}
93
     *
94
     * @throws NotImplementedException
95
     */
96
    public function updateSet($data)
97 1
    {
98
        throw new NotImplementedException;
99 1
    }
100
101
    /**
102
     * {@inheritdoc}
103
     *
104
     * @throws NotImplementedException
105
     */
106
    public function deleteOne($primary)
107 1
    {
108
        throw new NotImplementedException;
109 1
    }
110
111
    /**
112
     * {@inheritdoc}
113
     *
114
     * @throws NotImplementedException
115
     */
116
    public function deleteSet($data)
117 1
    {
118
        throw new NotImplementedException;
119 1
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getFields() : array
125
    {
126
        return $this->fields;
127 1
    }
128
129 1
    /**
130
     * @param array $fields
131
     */
132
    public function setFields(array $fields) : void
133
    {
134
        $this->fields = $fields;
135 6
    }
136
137 6
    /**
138
     * Filter input Fields
139
     *
140
     * @param array $data Request
141
     *
142
     * @return array
143 24
     */
144
    protected function filterData($data) : array
145 24
    {
146 24
        if (empty($this->getFields())) {
147
            return $data;
148
        }
149
        return array_intersect_key($data, array_flip($this->getFields()));
150
    }
151
152
    /**
153
     * Filter output Row
154
     *
155 2
     * @param RowInterface $row from database
156
     *
157 2
     * @return RowInterface
158
     */
159
    protected function filterRow(RowInterface $row) : RowInterface
160 2
    {
161
        if (empty($this->getFields())) {
162
            return $row;
163
        }
164
        $fields = array_keys($row->toArray());
165
        $toDelete = array_diff($fields, $this->getFields());
166
167
        foreach ($toDelete as $field) {
168
            unset($row->$field);
169
        }
170 2
        return $row;
171
    }
172
}
173