1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ApiBundle\Form\EventSubscriber; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\User\Model\UserAwareInterface; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Symfony\Component\Form\FormEvent; |
17
|
|
|
use Symfony\Component\Form\FormEvents; |
18
|
|
|
use Symfony\Component\Validator\Constraints\Valid; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Łukasz Chruściel <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class AddUserFormSubscriber implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $entryType; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $entryType |
33
|
|
|
*/ |
34
|
|
|
public function __construct($entryType) |
35
|
|
|
{ |
36
|
|
|
$this->entryType = $entryType; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
FormEvents::PRE_SET_DATA => 'preSetData', |
46
|
|
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param FormEvent $event |
52
|
|
|
*/ |
53
|
|
|
public function preSetData(FormEvent $event) |
54
|
|
|
{ |
55
|
|
|
$form = $event->getForm(); |
56
|
|
|
$form->add('user', $this->entryType, ['constraints' => [new Valid()]]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param FormEvent $event |
61
|
|
|
*/ |
62
|
|
|
public function preSubmit(FormEvent $event) |
63
|
|
|
{ |
64
|
|
|
$data = $event->getData(); |
65
|
|
|
$normData = $event->getForm()->getNormData(); |
66
|
|
|
if (!isset($data['user'])) { |
67
|
|
|
$this->removeUserField($event); |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
Assert::isInstanceOf($normData, UserAwareInterface::class); |
71
|
|
|
if ($this->isUserDataEmpty($data) && null === $normData->getUser()) { |
72
|
|
|
unset($data['user']); |
73
|
|
|
$event->setData($data); |
74
|
|
|
$this->removeUserField($event); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $data |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
private function isUserDataEmpty(array $data) |
84
|
|
|
{ |
85
|
|
|
foreach ($data['user'] as $field) { |
86
|
|
|
if (!empty($field)) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param FormEvent $event |
95
|
|
|
*/ |
96
|
|
|
private function removeUserField(FormEvent $event) |
97
|
|
|
{ |
98
|
|
|
$form = $event->getForm(); |
99
|
|
|
$form->remove('user'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|