[TASK] Refactor PostProcessorTest to use Prophecy
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / PostProcess / PostProcessor.php
1 <?php
2 namespace TYPO3\CMS\Form\PostProcess;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\ObjectFactory;
19
20 /**
21 * The post processor
22 */
23 class PostProcessor {
24
25 /**
26 * @var \TYPO3\CMS\Form\View\Form\FormView
27 */
28 protected $form;
29
30 /**
31 * @var \TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory
32 */
33 protected $typoscriptFactory;
34
35 /**
36 * Constructor
37 *
38 * @param \TYPO3\CMS\Form\Domain\Model\Form $form Form domain model
39 * @param array $typoScript Post processor TypoScript settings
40 */
41 public function __construct(\TYPO3\CMS\Form\Domain\Model\Form $form, array $typoScript) {
42 $this->form = $form;
43 $this->typoscriptFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory::class);
44 $this->typoScript = $typoScript;
45 }
46
47 /**
48 * The main method called by the controller
49 *
50 * Iterates over the configured post processors and calls them with their
51 * own settings
52 *
53 * @return string HTML messages from the called processors
54 */
55 public function process() {
56 $html = '';
57 if (is_array($this->typoScript)) {
58 $keys = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($this->typoScript);
59 $layoutHandler = $this->typoscriptFactory->setLayoutHandler($this->typoScript);
60
61 foreach ($keys as $key) {
62 if (!(int)$key || strpos($key, '.') !== FALSE) {
63 continue;
64 }
65 $className = FALSE;
66 $processorName = $this->typoScript[$key];
67 $processorArguments = array();
68 if (isset($this->typoScript[$key . '.'])) {
69 $processorArguments = $this->typoScript[$key . '.'];
70 }
71 if (class_exists($processorName, TRUE)) {
72 $className = $processorName;
73 } else {
74 $classNameExpanded = 'TYPO3\\CMS\\Form\\PostProcess\\' . ucfirst(strtolower($processorName)) . 'PostProcessor';
75 if (class_exists($classNameExpanded, TRUE)) {
76 $className = $classNameExpanded;
77 }
78 }
79 if ($className !== FALSE) {
80 $layout = $this->typoscriptFactory->getLayoutFromTypoScript($this->typoScript[$processorName . '.']);
81 $layoutHandler->setLayout($layout);
82
83 $processor = ObjectFactory::createFormObject($className, $this->form, $processorArguments);
84 if ($processor instanceof PostProcessorInterface) {
85 $html .= $processor->process();
86 }
87 }
88 }
89 }
90 return $html;
91 }
92
93 }