$childData) { if (in_array($childSection, self::$specialGroups)) { if (isset($changedStructure[$childSection]['children'])) { $children = $changedStructure[$childSection]['children']; if (isset($childData['children'])) { $children += $childData['children']; } $childData['children'] = $children; unset($children); } $changedStructure[$childSection] = $childData; } else { $moveInstructions = $this->getMoveInstructions($childSection, $childData); if (!empty($moveInstructions)) { foreach ($moveInstructions as $moveInstruction) { unset($childData['children'][$moveInstruction['section']]); unset($moveInstruction['data']['displayIn']); $changedStructure [$moveInstruction['parent']] ['children'] [$moveInstruction['section']] = $moveInstruction['data']; } } if (!isset($moveInstructions[$childSection])) { $changedStructure['other_payment_methods']['children'][$childSection] = $childData; } } } return $changedStructure; } /** * Recursively collect groups that should be moved to special section * * @param string $section * @param array $data * @return array */ private function getMoveInstructions($section, $data) { $moved = []; if (array_key_exists('children', $data)) { foreach ($data['children'] as $childSection => $childData) { $movedChildren = $this->getMoveInstructions($childSection, $childData); if (isset($movedChildren[$childSection])) { unset($data['children'][$childSection]); } $moved = array_merge($moved, $movedChildren); } } if (isset($data['displayIn']) && in_array($data['displayIn'], self::$specialGroups)) { $moved = array_merge( [ $section => [ 'parent' => $data['displayIn'], 'section' => $section, 'data' => $data ] ], $moved ); } return $moved; } }