1<?php 2/** 3 * Copyright since 2007 PrestaShop SA and Contributors 4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Open Software License (OSL 3.0) 9 * that is bundled with this package in the file LICENSE.md. 10 * It is also available through the world-wide-web at this URL: 11 * https://opensource.org/licenses/OSL-3.0 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to https://devdocs.prestashop.com/ for more information. 21 * 22 * @author PrestaShop SA and Contributors <contact@prestashop.com> 23 * @copyright Since 2007 PrestaShop SA and Contributors 24 * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) 25 */ 26declare(strict_types=1); 27 28namespace PrestaShop\PrestaShop\Core\Form; 29 30use Exception; 31use PrestaShop\PrestaShop\Core\Hook\HookDispatcherInterface; 32use Symfony\Component\Form\FormFactoryInterface; 33use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; 34 35/** 36 * Complete implementation of FormHandlerInterface. 37 */ 38class Handler implements FormHandlerInterface 39{ 40 /** 41 * @var string 42 */ 43 public $form; 44 45 /** 46 * @var FormFactoryInterface the form factory 47 */ 48 protected $formFactory; 49 50 /** 51 * @var FormDataProviderInterface the form data provider 52 */ 53 protected $formDataProvider; 54 55 /** 56 * @var HookDispatcherInterface the event dispatcher 57 */ 58 protected $hookDispatcher; 59 60 /** 61 * @var string the hook name 62 */ 63 protected $hookName; 64 65 /** 66 * @var string the form name 67 */ 68 protected $formName; 69 70 /** 71 * FormHandler constructor. 72 * 73 * @param FormFactoryInterface $formFactory 74 * @param HookDispatcherInterface $hookDispatcher 75 * @param FormDataProviderInterface $formDataProvider 76 * @param string $form 77 * @param string $hookName 78 * @param string $formName 79 */ 80 public function __construct( 81 FormFactoryInterface $formFactory, 82 HookDispatcherInterface $hookDispatcher, 83 FormDataProviderInterface $formDataProvider, 84 string $form, 85 $hookName, 86 $formName = 'form' 87 ) { 88 $this->formFactory = $formFactory; 89 $this->hookDispatcher = $hookDispatcher; 90 $this->formDataProvider = $formDataProvider; 91 $this->form = $form; 92 $this->hookName = $hookName; 93 $this->formName = $formName; 94 } 95 96 /** 97 * {@inheritdoc} 98 * 99 * @throws Exception 100 */ 101 public function getForm() 102 { 103 $formBuilder = $this->formFactory->createNamedBuilder($this->formName, $this->form); 104 105 $formBuilder->setData($this->formDataProvider->getData()); 106 107 $this->hookDispatcher->dispatchWithParameters( 108 "action{$this->hookName}Form", 109 [ 110 'form_builder' => $formBuilder, 111 ] 112 ); 113 114 return $formBuilder->getForm(); 115 } 116 117 /** 118 * {@inheritdoc} 119 * 120 * @throws Exception 121 * @throws UndefinedOptionsException 122 */ 123 public function save(array $data) 124 { 125 $errors = $this->formDataProvider->setData($data); 126 127 $this->hookDispatcher->dispatchWithParameters( 128 "action{$this->hookName}Save", 129 [ 130 'errors' => &$errors, 131 'form_data' => &$data, 132 ] 133 ); 134 135 return $errors; 136 } 137} 138