‪TYPO3CMS  ‪main
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 
36  public function ‪__construct(array ‪$data)
37  {
39  }
40 
44  public function ‪forEach(...$processings): array
45  {
46  $result = [];
47 
48  $processings = $this->‪getValidProcessings($processings);
49  foreach ($this->data as $key => $value) {
50  foreach ($processings as $processing) {
51  // explicitly escaping non-escaped '#' which is used
52  // as PCRE delimiter in the following processing
53  ‪$expression = preg_replace(
54  '/(?<!\\\\)#/',
55  '\\#',
56  $processing->getExpression()
57  );
58 
59  if (preg_match('#' . ‪$expression . '#', $key, $matches)) {
60  ‪$identifier = $processing->getIdentifier();
61  ‪$processor = $processing->getProcessor();
62  $result[‪$identifier] = $result[‪$identifier] ?? [];
63  $result[‪$identifier][$key] = ‪$processor($key, $value, $matches);
64  }
65  }
66  }
67 
68  return $result;
69  }
70 
75  protected function ‪getValidProcessings(array $allProcessings): array
76  {
77  $validProcessings = [];
78  $identifiers = [];
79  foreach ($allProcessings as $processing) {
80  if ($processing instanceof ArrayProcessing) {
81  if (in_array($processing->getIdentifier(), $identifiers, true)) {
82  throw new ArrayProcessorException(
83  'ArrayProcessing identifier must be unique.',
84  1528638085
85  );
86  }
87  $identifiers[] = $processing->getIdentifier();
88  $validProcessings[] = $processing;
89  }
90  }
91  return $validProcessings;
92  }
93 }
‪TYPO3\CMS\Core\Utility\ArrayUtility\flattenPlain
‪static flattenPlain(array $array)
Definition: ArrayUtility.php:496
‪TYPO3\CMS\Form\Domain\Configuration\Exception\ArrayProcessorException
Definition: ArrayProcessorException.php:25
‪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\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:26
‪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:74
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\__construct
‪__construct(array $data)
Definition: ArrayProcessor.php:35
‪TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor\forEach
‪forEach(... $processings)
Definition: ArrayProcessor.php:43