1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace midorikocak\nanodb; |
||
6 | |||
7 | use Exception; |
||
8 | use midorikocak\arraytools\ArrayValidator; |
||
9 | use midorikocak\querymaker\QueryInterface; |
||
10 | |||
11 | use function array_key_exists; |
||
12 | |||
13 | /** |
||
14 | * Repository is a class that receives arrays as input data and returns array of arrays |
||
15 | */ |
||
16 | class ArrayRepository implements RepositoryInterface |
||
17 | { |
||
18 | private DatabaseInterface $db; |
||
19 | private ?ArrayValidator $validator; |
||
20 | private $tableName; |
||
21 | |||
22 | 6 | public function __construct( |
|
23 | string $tableName, |
||
24 | DatabaseInterface $db, |
||
25 | ?array $schema = null, |
||
26 | ?ArrayValidator $validator = null |
||
27 | ) { |
||
28 | 6 | $this->db = $db; |
|
29 | 6 | $this->tableName = $tableName; |
|
30 | 6 | $this->validator = $validator ?? new ArrayValidator(); |
|
31 | 6 | if ($schema) { |
|
32 | $this->validator->schema($schema); |
||
33 | } |
||
34 | 6 | } |
|
35 | |||
36 | 2 | private function checkData($data): void |
|
37 | { |
||
38 | 2 | if (!$this->validator->validate($data)) { |
|
0 ignored issues
–
show
|
|||
39 | throw new Exception('Invalid data'); |
||
40 | } |
||
41 | 2 | } |
|
42 | |||
43 | /** |
||
44 | * @inheritDoc |
||
45 | */ |
||
46 | 2 | public function save($data): array |
|
47 | { |
||
48 | 2 | if (array_key_exists('id', $data)) { |
|
49 | 1 | $this->checkData($data); |
|
50 | |||
51 | 1 | $id = $data['id']; |
|
52 | |||
53 | 1 | unset($data['id']); |
|
54 | |||
55 | 1 | $this->db->update($this->tableName, $data)->where('id', $id)->execute(); |
|
56 | |||
57 | 1 | return $this->read($id); |
|
58 | } |
||
59 | |||
60 | 1 | $this->checkData($data); |
|
61 | 1 | $this->db->insert($this->tableName, $data)->execute(); |
|
62 | |||
63 | 1 | $lastInsertId = $this->db->lastInsertId(); |
|
64 | 1 | return $this->db->select('users')->where('id', $lastInsertId)->fetch(); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | 5 | public function read(?string $id = null): array |
|
71 | { |
||
72 | 5 | if ($id) { |
|
73 | 5 | $this->db->select($this->tableName)->where('id', $id)->execute(); |
|
74 | } else { |
||
75 | $this->db->select($this->tableName)->execute(); |
||
76 | } |
||
77 | 5 | return $this->db->fetch(); |
|
78 | } |
||
79 | |||
80 | 1 | public function readAll(?QueryInterface $query = null): array |
|
81 | { |
||
82 | 1 | if ($query !== null) { |
|
83 | $db = $this->db->query($query); |
||
84 | } else { |
||
85 | 1 | $db = $this->db->select($this->tableName); |
|
86 | } |
||
87 | 1 | $db->execute(); |
|
88 | 1 | return $db->fetchAll(); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | 1 | public function remove($data): int |
|
95 | { |
||
96 | 1 | if (array_key_exists('id', $data)) { |
|
97 | 1 | $this->db->delete($this->tableName)->where('id', $data['id'])->execute(); |
|
98 | 1 | return $this->db->rowCount(); |
|
99 | } |
||
100 | return 0; |
||
101 | } |
||
102 | } |
||
103 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.