Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52d1848

Browse files
michaelKaeferjaviereguiluz
authored andcommitted
Refactor/dto collections
1 parent 2916dbf commit 52d1848

20 files changed

Lines changed: 73 additions & 75 deletions

src/Collection/ActionCollection.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Collection;
44

5-
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Collection\CollectionInterface;
65
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
76
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto;
87

98
/**
109
* @author Javier Eguiluz <[email protected]>
11-
*
12-
* @implements CollectionInterface<string, ActionDto|ActionGroupDto>
1310
*/
14-
final class ActionCollection implements CollectionInterface
11+
final class ActionCollection implements \ArrayAccess, \Countable, \IteratorAggregate
1512
{
1613
/**
1714
* @param array<string, ActionDto|ActionGroupDto> $actions
1815
*/
19-
private function __construct(private array $actions)
16+
public function __construct(private array $actions = [])
2017
{
2118
}
2219

@@ -28,6 +25,8 @@ public function __clone()
2825
}
2926

3027
/**
28+
* @deprecated since 4.28.2 and removed in 5.0.0, use FilterCollection::__construct() instead.
29+
*
3130
* @param array<string, ActionDto|ActionGroupDto> $actions
3231
*/
3332
public static function new(array $actions): self
@@ -94,15 +93,15 @@ public function getEntityActions(): self
9493

9594
public function getGlobalActions(): self
9695
{
97-
return self::new(array_filter(
96+
return new self(array_filter(
9897
$this->actions,
9998
static fn (ActionDto|ActionGroupDto $action): bool => $action->isGlobalAction()
10099
));
101100
}
102101

103102
public function getBatchActions(): self
104103
{
105-
return self::new(array_filter(
104+
return new self(array_filter(
106105
$this->actions,
107106
static fn (ActionDto|ActionGroupDto $action): bool => $action instanceof ActionDto && $action->isBatchAction()
108107
));

src/Collection/EntityCollection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Collection;
44

5-
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Collection\CollectionInterface;
65
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
76

87
/**
98
* @author Javier Eguiluz <[email protected]>
10-
*
11-
* @implements CollectionInterface<string, EntityDto>
129
*/
13-
final class EntityCollection implements CollectionInterface
10+
final class EntityCollection implements \ArrayAccess, \Countable, \IteratorAggregate
1411
{
1512
/**
1613
* @param array<string, EntityDto> $entities
1714
*/
18-
private function __construct(private array $entities)
15+
public function __construct(private array $entities = [])
1916
{
2017
}
2118

2219
/**
20+
* @deprecated since 4.28.2 and removed in 5.0.0, use FilterCollection::__construct() instead.
21+
*
2322
* @param array<string, EntityDto> $entities
2423
*/
2524
public static function new(array $entities): self

src/Collection/FieldCollection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Collection;
44

5-
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Collection\CollectionInterface;
65
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
76
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
87
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
98

109
/**
1110
* @author Javier Eguiluz <[email protected]>
12-
*
13-
* @implements CollectionInterface<string, FieldDto>
1411
*/
15-
final class FieldCollection implements CollectionInterface
12+
final class FieldCollection implements \ArrayAccess, \Countable, \IteratorAggregate
1613
{
1714
/** @var array<string, FieldDto> */
1815
private array $fields;
1916

2017
/**
2118
* @param array<FieldInterface|string> $fields
2219
*/
23-
private function __construct(iterable $fields)
20+
public function __construct(iterable $fields)
2421
{
2522
$this->fields = $this->processFields($fields);
2623
}
@@ -37,6 +34,8 @@ public function __clone()
3734
}
3835

3936
/**
37+
* @deprecated since 4.28.2 and removed in 5.0.0, use FilterCollection::__construct() instead.
38+
*
4039
* @param array<FieldInterface|string> $fields
4140
*/
4241
public static function new(iterable $fields): self

src/Collection/FilterCollection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Collection;
44

5-
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Collection\CollectionInterface;
65
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto;
76

87
/**
98
* @author Javier Eguiluz <[email protected]>
10-
*
11-
* @implements CollectionInterface<string, FilterDto>
129
*/
13-
final class FilterCollection implements CollectionInterface
10+
final class FilterCollection implements \ArrayAccess, \Countable, \IteratorAggregate
1411
{
1512
/**
1613
* @param array<string, FilterDto> $filters
1714
*/
18-
private function __construct(private array $filters)
15+
public function __construct(private array $filters = [])
1916
{
2017
}
2118

2219
/**
20+
* @deprecated since 4.28.2 and removed in 5.0.0, use FilterCollection::__construct() instead.
21+
*
2322
* @param array<string, FilterDto> $filters
2423
*/
2524
public static function new(array $filters = []): self

src/Contracts/Collection/CollectionInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Contracts\Collection;
44

55
/**
6+
* @deprecated since 4.28.2 and removed in 5.0.0 without a replacement.
7+
*
68
* @author Javier Eguiluz <[email protected]>
79
*
810
* @template TKey

src/Controller/AbstractCrudController.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function index(AdminContext $context)
132132
throw new ForbiddenActionException($context);
133133
}
134134

135-
$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
135+
$fields = new FieldCollection($this->configureFields(Crud::PAGE_INDEX));
136136
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $fields, $context->getEntity());
137137
$queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields, $filters);
138138
$paginator = $this->container->get(PaginatorFactory::class)->create($queryBuilder);
@@ -147,7 +147,7 @@ public function index(AdminContext $context)
147147

148148
$entities = $this->container->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
149149
$this->container->get(FieldFactory::class)->processFieldsForAll($entities, $fields, Crud::PAGE_INDEX);
150-
$processedFields = $entities->first()?->getFields() ?? FieldCollection::new([]);
150+
$processedFields = $entities->first()?->getFields() ?? new FieldCollection([]);
151151
$context->getCrud()->setFieldAssets($this->getFieldAssets($processedFields));
152152
$actions = $this->container->get(ActionFactory::class)->processGlobalActionsAndEntityActionsForAll($entities, $context->getCrud()->getActionsConfig());
153153

@@ -186,7 +186,7 @@ public function detail(AdminContext $context)
186186
throw new InsufficientEntityPermissionException($context);
187187
}
188188

189-
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)), Crud::PAGE_DETAIL);
189+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), new FieldCollection($this->configureFields(Crud::PAGE_DETAIL)), Crud::PAGE_DETAIL);
190190
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
191191
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
192192

@@ -221,7 +221,7 @@ public function edit(AdminContext $context)
221221
throw new InsufficientEntityPermissionException($context);
222222
}
223223

224-
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)), Crud::PAGE_EDIT);
224+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), new FieldCollection($this->configureFields(Crud::PAGE_EDIT)), Crud::PAGE_EDIT);
225225
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
226226
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
227227
/** @var TEntity $entityInstance */
@@ -307,7 +307,7 @@ public function new(AdminContext $context)
307307
/** @var class-string<TEntity> $entityFqcn */
308308
$entityFqcn = $context->getEntity()->getFqcn();
309309
$context->getEntity()->setInstance($this->createEntity($entityFqcn));
310-
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)), Crud::PAGE_NEW);
310+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), new FieldCollection($this->configureFields(Crud::PAGE_NEW)), Crud::PAGE_NEW);
311311
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
312312
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
313313

@@ -469,7 +469,7 @@ public function batchDelete(AdminContext $context, BatchActionDto $batchActionDt
469469

470470
public function autocomplete(AdminContext $context): JsonResponse
471471
{
472-
$queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), FieldCollection::new([]), FilterCollection::new());
472+
$queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), new FieldCollection([]), new FilterCollection());
473473

474474
$autocompleteContext = $context->getRequest()->query->all(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
475475

@@ -482,7 +482,7 @@ public function autocomplete(AdminContext $context): JsonResponse
482482
$controller = $this->container->get(ControllerFactory::class)->getCrudControllerInstance($crudControllerFqcn, Action::INDEX, $context->getRequest());
483483
$originatingPage = $autocompleteContext['originatingPage'];
484484
$propertyName = $autocompleteContext['propertyName'];
485-
$fields = FieldCollection::new($controller->configureFields($originatingPage));
485+
$fields = new FieldCollection($controller->configureFields($originatingPage));
486486

487487
// find the first field with matching property that is displayed on the originating page;
488488
// this ensures we get the correct field when a controller defines more than one field for the
@@ -526,7 +526,7 @@ public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityD
526526

527527
public function renderFilters(AdminContext $context): KeyValueStore
528528
{
529-
$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
529+
$fields = new FieldCollection($this->configureFields(Crud::PAGE_INDEX));
530530
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), $fields, Crud::PAGE_INDEX);
531531
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity());
532532

src/Dto/ActionConfigDto.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function disableActions(array $actionNames): void
113113
*/
114114
public function getActions(): ActionCollection|array
115115
{
116-
return null === $this->pageName ? $this->actions : ActionCollection::new($this->actions[$this->pageName]);
116+
return null === $this->pageName ? $this->actions : new ActionCollection($this->actions[$this->pageName]);
117117
}
118118

119119
/**

src/Factory/ActionFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function processEntityActions(EntityDto $entityDto, ActionConfigDto $acti
9999
$processedItems = $this->sortActionsByPriority($processedItems);
100100
}
101101

102-
$entityDto->setActions(ActionCollection::new($processedItems));
102+
$entityDto->setActions(new ActionCollection($processedItems));
103103
$entityDto->setDefaultActionUrl($this->resolveDefaultActionUrl($processedItems));
104104
}
105105

@@ -223,7 +223,7 @@ public function processGlobalActions(?ActionConfigDto $actionsDto = null): Actio
223223
$processedItems = $this->sortActionsByPriority($processedItems);
224224
}
225225

226-
return ActionCollection::new($processedItems);
226+
return new ActionCollection($processedItems);
227227
}
228228

229229
public function processGlobalActionsAndEntityActionsForAll(EntityCollection $entityDtos, ActionConfigDto $actionConfigDto): ActionCollection

src/Factory/EntityFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function createCollection(EntityDto $entityDto, ?iterable $entityInstance
120120
$entityDtos[$newEntityId] = $newEntityDto;
121121
}
122122

123-
return EntityCollection::new($entityDtos);
123+
return new EntityCollection($entityDtos);
124124
}
125125

126126
/**

src/Factory/FilterFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function create(FilterConfigDto $filterConfig, FieldCollection $fields, E
9191
$builtFilters[$property] = $filterDto;
9292
}
9393

94-
return FilterCollection::new($builtFilters);
94+
return new FilterCollection($builtFilters);
9595
}
9696

9797
private function guessFilterClass(EntityDto $entityDto, string $propertyName): string

0 commit comments

Comments
 (0)