‪TYPO3CMS  10.4
PermutationUtility.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 
24 {
36  public static function ‪meltStringItems(array $payload, string $previousResult = ''): array
37  {
38  $results = [];
39  $items = static::nextItems($payload);
40  foreach ($items as $item) {
41  $resultItem = $previousResult . static::asString($item);
42  if (!empty($payload)) {
43  $results = array_merge(
44  $results,
45  static::meltStringItems($payload, $resultItem)
46  );
47  continue;
48  }
49  $results[] = $resultItem;
50  }
51  return $results;
52  }
53 
66  public static function ‪meltArrayItems(array $payload, array $previousResult = []): array
67  {
68  $results = [];
69  $items = static::nextItems($payload);
70  foreach ($items as $item) {
71  $resultItems = $previousResult;
72  $resultItems[] = $item;
73  if (!empty($payload)) {
74  $results = array_merge(
75  $results,
76  static::meltArrayItems($payload, $resultItems)
77  );
78  continue;
79  }
80  $results[] = $resultItems;
81  }
82  return $results;
83  }
84 
85  protected static function ‪nextItems(array &$payload): iterable
86  {
87  $items = array_shift($payload);
88  if (is_iterable($items)) {
89  return $items;
90  }
91  throw new \LogicException(
92  sprintf('Expected iterable, got %s', gettype($items)),
93  1578164101
94  );
95  }
96 
97  protected static function ‪asString($item): string
98  {
99  if (is_string($item)) {
100  return $item;
101  }
102  if (is_object($item) && method_exists($item, '__toString')) {
103  return (string)$item;
104  }
105  throw new \LogicException(
106  sprintf('Expected string, got %s', gettype($item)),
107  1578164102
108  );
109  }
110 }
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltStringItems
‪static array meltStringItems(array $payload, string $previousResult='')
Definition: PermutationUtility.php:36
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\PermutationUtility\nextItems
‪static nextItems(array &$payload)
Definition: PermutationUtility.php:85
‪TYPO3\CMS\Core\Utility\PermutationUtility
Definition: PermutationUtility.php:24
‪TYPO3\CMS\Core\Utility\PermutationUtility\asString
‪static asString($item)
Definition: PermutationUtility.php:97
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltArrayItems
‪static array meltArrayItems(array $payload, array $previousResult=[])
Definition: PermutationUtility.php:66