‪TYPO3CMS  9.5
ArrayProcessor.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
20 
28 {
29 
33  protected ‪$data;
34 
38  public function ‪__construct(array ‪$data)
39  {
40  $this->data = ‪ArrayUtility::flatten(‪$data);
41  }
42 
47  public function ‪forEach(...$processings): array
48  {
49  $result = [];
50 
51  $processings = $this->‪getValidProcessings($processings);
52  foreach ($this->data as $key => $value) {
53  foreach ($processings as $processing) {
54  // explicitly escaping non-escaped '#' which is used
55  // as PCRE delimiter in the following processing
56  ‪$expression = preg_replace(
57  '/(?<!\\\\)#/',
58  '\\#',
59  $processing->getExpression()
60  );
61 
62  if (preg_match('#' . ‪$expression . '#', $key, $matches)) {
63  ‪$identifier = $processing->getIdentifier();
64  ‪$processor = $processing->getProcessor();
65  $result[‪$identifier] = $result[‪$identifier] ?? [];
66  $result[‪$identifier][$key] = ‪$processor($key, $value, $matches);
67  }
68  }
69  }
70 
71  return $result;
72  }
73 
79  protected function ‪getValidProcessings(array $allProcessings): array
80  {
81  $validProcessings = [];
82  $identifiers = [];
83  foreach ($allProcessings as $processing) {
84  if ($processing instanceof ‪ArrayProcessing) {
85  if (in_array($processing->getIdentifier(), $identifiers, true)) {
87  'ArrayProcessing identifier must be unique.',
88  1528638085
89  );
90  }
91  $identifiers[] = $processing->getIdentifier();
92  $validProcessings[] = $processing;
93  }
94  }
95  return $validProcessings;
96  }
97 }
‪TYPO3\CMS\Form\Domain\Configuration\Exception\ArrayProcessorException
Definition: ArrayProcessorException.php:24
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\forEach
‪array forEach(... $processings)
Definition: ArrayProcessor.php:46
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing\$expression
‪string $expression
Definition: ArrayProcessing.php:33
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing
Definition: ArrayProcessing.php:3
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing\$identifier
‪string $identifier
Definition: ArrayProcessing.php:29
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing
Definition: ArrayProcessing.php:25
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing\$processor
‪callable $processor
Definition: ArrayProcessing.php:37
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor
Definition: ArrayProcessor.php:28
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\$data
‪array $data
Definition: ArrayProcessor.php:32
‪TYPO3\CMS\Core\Utility\ArrayUtility\flatten
‪static array flatten(array $array, $prefix='')
Definition: ArrayUtility.php:479
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\getValidProcessings
‪ArrayProcessing[] getValidProcessings(array $allProcessings)
Definition: ArrayProcessor.php:78
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\__construct
‪__construct(array $data)
Definition: ArrayProcessor.php:37