data = ArrayUtility::flattenPlain($data); } /** * @param ArrayProcessing[] $processings */ public function forEach(...$processings): array { $result = []; $processings = $this->getValidProcessings($processings); foreach ($this->data as $key => $value) { foreach ($processings as $processing) { // explicitly escaping non-escaped '#' which is used // as PCRE delimiter in the following processing $expression = preg_replace( '/(?getExpression() ); if (preg_match('#' . $expression . '#', $key, $matches)) { $identifier = $processing->getIdentifier(); $processor = $processing->getProcessor(); $result[$identifier] = $result[$identifier] ?? []; $result[$identifier][$key] = $processor($key, $value, $matches); } } } return $result; } /** * @return ArrayProcessing[] * @throws ArrayProcessorException */ protected function getValidProcessings(array $allProcessings): array { $validProcessings = []; $identifiers = []; foreach ($allProcessings as $processing) { if ($processing instanceof ArrayProcessing) { if (in_array($processing->getIdentifier(), $identifiers, true)) { throw new ArrayProcessorException( 'ArrayProcessing identifier must be unique.', 1528638085 ); } $identifiers[] = $processing->getIdentifier(); $validProcessings[] = $processing; } } return $validProcessings; } }