zend-blog-3-backend
186 строк · 4.8 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 16.11.14
6* Time: 17:43
7*/
8
9namespace App\Controller;
10
11use App\API\DataConverter;
12use Doctrine\ORM\EntityManager;
13use Doctrine\ORM\EntityManagerInterface;
14use Knp\Component\Pager\Pagination\PaginationInterface;
15use Knp\Component\Pager\Pagination\SlidingPagination;
16use Knp\Component\Pager\Paginator;
17use Knp\Component\Pager\PaginatorInterface;
18use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19use Symfony\Component\Form\Extension\Core\Type\FormType;
20use Symfony\Component\Form\FormInterface;
21use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
22use Symfony\Component\Validator\ConstraintViolationInterface;
23use Symfony\Component\Validator\Validator\ValidatorInterface;
24
25class BaseController extends AbstractController
26{
27/**
28* @var EntityManagerInterface
29*/
30protected EntityManagerInterface $em;
31
32/**
33* @var PaginatorInterface
34*/
35protected PaginatorInterface $paginator;
36
37/**
38* @var DataConverter
39*/
40protected DataConverter $apiDataConverter;
41
42protected array $errorsPathMap = [];
43
44/**
45* @param EntityManagerInterface $em
46* @param PaginatorInterface $paginator
47* @param DataConverter $apiDataConverter
48*/
49public function __construct(
50EntityManagerInterface $em,
51PaginatorInterface $paginator,
52DataConverter $apiDataConverter
53) {
54$this->em = $em;
55$this->paginator = $paginator;
56$this->apiDataConverter = $apiDataConverter;
57}
58
59/**
60* @return EntityManager
61*/
62public function getEm(): EntityManagerInterface
63{
64return $this->em;
65}
66
67/**
68* @return DataConverter
69*/
70public function getDataConverter(): DataConverter
71{
72return $this->apiDataConverter;
73}
74
75/**
76* @return Paginator
77*/
78public function getPaginator(): PaginatorInterface
79{
80return $this->paginator;
81}
82
83/**
84* @param $query
85* @param $page
86* @param int $limit
87*
88* @return SlidingPagination
89*/
90public function paginate($query, $page, int $limit = 15): PaginationInterface
91{
92return $this->getPaginator()
93->paginate($query, (int)$page, $limit);
94}
95
96/**
97* @param array $data
98*
99* @return array
100*/
101public function getPaginationMetadata(array $data): array
102{
103return [
104'last' => $data['last'],
105'current' => $data['current'],
106'previous' => $data['previous'] ?? false,
107'next' => $data['next'] ?? false,
108];
109}
110
111/**
112* @param FormInterface $form
113*
114* @return array
115*/
116protected function handleForm(FormInterface $form): array
117{
118if ($form->isSubmitted()) {
119if ($form->isValid()) {
120$formData = $form->getData();
121} else {
122$errors = ['errors' => []];
123/* @var \Symfony\Component\Form\FormError $formError */
124foreach ($form->getErrors(true) as $formError) {
125$errors['errors'][] = [
126'message' => $formError->getMessage(),
127'path' => $this->fixErrorPath($formError->getCause()->getPropertyPath()),
128];
129}
130
131return [null, $errors];
132}
133} else {
134throw new BadRequestHttpException('Form not submitted');
135}
136
137return [$formData, null];
138}
139
140/**
141* @param $child
142* @param $type
143* @param bool $put
144*
145* @throws \Psr\Container\ContainerExceptionInterface
146* @throws \Psr\Container\NotFoundExceptionInterface
147*
148* @return FormInterface
149*/
150protected function createObjectForm($child, $type, bool $put = false): FormInterface
151{
152$fb = $this->container
153->get('form.factory')
154->createNamedBuilder('', FormType::class, null, [
155'csrf_protection' => false,
156'method' => $put ? 'PUT' : 'POST',
157]);
158
159$fb->add($child, $type);
160
161return $fb->getForm();
162}
163
164protected function validate(ValidatorInterface $validator, object $entity): array
165{
166$errors = [];
167$violations = $validator->validate($entity);
168if (count($violations) > 0) {
169$errors = ['errors' => []];
170/* @var ConstraintViolationInterface $violation */
171foreach ($violations as $violation) {
172$errors['errors'][] = [
173'message' => $violation->getMessage(),
174'path' => $this->fixErrorPath($violation->getPropertyPath()),
175];
176}
177}
178
179return $errors;
180}
181
182private function fixErrorPath(string $path): string
183{
184return $this->errorsPathMap[$path] ?? 'unknown';
185}
186}
187