From ea7ddcde6555cf9264bdc88396e237f736d03b88 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 23 Jul 2023 14:39:22 +0300 Subject: [PATCH] set nullable: true for author property --- migrations/Version20230720115905.php | 2 +- src/Controller/PasteController.php | 2 +- src/DTO/PasteFormData.php | 7 +++---- src/Entity/Paste.php | 4 ++-- src/Form/Type/PasteForm.php | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/migrations/Version20230720115905.php b/migrations/Version20230720115905.php index cceeb1d..88821f2 100644 --- a/migrations/Version20230720115905.php +++ b/migrations/Version20230720115905.php @@ -13,7 +13,7 @@ final class Version20230720115905 extends AbstractMigration public function up(Schema $schema): void { $this->addSql('CREATE SEQUENCE paste_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); - $this->addSql('CREATE TABLE paste (id INT NOT NULL, text TEXT NOT NULL, language VARCHAR(25), description TEXT, filename VARCHAR(128), author VARCHAR(128) NOT NULL, publish_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, expiration_date TIMESTAMP(0) WITHOUT TIME ZONE, ip VARCHAR(15) NOT NULL, secret VARCHAR(40), PRIMARY KEY(id))'); + $this->addSql('CREATE TABLE paste (id INT NOT NULL, text TEXT NOT NULL, language VARCHAR(25), description TEXT, filename VARCHAR(128), author VARCHAR(128), publish_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, expiration_date TIMESTAMP(0) WITHOUT TIME ZONE, ip VARCHAR(15) NOT NULL, secret VARCHAR(40), PRIMARY KEY(id))'); } public function down(Schema $schema): void diff --git a/src/Controller/PasteController.php b/src/Controller/PasteController.php index f707eb4..6f411f6 100644 --- a/src/Controller/PasteController.php +++ b/src/Controller/PasteController.php @@ -37,7 +37,7 @@ class PasteController extends AbstractController } #[Route('/{id}/{secret}')] - public function showPaste(PasteRepository $pasteRepository, string $id, ?string $secret=NULL): Response + public function showPaste(PasteRepository $pasteRepository, string $id, ?string $secret = NULL): Response { $paste = $pasteRepository->findOneBy(['id' => $id, 'secret' => $secret]); $pasteData = new PasteFormData($paste); diff --git a/src/DTO/PasteFormData.php b/src/DTO/PasteFormData.php index 075b62a..f37ddba 100644 --- a/src/DTO/PasteFormData.php +++ b/src/DTO/PasteFormData.php @@ -15,11 +15,10 @@ class PasteFormData public ?string $language = null; public ?string $description = null; public ?string $filename = null; - #[Assert\NotBlank] - public string $author = 'anonymous'; + public ?string $author = null; public ?\DateTimeImmutable $expirationDate; - public function __construct(?Paste $paste=null) + public function __construct(?Paste $paste = null) { if ($paste === null) { @@ -30,7 +29,7 @@ class PasteFormData $this->language = $paste->language; $this->description = $paste->description; $this->filename = $paste->filename; - $this->author = $paste->author; + $this->author = $paste->author !== null ? $paste->author : 'anonymous'; $this->expirationDate = $paste->expirationDate; } } \ No newline at end of file diff --git a/src/Entity/Paste.php b/src/Entity/Paste.php index 11b91ce..9d4a42c 100644 --- a/src/Entity/Paste.php +++ b/src/Entity/Paste.php @@ -23,8 +23,8 @@ class Paste public readonly ?string $description, #[ORM\Column(type: 'string', length: 128, nullable: true)] public readonly ?string $filename, - #[ORM\Column(type: 'string', length: 128, nullable: false)] - public readonly string $author, + #[ORM\Column(type: 'string', length: 128, nullable: true)] + public readonly ?string $author, #[ORM\Column(type: 'datetime_immutable', nullable: false)] public readonly \DateTimeImmutable $publishDate, #[ORM\Column(type: 'datetime_immutable', nullable: true)] diff --git a/src/Form/Type/PasteForm.php b/src/Form/Type/PasteForm.php index e1481ce..d90dcce 100644 --- a/src/Form/Type/PasteForm.php +++ b/src/Form/Type/PasteForm.php @@ -28,7 +28,7 @@ class PasteForm extends AbstractType ) ->add('description', TextType::class, ['required' => false]) ->add('text', TextareaType::class) - ->add('author', TextType::class, ['attr' => ['maxlength' =>128]]) + ->add('author', TextType::class, ['attr' => ['maxlength' => 128], 'required' => false]) ->add('filename', TextType::class, ['required' => false, 'attr' => ['maxlength' =>128]]) ->add('expirationDate', DateTimeType::class, [ 'required' => false,