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