45 lines
1.0 KiB
PHP
45 lines
1.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Action\Article;
|
||
|
|
|
||
|
|
use App\Action\AbstractFormAction;
|
||
|
|
use App\Entity\Article;
|
||
|
|
use App\Hydrator\DoctrineObject;
|
||
|
|
use Doctrine\ORM\EntityManager;
|
||
|
|
use Psr\Http\Message\ResponseInterface;
|
||
|
|
use Psr\Http\Message\ServerRequestInterface;
|
||
|
|
use Zend\Diactoros\Response\JsonResponse;
|
||
|
|
|
||
|
|
class PostAction extends AbstractFormAction
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var EntityManager
|
||
|
|
*/
|
||
|
|
private $em;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var DoctrineObject
|
||
|
|
*/
|
||
|
|
private $hydrator;
|
||
|
|
|
||
|
|
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||
|
|
{
|
||
|
|
$this->em = $em;
|
||
|
|
$this->hydrator = $hydrator;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||
|
|
{
|
||
|
|
$data = $this->getRequestData($request);
|
||
|
|
|
||
|
|
$entity = $this->hydrator->hydrate($data, new Article());
|
||
|
|
$this->em->persist($entity);
|
||
|
|
$this->em->flush();
|
||
|
|
|
||
|
|
return new JsonResponse([
|
||
|
|
'success' => true,
|
||
|
|
'result' => $entity,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|