1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
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 WBW\Bundle\CoreBundle\Helper; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use WBW\Bundle\CoreBundle\Event\NotificationEvent; |
19
|
|
|
use WBW\Bundle\CoreBundle\Event\NotificationEvents; |
20
|
|
|
use WBW\Bundle\CoreBundle\Exception\RedirectResponseException; |
21
|
|
|
use WBW\Bundle\CoreBundle\Notification\NotificationFactory; |
22
|
|
|
use WBW\Bundle\CoreBundle\Service\EventDispatcherTrait; |
23
|
|
|
use WBW\Bundle\CoreBundle\Service\ObjectManagerTrait; |
24
|
|
|
use WBW\Library\Core\Exception\Argument\IllegalArgumentException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Form helper. |
28
|
|
|
* |
29
|
|
|
* @author webeweb <https://github.com/webeweb/> |
30
|
|
|
* @package WBW\Bundle\CoreBundle\Helper |
31
|
|
|
*/ |
32
|
|
|
class FormHelper { |
33
|
|
|
|
34
|
|
|
use EventDispatcherTrait; |
35
|
|
|
use ObjectManagerTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Service name. |
39
|
|
|
* |
40
|
|
|
* @avr string |
41
|
|
|
*/ |
42
|
|
|
const SERVICE_NAME = "webeweb.core.helper.form"; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param ObjectManager $objectManager The object manager. |
48
|
|
|
* @param EventDispatcherInterface $eventDispatcher The event dispatcher. |
49
|
|
|
*/ |
50
|
|
|
public function __construct(ObjectManager $objectManager, EventDispatcherInterface $eventDispatcher) { |
51
|
|
|
$this->setEventDispatcher($eventDispatcher); |
52
|
|
|
$this->setObjectManager($objectManager); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check an entity type. |
57
|
|
|
* |
58
|
|
|
* @param $collection The collection. |
59
|
|
|
* @param string $notification The notification. |
60
|
|
|
* @param string $redirectURL The redirect URL. |
61
|
|
|
* @param int $expected The expected count. |
62
|
|
|
* @throws IllegalArgumentException Throws an illegal argument exception if collection is null. |
63
|
|
|
* @throws RedirectResponseException Throws a redirect response exception if the collection is less than $expected. |
64
|
|
|
*/ |
65
|
|
|
public function checkCollection($collection, $notification, $redirectURL, $expected = 1) { |
66
|
|
|
|
67
|
|
|
// Check the collection. |
68
|
|
|
if (null === $collection) { |
69
|
|
|
throw new IllegalArgumentException("The collection must be a countable"); |
70
|
|
|
} |
71
|
|
|
if ($expected <= count($collection)) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Initialize the event name. |
76
|
|
|
$eventName = NotificationEvents::NOTIFICATION_WARNING; |
77
|
|
|
|
78
|
|
|
// Check the event dispatcher. |
79
|
|
|
if (null !== $this->getEventDispatcher() && true === $this->getEventDispatcher()->hasListeners($eventName)) { |
80
|
|
|
|
81
|
|
|
// Initialize the event. |
82
|
|
|
$notification = NotificationFactory::newWarningNotification($notification); |
83
|
|
|
$event = new NotificationEvent($eventName, $notification); |
84
|
|
|
|
85
|
|
|
// Dispatch the event. |
86
|
|
|
$this->getEventDispatcher()->dispatch($eventName, $event); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Throws a redirect response exception. |
90
|
|
|
throw new RedirectResponseException($redirectURL, null); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* On post handle request with collection. |
95
|
|
|
* |
96
|
|
|
* @param Collection $oldCollection The old collection. |
97
|
|
|
* @param Collection $newCollection The new collection. |
98
|
|
|
* @return int Returns the deleted count. |
99
|
|
|
*/ |
100
|
|
|
public function onPostHandleRequestWithCollection(Collection $oldCollection, Collection $newCollection) { |
101
|
|
|
$deleted = 0; |
102
|
|
|
foreach ($oldCollection as $current) { |
103
|
|
|
if (true === $newCollection->contains($current)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$this->getObjectManager()->remove($current); |
107
|
|
|
++$deleted; |
108
|
|
|
} |
109
|
|
|
return $deleted; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* On pre handle request with collection. |
114
|
|
|
* |
115
|
|
|
* @param Collection $collection The collection. |
116
|
|
|
* @return Collection Returns the cloned collection. |
117
|
|
|
*/ |
118
|
|
|
public function onPreHandleRequestWithCollection(Collection $collection) { |
119
|
|
|
$output = new ArrayCollection(); |
120
|
|
|
foreach ($collection as $current) { |
121
|
|
|
$output->add($current); |
122
|
|
|
} |
123
|
|
|
return $output; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|