Já revi o código dezenas de vezes e não consigo entender o que tem de errado
Minha Classe:
<?php
namespace Alura\Julio\Modelo;
class Video
{
public readonly int $id;
public readonly string $url;
private ?string $filePath;
public function __construct(
public readonly string $titulo,
string $url
)
{
$this->setUrl($url);
}
public function setId(int $id): void
{
$this->id = $id;
}
public function setUrl(string $url): void
{
if (filter_var($url, FILTER_VALIDATE_URL) === false)
{
throw new \InvalidArgumentException();
}
$this->url = $url;
}
public function setFilePath(string $filePath): void
{
$this->filePath = $filePath;
}
public function getFilePath(): ?string
{
return $this->filePath;
}
}
Meu repositorio:
<?php
namespace Alura\Julio\Repository;
use PDO;
use Alura\Julio\Modelo\Video;
class VideoRepository
{
public function __construct(
private PDO $pdo
)
{
$this->pdo = $pdo;
}
public function adiciona(Video $video): Video
{
$sql = "INSERT INTO videos (titulo, url, image_path) VALUES (:titulo, :url, :image_path)";
$statement = $this->pdo->prepare($sql);
$statement->bindValue(':titulo', $video->titulo);
$statement->bindValue(':url', $video->url);
$statement->bindValue(':image_path', $video->getFilePath());
$statement->execute();
$id = $this->pdo->lastInsertId();
$video->setId(intval($id));
return $video;
}
public function remove(int $id)
{
$sql = "DELETE FROM videos WHERE id = ?";
$statement = $this->pdo->prepare($sql);
$statement->bindValue(1, $id);
$statement->execute();
}
public function edita(Video $video)
{
$updateImageSql = '';
if ($video->getFilePath() !== null) {
$updateImageSql = ', image_path = :image_path';
}
$sql = "UPDATE videos SET
titulo = :titulo,
url = :url
$updateImageSql
WHERE id = :id";
$statement = $this->pdo->prepare($sql);
$statement->bindValue(':titulo', $video->titulo);
$statement->bindValue(':url', $video->url);
$statement->bindValue(':id', $video->id, PDO::PARAM_INT);
if ($video->getFilePath() !== null) {
$statement->bindValue(':image_path', $video->getFilePath());
}
$statement->execute();
}
public function busca(int $id)
{
$sql = 'SELECT * FROM videos WHERE id = ?';
$statement = $this->pdo->prepare($sql);
$statement->bindValue(1, $id, PDO::PARAM_INT);
$statement->execute();
return $this->hydrateVideo($statement->fetch(PDO::FETCH_ASSOC));
}
public function hydrateVideo(array $videoData): Video
{
$video = new Video($videoData['titulo'], $videoData['url']);
$video->setId($videoData['id']);
if ($videoData['image_path'] !== null){
$video->setFilePath($videoData['image_path']);
}
return $video;
}
/**
* @return Video[]
*/
public function lista(): array
{
$sql = "SELECT * FROM videos";
$videoList = $this->pdo
->query($sql)
->fetchAll(PDO::FETCH_ASSOC);
return array_map($this->hydrateVideo(...), $videoList
);
}
}
Minha listagem está identica a do Vinicius