40fa5d424ae021e520b49964e15c1efe3b71d0c3
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / ContentDataProcessor.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
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\ArrayUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * A class that contains methods that can be used to use the dataProcessing functionality
22 */
23 class ContentDataProcessor
24 {
25 /**
26 * Check for the availability of processors, defined in TypoScript, and use them for data processing
27 *
28 * @param ContentObjectRenderer $cObject
29 * @param array $configuration Configuration array
30 * @param array $variables the variables to be processed
31 * @return array the processed data and variables as key/value store
32 * @throws \UnexpectedValueException If a processor class does not exist
33 */
34 public function process(ContentObjectRenderer $cObject, array $configuration, array $variables)
35 {
36 if (
37 !empty($configuration['dataProcessing.'])
38 && is_array($configuration['dataProcessing.'])
39 ) {
40 $processors = $configuration['dataProcessing.'];
41 $processorKeys = ArrayUtility::filterAndSortByNumericKeys($processors);
42
43 foreach ($processorKeys as $key) {
44 $className = $processors[$key];
45 if (!class_exists($className)) {
46 throw new \UnexpectedValueException('Processor class name "' . $className . '" does not exist!', 1427455378);
47 }
48
49 if (!in_array(DataProcessorInterface::class, class_implements($className), true)) {
50 throw new \UnexpectedValueException(
51 'Processor with class name "' . $className . '" ' .
52 'must implement interface "' . DataProcessorInterface::class . '"',
53 1427455377
54 );
55 }
56
57 $processorConfiguration = isset($processors[$key . '.']) ? $processors[$key . '.'] : [];
58
59 $variables = GeneralUtility::makeInstance($className)->process(
60 $cObject,
61 $configuration,
62 $processorConfiguration,
63 $variables
64 );
65 }
66 }
67
68 return $variables;
69 }
70 }