From ad837045fd00b2c0cc24576583ed3db3088267ce Mon Sep 17 00:00:00 2001 From: Sergio Mendolia Date: Wed, 6 Sep 2023 12:05:16 +0200 Subject: [PATCH] Fix adding array instead of object --- src/Entity/Book.php | 16 +++++++++++----- src/Repository/BookRepository.php | 2 ++ src/Twig/InlineEditBook.php | 6 +++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 0b1047a8..e6ed161a 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -123,7 +123,7 @@ public function setTitle(string $title): self { $this->title = trim($title); - if ($title === '') { + if ('' === $title) { $this->title = 'unknown'; } @@ -240,7 +240,7 @@ public function getSerie(): ?string public function setSerie(?string $serie): static { $serie = trim($serie ?? ''); - if ($serie === '') { + if ('' === $serie) { $serie = null; } $this->serie = $serie; @@ -306,6 +306,7 @@ public function getAuthors(): array /** * @param array $authors + * * @return $this */ public function setAuthors(array $authors): static @@ -317,9 +318,10 @@ public function setAuthors(array $authors): static public function addAuthor(string $author): static { - if (!in_array($author, $this->authors, true)) { + if (!in_array($author, $this->authors, true) && '' !== $author) { $this->authors[] = $author; } + $this->authors = array_values($this->authors); return $this; } @@ -331,25 +333,27 @@ public function removeAuthor(string $author): static unset($this->authors[$key]); } } + $this->authors = array_values($this->authors); return $this; } public function addTag(string $tag): static { - if ($this->tags === null) { + if (null === $this->tags) { $this->tags = []; } if (!in_array($tag, $this->tags, true)) { $this->tags[] = $tag; } + $this->tags = array_values($this->tags); return $this; } public function removeTag(string $tag): static { - if ($this->tags === null) { + if (null === $this->tags) { $this->tags = []; } foreach ($this->tags as $key => $value) { @@ -357,6 +361,7 @@ public function removeTag(string $tag): static unset($this->tags[$key]); } } + $this->tags = array_values($this->tags); return $this; } @@ -433,6 +438,7 @@ public function getTags(): ?array /** * @param array|null $tags + * * @return $this */ public function setTags(?array $tags): static diff --git a/src/Repository/BookRepository.php b/src/Repository/BookRepository.php index 24f6b6f2..317a4b52 100644 --- a/src/Repository/BookRepository.php +++ b/src/Repository/BookRepository.php @@ -199,6 +199,8 @@ private function convertResults(mixed $intermediateResults): array } } + ksort($results); + return $results; } } diff --git a/src/Twig/InlineEditBook.php b/src/Twig/InlineEditBook.php index 5ae609f6..96d10bff 100644 --- a/src/Twig/InlineEditBook.php +++ b/src/Twig/InlineEditBook.php @@ -54,12 +54,12 @@ public function usesuggestion(#[LiveArg] string $field, #[LiveArg] string $sugge $to_call = 'set'.ucfirst($field); $value = $this->suggestions[$field][$suggestion]; if (is_callable([$this->book, $to_call])) { - /* @phpstan-ignore-next-line */ - if ($field === 'tags') { + if ('tags' === $field) { $this->book->addTag($value); - } elseif ($field === 'authors') { + } elseif ('authors' === $field) { $this->book->addAuthor($value); } else { + /* @phpstan-ignore-next-line */ $this->book->$to_call($value); } }