From 4f5b44bd3bd490309eb2ba7b44df4769816ba729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 3 Aug 2013 19:26:54 +0200 Subject: twig implementation --- .../EventListener/FixCheckboxInputListener.php | 62 ++++++++ .../Core/EventListener/FixRadioInputListener.php | 66 ++++++++ .../Core/EventListener/FixUrlProtocolListener.php | 56 +++++++ .../Core/EventListener/MergeCollectionListener.php | 137 ++++++++++++++++ .../Core/EventListener/ResizeFormListener.php | 173 +++++++++++++++++++++ .../Extension/Core/EventListener/TrimListener.php | 55 +++++++ 6 files changed, 549 insertions(+) create mode 100644 vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php create mode 100644 vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php create mode 100644 vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php create mode 100644 vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php create mode 100644 vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php create mode 100644 vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener') diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php new file mode 100644 index 00000000..1f62e060 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; + +/** + * Takes care of converting the input from a list of checkboxes to a correctly + * indexed array. + * + * @author Bernhard Schussek + */ +class FixCheckboxInputListener implements EventSubscriberInterface +{ + private $choiceList; + + /** + * Constructor. + * + * @param ChoiceListInterface $choiceList + */ + public function __construct(ChoiceListInterface $choiceList) + { + $this->choiceList = $choiceList; + } + + public function preSubmit(FormEvent $event) + { + $values = (array) $event->getData(); + $indices = $this->choiceList->getIndicesForValues($values); + + $event->setData(count($indices) > 0 ? array_combine($indices, $values) : array()); + } + + /** + * Alias of {@link preSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link preSubmit()} instead. + */ + public function preBind(FormEvent $event) + { + $this->preSubmit($event); + } + + public static function getSubscribedEvents() + { + return array(FormEvents::PRE_SUBMIT => 'preSubmit'); + } +} diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php new file mode 100644 index 00000000..bf03792f --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; + +/** + * Takes care of converting the input from a single radio button + * to an array. + * + * @author Bernhard Schussek + */ +class FixRadioInputListener implements EventSubscriberInterface +{ + private $choiceList; + + private $placeholderPresent; + + /** + * Constructor. + * + * @param ChoiceListInterface $choiceList + * @param Boolean $placeholderPresent + */ + public function __construct(ChoiceListInterface $choiceList, $placeholderPresent) + { + $this->choiceList = $choiceList; + $this->placeholderPresent = $placeholderPresent; + } + + public function preSubmit(FormEvent $event) + { + $value = $event->getData(); + $index = current($this->choiceList->getIndicesForValues(array($value))); + + $event->setData(false !== $index ? array($index => $value) : ($this->placeholderPresent ? array('placeholder' => '') : array())) ; + } + + /** + * Alias of {@link preSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link preSubmit()} instead. + */ + public function preBind(FormEvent $event) + { + $this->preSubmit($event); + } + + public static function getSubscribedEvents() + { + return array(FormEvents::PRE_SUBMIT => 'preSubmit'); + } +} diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php new file mode 100644 index 00000000..e25dacf2 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Adds a protocol to a URL if it doesn't already have one. + * + * @author Bernhard Schussek + */ +class FixUrlProtocolListener implements EventSubscriberInterface +{ + private $defaultProtocol; + + public function __construct($defaultProtocol = 'http') + { + $this->defaultProtocol = $defaultProtocol; + } + + public function onSubmit(FormEvent $event) + { + $data = $event->getData(); + + if ($this->defaultProtocol && $data && !preg_match('~^\w+://~', $data)) { + $event->setData($this->defaultProtocol.'://'.$data); + } + } + + /** + * Alias of {@link onSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link onSubmit()} instead. + */ + public function onBind(FormEvent $event) + { + $this->onSubmit($event); + } + + public static function getSubscribedEvents() + { + return array(FormEvents::SUBMIT => 'onSubmit'); + } +} diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php new file mode 100644 index 00000000..4d0bdfaa --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php @@ -0,0 +1,137 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\Exception\UnexpectedTypeException; + +/** + * @author Bernhard Schussek + */ +class MergeCollectionListener implements EventSubscriberInterface +{ + /** + * Whether elements may be added to the collection + * @var Boolean + */ + private $allowAdd; + + /** + * Whether elements may be removed from the collection + * @var Boolean + */ + private $allowDelete; + + /** + * Creates a new listener. + * + * @param Boolean $allowAdd Whether values might be added to the + * collection. + * @param Boolean $allowDelete Whether values might be removed from the + * collection. + */ + public function __construct($allowAdd = false, $allowDelete = false) + { + $this->allowAdd = $allowAdd; + $this->allowDelete = $allowDelete; + } + + public static function getSubscribedEvents() + { + return array( + FormEvents::SUBMIT => 'onSubmit', + ); + } + + public function onSubmit(FormEvent $event) + { + $dataToMergeInto = $event->getForm()->getNormData(); + $data = $event->getData(); + + if (null === $data) { + $data = array(); + } + + if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) { + throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)'); + } + + if (null !== $dataToMergeInto && !is_array($dataToMergeInto) && !($dataToMergeInto instanceof \Traversable && $dataToMergeInto instanceof \ArrayAccess)) { + throw new UnexpectedTypeException($dataToMergeInto, 'array or (\Traversable and \ArrayAccess)'); + } + + // If we are not allowed to change anything, return immediately + if ((!$this->allowAdd && !$this->allowDelete) || $data === $dataToMergeInto) { + $event->setData($dataToMergeInto); + + return; + } + + if (!$dataToMergeInto) { + // No original data was set. Set it if allowed + if ($this->allowAdd) { + $dataToMergeInto = $data; + } + } else { + // Calculate delta + $itemsToAdd = is_object($data) ? clone $data : $data; + $itemsToDelete = array(); + + foreach ($dataToMergeInto as $beforeKey => $beforeItem) { + foreach ($data as $afterKey => $afterItem) { + if ($afterItem === $beforeItem) { + // Item found, next original item + unset($itemsToAdd[$afterKey]); + continue 2; + } + } + + // Item not found, remember for deletion + $itemsToDelete[] = $beforeKey; + } + + // Remove deleted items before adding to free keys that are to be + // replaced + if ($this->allowDelete) { + foreach ($itemsToDelete as $key) { + unset($dataToMergeInto[$key]); + } + } + + // Add remaining items + if ($this->allowAdd) { + foreach ($itemsToAdd as $key => $item) { + if (!isset($dataToMergeInto[$key])) { + $dataToMergeInto[$key] = $item; + } else { + $dataToMergeInto[] = $item; + } + } + } + } + + $event->setData($dataToMergeInto); + } + + /** + * Alias of {@link onSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link onSubmit()} instead. + */ + public function onBind(FormEvent $event) + { + $this->onSubmit($event); + } +} diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php new file mode 100644 index 00000000..f1c39db2 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php @@ -0,0 +1,173 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Resize a collection form element based on the data sent from the client. + * + * @author Bernhard Schussek + */ +class ResizeFormListener implements EventSubscriberInterface +{ + /** + * @var string + */ + protected $type; + + /** + * @var array + */ + protected $options; + + /** + * Whether children could be added to the group + * @var Boolean + */ + protected $allowAdd; + + /** + * Whether children could be removed from the group + * @var Boolean + */ + protected $allowDelete; + + public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false) + { + $this->type = $type; + $this->allowAdd = $allowAdd; + $this->allowDelete = $allowDelete; + $this->options = $options; + } + + public static function getSubscribedEvents() + { + return array( + FormEvents::PRE_SET_DATA => 'preSetData', + FormEvents::PRE_SUBMIT => 'preSubmit', + // (MergeCollectionListener, MergeDoctrineCollectionListener) + FormEvents::SUBMIT => array('onSubmit', 50), + ); + } + + public function preSetData(FormEvent $event) + { + $form = $event->getForm(); + $data = $event->getData(); + + if (null === $data) { + $data = array(); + } + + if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) { + throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)'); + } + + // First remove all rows + foreach ($form as $name => $child) { + $form->remove($name); + } + + // Then add all rows again in the correct order + foreach ($data as $name => $value) { + $form->add($name, $this->type, array_replace(array( + 'property_path' => '['.$name.']', + ), $this->options)); + } + } + + public function preSubmit(FormEvent $event) + { + $form = $event->getForm(); + $data = $event->getData(); + + if (null === $data || '' === $data) { + $data = array(); + } + + if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) { + throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)'); + } + + // Remove all empty rows + if ($this->allowDelete) { + foreach ($form as $name => $child) { + if (!isset($data[$name])) { + $form->remove($name); + } + } + } + + // Add all additional rows + if ($this->allowAdd) { + foreach ($data as $name => $value) { + if (!$form->has($name)) { + $form->add($name, $this->type, array_replace(array( + 'property_path' => '['.$name.']', + ), $this->options)); + } + } + } + } + + public function onSubmit(FormEvent $event) + { + $form = $event->getForm(); + $data = $event->getData(); + + if (null === $data) { + $data = array(); + } + + if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) { + throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)'); + } + + // The data mapper only adds, but does not remove items, so do this + // here + if ($this->allowDelete) { + foreach ($data as $name => $child) { + if (!$form->has($name)) { + unset($data[$name]); + } + } + } + + $event->setData($data); + } + + /** + * Alias of {@link preSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link preSubmit()} instead. + */ + public function preBind(FormEvent $event) + { + $this->preSubmit($event); + } + + /** + * Alias of {@link onSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link onSubmit()} instead. + */ + public function onBind(FormEvent $event) + { + $this->onSubmit($event); + } +} diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php new file mode 100644 index 00000000..cbe6e0ab --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Trims string data + * + * @author Bernhard Schussek + */ +class TrimListener implements EventSubscriberInterface +{ + public function preSubmit(FormEvent $event) + { + $data = $event->getData(); + + if (!is_string($data)) { + return; + } + + if (null !== $result = @preg_replace('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u', '', $data)) { + $event->setData($result); + } else { + $event->setData(trim($data)); + } + } + + /** + * Alias of {@link preSubmit()}. + * + * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use + * {@link preSubmit()} instead. + */ + public function preBind(FormEvent $event) + { + $this->preSubmit($event); + } + + public static function getSubscribedEvents() + { + return array(FormEvents::PRE_SUBMIT => 'preSubmit'); + } +} -- cgit v1.2.3