Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\Entity;

use App\Enum\BookCategory;
use App\Grid\BookGrid;
use App\Repository\BookRepository;
use App\Responder\ExportGridToCsvResponder;
Expand Down Expand Up @@ -79,6 +80,9 @@ class Book implements ResourceInterface
#[NotBlank]
private ?string $authorName = null;

#[ORM\Column(type: 'enum', length: 255, nullable: true)]
private ?BookCategory $category = null;

#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $createdAt;

Expand Down Expand Up @@ -121,4 +125,14 @@ public function setCreatedAt(\DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}

public function getCategory(): ?BookCategory
{
return $this->category;
}

public function setCategory(?BookCategory $category): void
{
$this->category = $category;
}
}
51 changes: 51 additions & 0 deletions app/Enum/BookCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Enum;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum BookCategory: string implements TranslatableInterface
{
case FICTION = 'fiction';
case NON_FICTION = 'non-fiction';
case MYSTERY = 'mystery';
case THRILLER = 'thriller';
case SCIENCE_FICTION = 'science-fiction';
case FANTASY = 'fantasy';
case ROMANCE = 'romance';
case HORROR = 'horror';
case BIOGRAPHY = 'biography';
case HISTORY = 'history';
case SCIENCE = 'science';
case TECHNOLOGY = 'technology';
case BUSINESS = 'business';
case COOKING = 'cooking';
case TRAVEL = 'travel';
case POETRY = 'poetry';
case DRAMA = 'drama';
case CHILDREN = 'children';
case YOUNG_ADULT = 'young adult';
case RELIGION = 'religion';
case PHILOSOPHY = 'philosophy';
case ART = 'art';
case MANGA = 'manga';
case COMICS = 'comics';

/** added for Symfony 6 compatibility */
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return ucfirst($translator->trans($this->value, locale: $locale));
}
}
7 changes: 7 additions & 0 deletions app/Factory/BookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Factory;

use App\Entity\Book;
use App\Enum\BookCategory;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;

/**
Expand All @@ -36,11 +37,17 @@ public function withAuthorName(string $authorName): self
return $this->with(['authorName' => $authorName]);
}

public function withCategory(BookCategory $category): self
{
return $this->with(['category' => $category]);
}

protected function defaults(): array|callable
{
return [
'title' => ucfirst(self::faker()->words(3, true)),
'authorName' => self::faker()->firstName() . ' ' . self::faker()->lastName(),
'category' => self::faker()->randomElement(BookCategory::cases()),
];
}
}
35 changes: 21 additions & 14 deletions app/Grid/BookGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Grid;

use App\Entity\Book;
use App\Enum\BookCategory;
use Sylius\Bundle\GridBundle\Builder\Action\Action;
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
Expand All @@ -22,36 +23,47 @@
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\EnumField;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Filter\EnumFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
use Sylius\Component\Grid\Attribute\AsGrid;
use Symfony\Contracts\Translation\TranslatorInterface;

final class BookGrid extends AbstractGrid implements ResourceAwareGridInterface
#[AsGrid(
resourceClass: Book::class,
name: 'app_book',
)]
final class BookGrid extends AbstractGrid
{
public static function getName(): string
public function __construct(private readonly TranslatorInterface $translator)
{
return 'app_book';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
public function __invoke(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->orderBy('title')
->addFilter(
->withFilters(
StringFilter::create('search', ['title', 'authorName'])
->setLabel('sylius.ui.search'),
EnumFilter::create(name: 'category', enumClass: BookCategory::class, field: 'category')
->addFormOption('choice_value', fn (?BookCategory $enum) => $enum?->value)
->addFormOption('choice_label', fn (BookCategory $choice) => $choice->trans($this->translator))
->setLabel('app.ui.category'),
)
->addField(
->withFields(
StringField::create('title')
->setLabel('app.ui.title')
->setSortable(true),
)
->addField(
StringField::create('authorName')
->setLabel('app.ui.author_name')
->setSortable(true),
EnumField::create('category')
->setLabel('app.ui.category')
->setSortable(true),
)
->addActionGroup(
MainActionGroup::create(
Expand All @@ -78,9 +90,4 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
)
;
}

public function getResourceClass(): string
{
return Book::class;
}
}
Loading
Loading