Segue o PdoStudentRepository.php:
<?php
namespace Alura\Pdo\Infraestructure\Repository;
use Alura\Pdo\Domain\Model\Phone;
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Domain\Repository\StudentRepository;
use DateTimeImmutable;
use DateTimeInterface;
use PDO;
use PDOStatement;
class PdoStudentRepository implements StudentRepository
{
private PDO $connection;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function allStudents(): array
{
$sqlQuery = 'SELECT * FROM student;';
$statement = $this->connection->query($sqlQuery);
return $this->hydrateStudentList($statement);
}
public function update(Student $student): bool
{
$sqlUpdate = 'UPDATE student SET name = :name, birth_date = :birth_date WHERE id = :id;';
$statement = $this->connection->prepare($sqlUpdate);
$statement->bindValue(':name', $student->name());
$statement->bindValue(':birth_date', $student->birthDate()->format('Y-m-d'));
$statement->bindValue(':id', $student->id(), PDO::PARAM_INT);
return $statement->execute();
}
public function insert(Student $student): bool
{
$sqlInsert = "INSERT INTO student (name,birth_date) VALUES (:name, :birth_date);";
$statement = $this->connection->prepare($sqlInsert);
$success = $statement->execute([
':name' => $student->name(),
':birth_date' => $student->birthDate()->format('Y-m-d')
]);
if ($success) $student->defineId($this->connection->lastInsertId());
return $success;
}
public function studentBirthDate(DateTimeInterface $birthDate): array
{
$sqlQuery = 'SELECT * FROM student WHERE birth_date = ?;';
$statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(1, $birthDate->format('Y-m-d'));
$statement->execute();
return $this->hydrateStudentList($statement);
}
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);
}
public function remove(Student $student): bool
{
$statement = $this->connection->prepare("DELETE FROM student WHERE id= ?;");
$statement->bindValue(1, $student->id(), PDO::PARAM_INT);
return $statement->execute();
}
public function studentsWithPhones(): array
{
$sqlQuery = 'SELECT student.id,
student.name,
student.birth_date,
phones.id AS phone_id,
phones.area_code,
phones.number
FROM student
JOIN phones ON student.id = phones.student_id;';
$statement = $this->connection->query($sqlQuery);
$result = $statement->fetchAll();
$studentList = [];
foreach ($result as $row) {
if (!array_key_exists($row['id'], $studentList)) {
$studentList[$row['id']] = new Student(
$row['id'],
$row['name'],
new DateTimeImmutable($row['birth_date'])
);
}
$phone = new Phone($row['phone_id'], $row['area_code'], $row['number']);
$studentList[$row['id']]->addPhone($phone);
}
return $studentList;
}
}