Skip to content

Commit

Permalink
Fix adding array instead of object
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 6, 2023
1 parent e43a100 commit ad83704
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function setTitle(string $title): self
{
$this->title = trim($title);

if ($title === '') {
if ('' === $title) {
$this->title = 'unknown';
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -306,6 +306,7 @@ public function getAuthors(): array

/**
* @param array<string> $authors
*
* @return $this
*/
public function setAuthors(array $authors): static
Expand All @@ -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;
}
Expand All @@ -331,32 +333,35 @@ 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) {
if ($value === $tag) {
unset($this->tags[$key]);
}
}
$this->tags = array_values($this->tags);

return $this;
}
Expand Down Expand Up @@ -433,6 +438,7 @@ public function getTags(): ?array

/**
* @param array<string>|null $tags
*
* @return $this
*/
public function setTags(?array $tags): static
Expand Down
2 changes: 2 additions & 0 deletions src/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ private function convertResults(mixed $intermediateResults): array
}
}

ksort($results);

return $results;
}
}
6 changes: 3 additions & 3 deletions src/Twig/InlineEditBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit ad83704

Please sign in to comment.