1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Repository; |
6
|
|
|
|
7
|
|
|
use App\Exception\NoteException; |
8
|
|
|
use App\Entity\Note; |
9
|
|
|
|
10
|
|
|
final class NoteRepository extends BaseRepository |
11
|
|
|
{ |
12
|
7 |
|
public function checkAndGetNote(int $noteId): Note |
13
|
|
|
{ |
14
|
7 |
|
$query = 'SELECT * FROM `notes` WHERE `id` = :id'; |
15
|
7 |
|
$statement = $this->getDb()->prepare($query); |
16
|
7 |
|
$statement->bindParam('id', $noteId); |
17
|
7 |
|
$statement->execute(); |
18
|
7 |
|
$note = $statement->fetchObject(Note::class); |
19
|
7 |
|
if (!$note) { |
20
|
3 |
|
throw new NoteException('Note not found.', 404); |
21
|
|
|
} |
22
|
|
|
|
23
|
4 |
|
return $note; |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function getAllNotes(): array |
27
|
|
|
{ |
28
|
1 |
|
$query = 'SELECT * FROM `notes` ORDER BY `id`'; |
29
|
1 |
|
$statement = $this->getDb()->prepare($query); |
30
|
1 |
|
$statement->execute(); |
31
|
|
|
|
32
|
1 |
|
return (array) $statement->fetchAll(); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function getQueryNotesByPage(): string |
36
|
|
|
{ |
37
|
2 |
|
return " |
38
|
|
|
SELECT * |
39
|
|
|
FROM `notes` |
40
|
|
|
WHERE `name` LIKE CONCAT('%', :name, '%') |
41
|
|
|
AND `description` LIKE CONCAT('%', :description, '%') |
42
|
|
|
ORDER BY `id` |
43
|
|
|
"; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
public function getNotesByPage( |
47
|
|
|
int $page, |
48
|
|
|
int $perPage, |
49
|
|
|
?string $name, |
50
|
|
|
?string $description |
51
|
|
|
): array { |
52
|
|
|
$params = [ |
53
|
2 |
|
'name' => is_null($name) ? '' : $name, |
54
|
2 |
|
'description' => is_null($description) ? '' : $description, |
55
|
|
|
]; |
56
|
2 |
|
$query = $this->getQueryNotesByPage(); |
57
|
2 |
|
$statement = $this->getDb()->prepare($query); |
58
|
2 |
|
$statement->bindParam('name', $params['name']); |
59
|
2 |
|
$statement->bindParam('description', $params['description']); |
60
|
2 |
|
$statement->execute(); |
61
|
2 |
|
$total = $statement->rowCount(); |
62
|
|
|
|
63
|
2 |
|
return $this->getResultsWithPagination( |
64
|
2 |
|
$query, |
65
|
|
|
$page, |
66
|
|
|
$perPage, |
67
|
|
|
$params, |
68
|
|
|
$total |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function search(string $strNotes): array |
73
|
|
|
{ |
74
|
2 |
|
$query = ' |
75
|
|
|
SELECT * |
76
|
|
|
FROM `notes` |
77
|
|
|
WHERE `name` LIKE :name OR `description` LIKE :description |
78
|
|
|
ORDER BY `id` |
79
|
|
|
'; |
80
|
2 |
|
$name = '%' . $strNotes . '%'; |
81
|
2 |
|
$description = '%' . $strNotes . '%'; |
82
|
2 |
|
$statement = $this->getDb()->prepare($query); |
83
|
2 |
|
$statement->bindParam('name', $name); |
84
|
2 |
|
$statement->bindParam('description', $description); |
85
|
2 |
|
$statement->execute(); |
86
|
2 |
|
$notes = (array) $statement->fetchAll(); |
87
|
2 |
|
if (!$notes) { |
|
|
|
|
88
|
1 |
|
$message = 'No notes were found with that name or description.'; |
89
|
1 |
|
throw new NoteException($message, 404); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $notes; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function create(Note $note): Note |
96
|
|
|
{ |
97
|
1 |
|
$query = ' |
98
|
|
|
INSERT INTO `notes` |
99
|
|
|
(`name`, `description`, `createdAt`) |
100
|
|
|
VALUES |
101
|
|
|
(:name, :description, :createdAt) |
102
|
|
|
'; |
103
|
1 |
|
$statement = $this->getDb()->prepare($query); |
104
|
1 |
|
$name = $note->getName(); |
105
|
1 |
|
$desc = $note->getDescription(); |
106
|
1 |
|
$created = $note->getCreatedAt(); |
107
|
1 |
|
$statement->bindParam('name', $name); |
108
|
1 |
|
$statement->bindParam('description', $desc); |
109
|
1 |
|
$statement->bindParam('createdAt', $created); |
110
|
1 |
|
$statement->execute(); |
111
|
|
|
|
112
|
1 |
|
return $this->checkAndGetNote((int) $this->getDb()->lastInsertId()); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function update(Note $note): Note |
116
|
|
|
{ |
117
|
2 |
|
$query = ' |
118
|
|
|
UPDATE `notes` |
119
|
|
|
SET |
120
|
|
|
`name` = :name, |
121
|
|
|
`description` = :description, |
122
|
|
|
`updatedAt` = :updatedAt |
123
|
|
|
WHERE `id` = :id |
124
|
|
|
'; |
125
|
2 |
|
$statement = $this->getDb()->prepare($query); |
126
|
2 |
|
$id = $note->getId(); |
127
|
2 |
|
$name = $note->getName(); |
128
|
2 |
|
$desc = $note->getDescription(); |
129
|
2 |
|
$updated = $note->getUpdatedAt(); |
130
|
2 |
|
$statement->bindParam('id', $id); |
131
|
2 |
|
$statement->bindParam('name', $name); |
132
|
2 |
|
$statement->bindParam('description', $desc); |
133
|
2 |
|
$statement->bindParam('updatedAt', $updated); |
134
|
2 |
|
$statement->execute(); |
135
|
|
|
|
136
|
2 |
|
return $this->checkAndGetNote((int) $id); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function delete(int $noteId): void |
140
|
|
|
{ |
141
|
1 |
|
$query = 'DELETE FROM `notes` WHERE `id` = :id'; |
142
|
1 |
|
$statement = $this->getDb()->prepare($query); |
143
|
1 |
|
$statement->bindParam('id', $noteId); |
144
|
1 |
|
$statement->execute(); |
145
|
1 |
|
} |
146
|
|
|
} |
147
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.