loophp /
phptree
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * For the full copyright and license information, please view |
||
| 5 | * the LICENSE file that was distributed with this source code. |
||
| 6 | */ |
||
| 7 | |||
| 8 | declare(strict_types=1); |
||
| 9 | |||
| 10 | namespace loophp\phptree\Builder; |
||
| 11 | |||
| 12 | use loophp\phptree\Node\NodeInterface; |
||
| 13 | |||
| 14 | use function is_callable; |
||
| 15 | |||
| 16 | class Random implements BuilderInterface |
||
| 17 | { |
||
| 18 | 1 | public static function create(iterable $nodes): ?NodeInterface |
|
| 19 | { |
||
| 20 | 1 | $root = null; |
|
| 21 | |||
| 22 | 1 | foreach ($nodes as $key => $value) { |
|
| 23 | 1 | if (0 === $key) { |
|
| 24 | 1 | $root = self::createNode($value); |
|
| 25 | |||
| 26 | 1 | continue; |
|
| 27 | } |
||
| 28 | |||
| 29 | 1 | if (!$root instanceof NodeInterface) { |
|
| 30 | 1 | continue; |
|
| 31 | } |
||
| 32 | |||
| 33 | 1 | self::pickRandomNode($root)->add(self::createNode($value)); |
|
| 34 | } |
||
| 35 | |||
| 36 | 1 | return $root; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param array<int, class-string|callable():(NodeInterface)|mixed> $parameters |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 41 | */ |
||
| 42 | 1 | private static function createNode(array $parameters = []): NodeInterface |
|
| 43 | { |
||
| 44 | 1 | $parameters = array_map( |
|
| 45 | /** |
||
| 46 | * @param class-string|callable():(NodeInterface)|mixed $parameter |
||
|
0 ignored issues
–
show
|
|||
| 47 | * |
||
| 48 | * @return class-string|mixed |
||
|
0 ignored issues
–
show
|
|||
| 49 | */ |
||
| 50 | static function ($parameter) { |
||
| 51 | 1 | if (is_callable($parameter)) { |
|
| 52 | 1 | return $parameter(); |
|
| 53 | } |
||
| 54 | |||
| 55 | 1 | return $parameter; |
|
| 56 | 1 | }, |
|
| 57 | $parameters |
||
| 58 | ); |
||
| 59 | |||
| 60 | 1 | $class = array_shift($parameters); |
|
| 61 | |||
| 62 | 1 | return new $class(...$parameters); |
|
| 63 | } |
||
| 64 | |||
| 65 | 1 | private static function pickRandomNode(NodeInterface $node): NodeInterface |
|
| 66 | { |
||
| 67 | 1 | $pick = (int) random_int(0, $node->count()); |
|
| 68 | |||
| 69 | 1 | $i = 0; |
|
| 70 | |||
| 71 | 1 | foreach ($node->all() as $child) { |
|
| 72 | 1 | if (++$i === $pick) { |
|
| 73 | 1 | return $child; |
|
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | 1 | return $node; |
|
| 78 | } |
||
| 79 | } |
||
| 80 |