TYPO3 CMS  TYPO3_8-7
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 
27 {
28 
32  protected $data;
33 
37  public function __construct(array $data)
38  {
39  $this->data = ArrayUtility::flatten($data);
40  }
41 
46  public function forEach(...$processings): array
47  {
48  $result = [];
49 
50  $processings = $this->getValidProcessings($processings);
51  foreach ($this->data as $key => $value) {
52  foreach ($processings as $processing) {
53  // explicitly escaping non-escaped '#' which is used
54  // as PCRE delimiter in the following processing
55  $expression = preg_replace(
56  '/(?<!\\\\)#/',
57  '\\#',
58  $processing->getExpression()
59  );
60 
61  if (preg_match('#' . $expression . '#', $key, $matches)) {
62  $identifier = $processing->getIdentifier();
63  $processor = $processing->getProcessor();
64  $result[$identifier] = $result[$identifier] ?? [];
65  $result[$identifier][$key] = $processor($key, $value, $matches);
66  }
67  }
68  }
69 
70  return $result;
71  }
72 
78  protected function getValidProcessings(array $allProcessings): array
79  {
80  $validProcessings = [];
81  $identifiers = [];
82  foreach ($allProcessings as $processing) {
83  if ($processing instanceof ArrayProcessing) {
84  if (in_array($processing->getIdentifier(), $identifiers, true)) {
85  throw new ArrayProcessorException(
86  'ArrayProcessing identifier must be unique.',
87  1528638085
88  );
89  }
90  $identifiers[] = $processing->getIdentifier();
91  $validProcessings[] = $processing;
92  }
93  }
94  return $validProcessings;
95  }
96 }
static flatten(array $array, $prefix='')