/
PdoStudentRepository.php
148 lines (117 loc) · 4.47 KB
/
PdoStudentRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Alura\Pdo\Infrastructure\Repository;
use Alura\Pdo\Domain\Model\Phone;
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Domain\Repository\StudentRepository;
use DateTimeImmutable;
use PDO;
use PDOStatement;
class PdoStudentRepository implements StudentRepository
{
private PDO $connection;
private PDOStatement $statement;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function allStudents(): array
{
$this->statement = $this->connection->query('SELECT * FROM students;');
return $this->hydrateStudentList($this->statement);
}
public function studentBirthAt(\DateTimeInterface $birthDate): array
{
$selectQuery = "SELECT * FROM students WHERE birth_date = ?;";
$this->statement = $this->connection->query($selectQuery);
$this->statement->bindValue(1, $birthDate->format('Y-m-d'));
$this->statement->execute();
return $this->hydrateStudentList($this->statement);
}
public function find(int $id): ?Student
{
$selectStudentQuery = 'SELECT * FROM students WHERE id = ?;';
$this->statement = $this->connection->prepare($selectStudentQuery);
$this->statement->bindValue(1, $id, PDO::PARAM_INT);
$this->statement->execute();
$studentList = $this->hydrateStudentList($this->statement);
return count($studentList) === 1 ? array_shift($studentList) : null;
}
private function hydrateStudentList(PDOStatement $statement): array
{
$studentDataList = $statement->fetchAll();
$studentList = [];
foreach ($studentDataList as $studentData) {
$studentList[] = new Student(
$studentData['id'],
$studentData['name'],
new DateTimeImmutable($studentData['birth_date'])
);
}
return $studentList;
}
public function save(Student $student): bool
{
if ($student->id() === null) {
return $this->insert($student);
}
return $this->update($student);
}
private function insert(Student $student): bool
{
$insertQuery = 'INSERT INTO students (name, birth_date) VALUES (:name, :birth_date);';
$this->statement = $this->connection->prepare($insertQuery);
$success = $this->statement->execute([
':name' => $student->name(),
':birth_date' => $student->birthDate()->format('Y-m-d')
]);
if ($success) {
$student->defineId($this->connection->lastInsertId());
}
return $success;
}
private function update(Student $student): bool
{
$updateQuery = 'UPDATE students SET name = :name, birth_date = :birth_date WHERE id = :id;';
$this->statement = $this->connection->prepare($updateQuery);
$this->statement->bindValue(':name', $student->name());
$this->statement->bindValue(':birth_date', $student->birthDate()->format('Y-m-d'));
$this->statement->bindValue(':id', $student->id(), PDO::PARAM_INT);
return $this->statement->execute();
}
public function remove(?Student $student): bool
{
if (is_null($student)) {
return false;
}
$this->statement = $this->connection->prepare('DELETE FROM students WHERE id = :id;');
$this->statement->bindValue(1, $student->id(), PDO::PARAM_INT);
return $this->statement->execute();
}
public function studentsWithPhones(): array
{
$sqlQuery = '
SELECT students.id,
students.name,
students.birth_date,
phones.id AS phone_id,
phones.area_code,
phones.number
FROM students
JOIN phones
ON students.id = phones.student_id;
';
$this->statement = $this->connection->query($sqlQuery);
return $this->addPhonesForStudent($this->statement);
}
private function addPhonesForStudent(PDOStatement $statement): array
{
$studentList = [];
foreach ($statement->fetchAll() as $row) {
if (!array_key_exists($row['id'], $studentList)) {
$studentList[$row['id']] = new Student($row['id'], $row['name'], new DateTimeImmutable($row['birth_date']));
}
$studentList[$row['id']]->addPhone($row['phone_id'], $row['area_code'], $row['number']);
}
return $studentList;
}
}