‪TYPO3CMS  11.5
NormalizeTreeTrait.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 
21 {
25  private function ‪sortTreeArray(array $tree): array
26  {
27  ksort($tree);
28  return array_map(
29  function (array $item) {
30  foreach ($item as $propertyName => $propertyValue) {
31  if (!is_array($propertyValue)) {
32  continue;
33  }
34  $item[$propertyName] = $this->sortTreeArray($propertyValue);
35  }
36  return $item;
37  },
38  $tree
39  );
40  }
41 
49  private function ‪normalizeTreeArray(array $tree, array $keepProperties): array
50  {
51  return array_map(
52  function (array $item) use ($keepProperties) {
53  // only keep these property names
54  $item = array_intersect_key($item, $keepProperties);
55  foreach ($item as $propertyName => $propertyValue) {
56  if (!is_array($propertyValue)) {
57  continue;
58  }
59  // process recursively for nested array items (e.g. `_children`)
60  $item[$propertyName] = $this->normalizeTreeArray($propertyValue, $keepProperties);
61  }
62  return $item;
63  },
64  // normalize numeric indexes (remove sorting markers)
65  array_values($tree)
66  );
67  }
68 }
‪TYPO3\CMS\Backend\Tests\Functional\Tree\Repository\Fixtures\Tree\NormalizeTreeTrait
Definition: NormalizeTreeTrait.php:21
‪TYPO3\CMS\Backend\Tests\Functional\Tree\Repository\Fixtures\Tree\NormalizeTreeTrait\normalizeTreeArray
‪array normalizeTreeArray(array $tree, array $keepProperties)
Definition: NormalizeTreeTrait.php:49
‪TYPO3\CMS\Backend\Tests\Functional\Tree\Repository\Fixtures\Tree
Definition: NormalizeTreeTrait.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Tree\Repository\Fixtures\Tree\NormalizeTreeTrait\sortTreeArray
‪sortTreeArray(array $tree)
Definition: NormalizeTreeTrait.php:25