* element factory edded for doctrine forms
* some fields changed to optional
This commit is contained in:
parent
117c10a56a
commit
71d6577810
@ -38,6 +38,7 @@ class ConfigProvider
|
||||
'factories' => [
|
||||
Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class,
|
||||
Handler\TeamHandler::class => Handler\TeamHandlerFactory::class,
|
||||
Handler\SlideHandler::class => Handler\SlideHandlerFactory::class,
|
||||
|
||||
Service\TeamService::class => Service\TeamServiceFactory::class,
|
||||
Service\SlideManager::class => Service\SlideManagerFactory::class,
|
||||
|
||||
@ -36,7 +36,7 @@ class Slide implements JsonSerializable
|
||||
private $team;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="slide_data", type="text")
|
||||
* @ORM\Column(name="slide_data", type="text", nullable=false)
|
||||
* @var string
|
||||
*/
|
||||
private $slideData;
|
||||
@ -73,7 +73,7 @@ class Slide implements JsonSerializable
|
||||
* @param int $id
|
||||
* @return Slide
|
||||
*/
|
||||
public function setId(int $id): Slide
|
||||
public function setId(?int $id): Slide
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
@ -109,7 +109,7 @@ class Slide implements JsonSerializable
|
||||
* @param Team $team
|
||||
* @return Slide
|
||||
*/
|
||||
public function setTeam(Team $team): Slide
|
||||
public function setTeam(?Team $team): Slide
|
||||
{
|
||||
$this->team = $team;
|
||||
return $this;
|
||||
|
||||
@ -85,7 +85,7 @@ class Team implements JsonSerializable
|
||||
* @param int $id
|
||||
* @return Team
|
||||
*/
|
||||
public function setId(int $id): Team
|
||||
public function setId(?int $id): Team
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
|
||||
@ -31,13 +31,14 @@ class Slide
|
||||
private $title;
|
||||
|
||||
/**
|
||||
* @Annotation\Type("Zend\Form\Element\Text")
|
||||
* @Annotation\Required(true)
|
||||
* @Annotation\Type("doctrine.object_select")
|
||||
* @Annotation\Required(false)
|
||||
* @Annotation\Options({
|
||||
* "property": "name",
|
||||
* "label": "Team",
|
||||
* "target_class": "App\Entity\Team",
|
||||
* "display_empty_item": false,
|
||||
* "empty_item_label": "",
|
||||
* "display_empty_item": true,
|
||||
* "empty_item_label": "---",
|
||||
* "is_method": true,
|
||||
* "find_method": {
|
||||
* "name": "findBy",
|
||||
@ -65,8 +66,13 @@ class Slide
|
||||
/**
|
||||
* @Annotation\Type("Zend\Form\Element\Checkbox")
|
||||
* @Annotation\Options({
|
||||
* "label": "Visible"
|
||||
* "label": "Visible",
|
||||
* })
|
||||
* @Annotation\Validator({
|
||||
* "name":"NotEmpty",
|
||||
* "options": {"type": 126 }
|
||||
* })
|
||||
* @Annotation\Required(false)
|
||||
* @var bool
|
||||
*/
|
||||
private $isVisible;
|
||||
|
||||
@ -42,10 +42,17 @@ class Team
|
||||
private $members;
|
||||
|
||||
/**
|
||||
* @Annotation\Type("Zend\Form\Element\Checkbox")
|
||||
* Also a dummy field, a
|
||||
* @Annotation\Type("Zend\Form\Element\Text")
|
||||
* @Annotation\Options({
|
||||
* "label": "Active"
|
||||
* })
|
||||
* @Annotation\Validator({
|
||||
* "name":"NotEmpty",
|
||||
* "options": {"type": Zend\Validator\NotEmpty::NULL}
|
||||
* })
|
||||
* @Annotation\Required(false)
|
||||
|
||||
* @var bool
|
||||
*/
|
||||
private $isActive;
|
||||
|
||||
@ -15,7 +15,8 @@ class SlideHandler extends AbstractCrudHandler
|
||||
/** @var SlideManager */
|
||||
private $slideManager;
|
||||
|
||||
public function __construct(SlideManager $teamService) {
|
||||
public function __construct(SlideManager $teamService)
|
||||
{
|
||||
$this->slideManager = $teamService;
|
||||
}
|
||||
|
||||
@ -60,6 +61,29 @@ class SlideHandler extends AbstractCrudHandler
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function update(ServerRequestInterface $request)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
$data = $this->getRequestData($request);
|
||||
try {
|
||||
return new JsonResponse($this->slideManager->changeSlide((int)$id, $data));
|
||||
} catch (UniqueConstraintViolationException $e) {
|
||||
return new JsonResponse([
|
||||
'message' => 'The field `name` must be unique',
|
||||
], 500);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return new JsonResponse([
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
@ -68,6 +92,6 @@ class SlideHandler extends AbstractCrudHandler
|
||||
public function delete(ServerRequestInterface $request)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
return new JsonResponse($this->slideManager->removeSlide($id));
|
||||
return new JsonResponse($this->slideManager->removeSlide((int)$id));
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\Slide;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityNotFoundException;
|
||||
use Zend\Form\Form;
|
||||
|
||||
class SlideManager
|
||||
@ -29,9 +30,7 @@ class SlideManager
|
||||
*/
|
||||
public function listSlides(): array
|
||||
{
|
||||
return $this->em->getRepository(self::ENTITY_NAME)->findBy([
|
||||
'isActive' => true,
|
||||
]);
|
||||
return $this->em->getRepository(self::ENTITY_NAME)->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -76,14 +75,14 @@ class SlideManager
|
||||
/**
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @return Slide
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function changeSlide(int $id, array $data): bool
|
||||
public function changeSlide(int $id, array $data): Slide
|
||||
{
|
||||
if (null === ($entity = $this->getSlide($id))) {
|
||||
return false;
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
$this->form
|
||||
->bind($entity)
|
||||
@ -91,9 +90,15 @@ class SlideManager
|
||||
|
||||
if ($this->form->isValid()) {
|
||||
$this->em->flush();
|
||||
return true;
|
||||
return $entity;
|
||||
}
|
||||
return false;
|
||||
|
||||
$messages = $this->form->getMessages();
|
||||
$fields = array_keys($messages);
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
"The following fields are invalid: (%s)",
|
||||
implode(", ", $fields)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -105,6 +110,7 @@ class SlideManager
|
||||
{
|
||||
if (null !== ($entity = $this->getSlide($id))) {
|
||||
$this->em->remove($entity);
|
||||
$this->em->flush();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Form\Team;
|
||||
use App\Form\Slide;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Zend\Form\Annotation\AnnotationBuilder;
|
||||
@ -17,7 +17,7 @@ class SlideManagerFactory
|
||||
$em = $container->get(EntityManager::class);
|
||||
$formBuilder = $container->get(AnnotationBuilder::class);
|
||||
/** @var Form $form */
|
||||
$form = $formBuilder->createForm(Team::class);
|
||||
$form = $formBuilder->createForm(Slide::class);
|
||||
return new SlideManager($em, $form);
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,7 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\Team;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityNotFoundException;
|
||||
use Zend\Form\Form;
|
||||
|
||||
class TeamService
|
||||
@ -29,9 +30,7 @@ class TeamService
|
||||
*/
|
||||
public function listTeams(): array
|
||||
{
|
||||
return $this->em->getRepository(self::ENTITY_NAME)->findBy([
|
||||
'isActive' => true,
|
||||
]);
|
||||
return $this->em->getRepository(self::ENTITY_NAME)->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -76,14 +75,14 @@ class TeamService
|
||||
/**
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @return Team
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function changeTeam(int $id, array $data): bool
|
||||
public function changeTeam(int $id, array $data): Team
|
||||
{
|
||||
if (null === ($entity = $this->getTeam($id))) {
|
||||
return false;
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
$this->form
|
||||
->bind($entity)
|
||||
@ -91,7 +90,7 @@ class TeamService
|
||||
|
||||
if ($this->form->isValid()) {
|
||||
$this->em->flush();
|
||||
return true;
|
||||
return $entity;
|
||||
}
|
||||
|
||||
$messages = $this->form->getMessages();
|
||||
|
||||
@ -24,6 +24,7 @@ class ConfigProvider
|
||||
{
|
||||
return [
|
||||
'dependencies' => $this->getDependencies(),
|
||||
'form_elements' => $this->getFormElements(),
|
||||
];
|
||||
}
|
||||
|
||||
@ -45,4 +46,19 @@ class ConfigProvider
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the form dependencies
|
||||
*/
|
||||
public function getFormElements() : array
|
||||
{
|
||||
return [
|
||||
'aliases' => [
|
||||
'doctrine.object_select' => Form\Element\ObjectSelect::class,
|
||||
],
|
||||
'factories' => [
|
||||
Form\Element\ObjectSelect::class => Form\Element\ElementFactory::class,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
18
src/DoctrineExpressiveModule/Form/Element/ElementFactory.php
Normal file
18
src/DoctrineExpressiveModule/Form/Element/ElementFactory.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace DoctrineExpressiveModule\Form\Element;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ElementFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container, string $elementClass)
|
||||
{
|
||||
$em = $container->get('doctrine.entity_manager.orm_default');
|
||||
/** @var ObjectSelect|ObjectRadio|ObjectMultiCheckbox $element */
|
||||
$element = new $elementClass();
|
||||
$element->setOption('object_manager', $em);
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user