‪TYPO3CMS  11.5
ArrayProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
22 
30 {
34  protected ‪$data;
35 
39  public function ‪__construct(array ‪$data)
40  {
42  }
43 
48  public function ‪forEach(...$processings): array
49  {
50  $result = [];
51 
52  $processings = $this->‪getValidProcessings($processings);
53  foreach ($this->data as $key => $value) {
54  foreach ($processings as $processing) {
55  // explicitly escaping non-escaped '#' which is used
56  // as PCRE delimiter in the following processing
57  ‪$expression = preg_replace(
58  '/(?<!\\\\)#/',
59  '\\#',
60  $processing->getExpression()
61  );
62 
63  if (preg_match('#' . ‪$expression . '#', $key, $matches)) {
64  ‪$identifier = $processing->getIdentifier();
65  ‪$processor = $processing->getProcessor();
66  $result[‪$identifier] = $result[‪$identifier] ?? [];
67  $result[‪$identifier][$key] = ‪$processor($key, $value, $matches);
68  }
69  }
70  }
71 
72  return $result;
73  }
74 
80  protected function ‪getValidProcessings(array $allProcessings): array
81  {
82  $validProcessings = [];
83  $identifiers = [];
84  foreach ($allProcessings as $processing) {
85  if ($processing instanceof ‪ArrayProcessing) {
86  if (in_array($processing->getIdentifier(), $identifiers, true)) {
88  'ArrayProcessing identifier must be unique.',
89  1528638085
90  );
91  }
92  $identifiers[] = $processing->getIdentifier();
93  $validProcessings[] = $processing;
94  }
95  }
96  return $validProcessings;
97  }
98 }
‪TYPO3\CMS\Form\Domain\Configuration\Exception\ArrayProcessorException
Definition: ArrayProcessorException.php:25
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\forEach
‪array forEach(... $processings)
Definition: ArrayProcessor.php:47
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing\$expression
‪string $expression
Definition: ArrayProcessing.php:34
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing
Definition: ArrayProcessing.php:18
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing\$identifier
‪string $identifier
Definition: ArrayProcessing.php:30
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing
Definition: ArrayProcessing.php:27
‪TYPO3\CMS\Core\Utility\ArrayUtility\flattenPlain
‪static array flattenPlain(array $array)
Definition: ArrayUtility.php:515
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing\$processor
‪callable $processor
Definition: ArrayProcessing.php:38
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor
Definition: ArrayProcessor.php:30
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\$data
‪array $data
Definition: ArrayProcessor.php:33
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\getValidProcessings
‪ArrayProcessing[] getValidProcessings(array $allProcessings)
Definition: ArrayProcessor.php:79
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\__construct
‪__construct(array $data)
Definition: ArrayProcessor.php:38