Last active
May 17, 2019 13:14
-
-
Save yceruto/cb13379d9862f581bdded78e4adac228 to your computer and use it in GitHub Desktop.
DTO simplification for EasyAdminBundle applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController; | |
use Symfony\Component\Form\FormInterface; | |
/** | |
* See more https://github.com/yceruto/easyadmin-demo/tree/dto_example | |
*/ | |
abstract class EasyAdminDTOController extends EasyAdminController | |
{ | |
protected function createNewEntity() | |
{ | |
return $this->createDTO(); | |
} | |
protected function persistEntity($dto): void | |
{ | |
$entity = $this->createEntity($dto); | |
parent::persistEntity($entity); | |
} | |
protected function createEditForm($entity, array $entityProperties) | |
{ | |
$dto = $this->createDTO($entity); | |
return $this->createEntityForm($dto, $entityProperties, 'edit'); | |
} | |
protected function updateEntity($entity, FormInterface $editForm = null): void | |
{ | |
if (null !== $editForm) { | |
$dto = $editForm->getData(); | |
$this->mutateEntity($entity, $dto); | |
} | |
parent::updateEntity($entity); | |
} | |
/** | |
* Creates a new Data Transfer Object. | |
* | |
* This method is called on new and edit actions. | |
* | |
* @param object|null $entity The entity instance if editing, null otherwise | |
* | |
* @return object The new Data Transfer Object | |
*/ | |
abstract protected function createDTO($entity = null); | |
/** | |
* Creates a new Entity from a Data Transfer Object. | |
* | |
* This method is called on new action only. | |
* | |
* @param object $dto The DTO instance | |
* | |
* @return object The new entity object | |
*/ | |
abstract protected function createEntity($dto); | |
/** | |
* Updates an Entity from a Data Transfer Object. | |
* | |
* This method is called on edit action only. | |
* | |
* @param object $entity The entity instance | |
* @param object $dto The DTO instance | |
* | |
* @return object The mutated entity object | |
*/ | |
abstract protected function mutateEntity($entity, $dto); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment