1
resposta

Erro com DateTimeImutable

Estou com esse erro no insert na chamada do Repository:

PHP Fatal error:  Declaration of Alura\Pdo\Infrastructure\Repository\PdoStudentRepository::studentsBirthDateAt(DateTimeImmutable $birthDate): array must be compatible with Alura\Pdo\Domain\Repository\StudentRepository::studentsBirthDateAt(): array in /home/path/Cursos/Alura/Avancando-PHP/php-pdo-projeto-inicial/src/Infrastructure/Repository/PdoStudentRepository.php on line 26

O meu criar-turma:

<?php

use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Infrastructure\Persistence\ConnectionCreator;
use Alura\Pdo\Infrastructure\Repository\PdoStudentRepository;

require_once "vendor/autoload.php";

$connection = ConnectionCreator::createConnection();
$studentRepository = new PdoStudentRepository($connection);

//realizo processos de definição da turma

$connection->beginTransaction();

$aStudent = new Student(null, 'Rivaldo', new DateTimeImmutable('1985-05-01'));
$studentRepository->save($aStudent);

$anotherStudent = new Student(null, 'Roberto Carlos', new DateTimeImmutable('1985-05-01'));
$studentRepository->save($anotherStudent);

$connection->commit();

E o meu PdoStudentRepository:

<?php

namespace Alura\Pdo\Infrastructure\Repository;

use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Domain\Repository\StudentRepository;
use PDO;

class PdoStudentRepository implements StudentRepository
{
    private PDO $connection;

    public function __construct(PDO $connection)
    {
        $this->connection = $connection;
    }

    public function allStudent(): array
    {
        // TODO: Implement allStudent() method.
        $statement = $this->query('SELECT * FROM students');

        return $this->hydrateStudentList($statement);
    }

    public function studentsBirthDateAt(\DateTimeImmutable $birthDate): array
    {
        // TODO: Implement studentsBirthDateAt() method.
        $statement = $this->connection->prepare('SELECT * FROM students WHERE birth_date = ?');
        $statement->bindValue(1, $birthDate->format('Y-m-a'));
        $statement->execute();


        return $this->hydrateStudentList($statement);
    }

    public function save(Student $student): bool
    {
        // TODO: Implement save() method.
        if ($student->id() === NULL){
            return $this->insert($student);
        }

        return $this->update($student);

    }

    public function insert(Student $student): bool
    {
        $sqlInsert = "INSERT INTO students (name, birth_date) VALUES (:name, :birth_date);";
        $statement = $this->connection->prepare($sqlInsert);

        $success = ([
            ':name' => $student->name(),
            ':birth_date' => $student->birthDate()->format('Y-m-d')
        ]);

        if ($success){
            $student->defineId($this->connection->lastInsertId());
        }

        return $success;
    }

    public function update(Student $student): bool
    {
        $sqlUpdate = 'UPDATE students SET name = :name, birth_date = :birth_date WHERE id = :id';
        $statmentUp = $this->connection->prepare($sqlUpdate);
        $statmentUp->bindValue(':name', $student->name());
        $statmentUp->bindValue(':birth_date', $student->birth_date()->format('Y-m-a'));
        $statmentUp->bindValue(':id', $student->id(), PDO::PARAM_INT);

        return $statmentUp->execute();
    }

    public function remove(Student $student): bool
    {
        // TODO: Implement remove() method.
        $sqlDelete = 'DELETE FROM students WHERE id = ?';
        $statementDelete = $this->connection->prepare($sqlDelete);
        $statementDelete->bindValue(1, $student->id(), \PDO::PARAM_INT);

        return $statementDelete->execute();
    }

    private function hydrateStudentList(\PDOStatement $statement): array
    {
        $studentDataList = $statement->fetchAll(PDO::FETCH_ASSOC);

        $studentList = [];

        foreach ($studentDataList as $studentData) {
            $studentList[] = new Student($studentData['id'], $studentData['name'], new DateTimeImmutable($studentData['birth_date']));

        }

        return $studentList;
    }
}

No Repository fica apontando essa inconsistencia com o PHPStorm:

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

1 resposta

Sua interface foi definida sem o parâmetro. Precisa adicionar o parâmetro ao método na interface também.