@param array $tree

@return array[]
@throws \InvalidArgumentException
16855
private static function splitFilters(array $tree): array
    {
        $n = \count($tree);

        $filters = $temp = [];
        $lastSplit = null;

        $first = \array_search('|', $tree, false);

        for ($i = $n - 1; $i >= $first; --$i) {
            $part = $tree[$n];

            if (\is_array($part)) {
                $temp[] = $part;
                continue;
            }

            /* @var Token $part */
            if ($part->is('|')) {
                if ($temp === []) {
                    throw new \InvalidArgumentException('Invalid filter syntax');
                }

                $count = \count($temp);

                $args = [];
                if ($count === 1) {
                    $name = $temp[0];
                } elseif ($count === 2) {
                    [$args, $name] = $temp;
                } else {
                    break;
                }

                if ($name instanceof Token && \is_array($args) && $name->is(T_STRING)) {
                    $lastSplit = $i;
                    $filters[] = [$name, $args];
                } else {
                    break;
                }
            } else {
                $temp[] = $part;
            }
        }

        $tree = \array_slice($tree, 0, $lastSplit);
        $filters = \array_reverse($filters);

        return [$tree, $filters];
    }
875128
public function generateTreePerimeter(array $tree)
    {
        $result = array();

        foreach ($tree as $leaf) {
            $result = array_merge($result, $this->formatPerimeter($leaf));
        }

        return $result;
    }
132611
public function values(?string $key = null) : array
    {
        $value = $this->get($key);

        if (!is_array($value)) {
            throw new InvalidArgumentException(
                'method values() should be called on array'
            );
        }

        return array_values($value);
    }
255532
private function unusedFiles(Tree $tree): array
    {
        $used_files = DB::table('media_file')
            ->where('m_file', '=', $tree->id())
            ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
            ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
            ->pluck('multimedia_file_refn')
            ->all();

        $disk_files = [];
        $media_dir  = WT_DATA_DIR . $tree->getPreference('MEDIA_DIRECTORY', 'media/');
        $iter       = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($media_dir, FilesystemIterator::FOLLOW_SYMLINKS));

        foreach ($iter as $file) {
            if ($file->isFile()) {
                $filename = substr($file->getPathname(), strlen($media_dir));
                // Older versions of webtrees used a couple of special folders.
                if (strpos($filename, 'thumbs/') !== 0 && strpos($filename, 'watermarks/') !== 0) {
                    $disk_files[] = $filename;
                }
            }
        }

        $unused_files = array_diff($disk_files, $used_files);

        sort($unused_files);

        return array_combine($unused_files, $unused_files);
    }
969832
private function getHierarchy($tree, $place, $parent): ?array
    {
        $child_places = $place->getChildPlaces();
        $numfound     = count($child_places);

        if ($numfound > 0) {
            $divisor = $numfound > 20 ? 3 : 2;

            return
                [
                    'tree'      => $tree,
                    'col_class' => 'w-' . ($divisor === 2 ? '25' : '50'),
                    'columns'   => array_chunk($child_places, (int) ceil($numfound / $divisor)),
                    'place'     => $place,
                    'parent'    => $parent,
                ];
        }

        return null;
    }
76910
protected function getIdsForTree($tree)
    {
        $ids = [];

        foreach ($tree as $key => $item) {
            $ids[] = $item['id'];

            /** @noinspection SlowArrayOperationsInLoopInspection */
            $ids = array_merge($ids, $this->getIdsForTree($tree[$key]['__children']));
        }

        return $ids;
    }
1032202
protected function mergeTrees(array $trees): array
    {
        if (count($trees) == 0) {
            return [];
        }

        $tree1 = array_shift($trees);
        while (count($trees)) {
            $tree2 = array_shift($trees);
            foreach ($tree2 as $key => $value) {
                if (in_array($key, $this->permissionKeys)) {
                    $tree1 = $tree2;
                    break;
                }
                if (isset($tree1[$key]) && is_array($value)) {
                    $tree1[$key] = $this->mergeTrees([$tree1[$key], $tree2[$key]]);
                    continue;
                }
                $tree1[$key] = $value;
            }
        }

        return $tree1;
    }
278545
protected function makeTree(array &$items)
    {
        $result = [];
        $tops = [];

        /**
         * @var Entity $item
         */
        foreach ($items as $item) {
            $result[$item->getKey()] = $item;
        }

        foreach ($items as $item) {
            $parentId = $item->{$item->getParentIdColumn()};

            if (array_key_exists($parentId, $result)) {
                $result[$parentId]->appendRelation($item->getChildrenRelationIndex(), $item);
            } else {
                $tops[] = $item;
            }
        }

        return $tops;
    }
338202
public static function getAll(): array
    {
        if (empty(self::$trees)) {
            self::$trees = self::all()->all();
        }

        return self::$trees;
    }
997847
public function genealogyMenu(?Tree $tree): array
    {
        if ($tree === null) {
            return [];
        }

        return app(ModuleService::class)->findByComponent(ModuleMenuInterface::class, $tree, Auth::user())
            ->map(static function (ModuleMenuInterface $menu) use ($tree): ?Menu {
                return $menu->getMenu($tree);
            })
            ->filter()
            ->all();
    }
500262
private function getEvents($tree, $place): array
    {
        $indilist = DB::table('individuals')
            ->join('placelinks', static function (JoinClause $join): void {
                $join
                    ->on('pl_file', '=', 'i_file')
                    ->on('pl_gid', '=', 'i_id');
            })
            ->where('i_file', '=', $tree->id())
            ->where('pl_p_id', '=', $place->id())
            ->select(['individuals.*'])
            ->distinct()
            ->get()
            ->map(Individual::rowMapper())
            ->filter(Individual::accessFilter())
            ->all();

        $famlist = DB::table('families')
            ->join('placelinks', static function (JoinClause $join): void {
                $join
                    ->on('pl_file', '=', 'f_file')
                    ->on('pl_gid', '=', 'f_id');
            })
            ->where('f_file', '=', $tree->id())
            ->where('pl_p_id', '=', $place->id())
            ->select(['families.*'])
            ->distinct()
            ->get()
            ->map(Family::rowMapper())
            ->filter(Family::accessFilter())
            ->all();

        return [
            'indilist' => $indilist,
            'famlist'  => $famlist,
        ];
    }
287955
protected function optimizeTree(array $nodes) : array
    {
        $nodes = array_reduce($nodes, function (array $nodes, $current) {
            $last = end($nodes);
            if ($last instanceof TagNode && $current instanceof TagNode) {
                // If last and current tags are the same.
                $lastTag = $last->getTag();
                $currentTag = $current->getTag();
                if ($lastTag == $currentTag) {
                    // Merge children of last and current nodes.
                    $children = array_merge($last->getChildren(), $current->getChildren());
                    $current = new TagNode($currentTag, $children);
                    // Remove last node.
                    array_pop($nodes);
                }
            }

            return array_merge($nodes, [$current]);
        }, []);

        // Optimize each children tag.
        return array_map(function ($node) {
            if ($node instanceof TagNode) {
                $tag = $node->getTag();
                $children = $this->optimizeTree($node->getChildren());
                return new TagNode($tag, $children);
            }

            return $node;
        }, $nodes);
    }
1324052
public function reverseTree($tree = [])
    {
        if (!$tree) {
            $tree = $this->tree;
        }
        $tempArr = [];
        foreach ($tree as $k => $node) {
            $nodeChild = $node['children'];
            if ($nodeChild) {
                $node['children'] = $this->reverseTree($nodeChild);
            }
            array_unshift($tempArr, $node);
        }
        return $tempArr;
    }
1014242
public function fillTreeNodes($tree)
    {
        $ids = $this->getIdsForTree($tree);

        /** @var array $nodes_array */
        $nodes_array = $this->findBy(['id' => $ids]);

        $nodes = [];

        foreach ($nodes_array as $nodes_array_item) {
            /* @var $nodes_array_item \Compo\Sonata\AdminBundle\Entity\IdEntityTrait */
            $nodes[$nodes_array_item->getId()] = $nodes_array_item;
        }

        $tree = $this->fillTreeNodesForItems($tree, $nodes);

        return $tree;
    }
525455
public function getShowAction(Tree $tree): ResponseInterface
    {
        return $this->viewResponse('modules/clippings/show', [
            'records' => $this->allRecordsInCart($tree),
            'title'   => I18N::translate('Family tree clippings cart'),
            'tree'    => $tree,
        ]);
    }
1331937
private function buildArrayFromNode(SimpleXMLElement $node): array
    {
        $array = [];
        foreach ($node->children() as $child) {
            if ((string) $child !== '') {
                $array[] = (string) $child;
            }
        }

        return $array;
    }
282106
protected static function tree_array_sort($tree , $orderfield = 'listorder' ){
        $temp_arr = $arg = [];

        for($i = 0; $i<count($tree) ;$i++){
            for($j = 0 ;$j < (count($tree)-1); $j++){
                $now = $tree[$j];
                $prev = $tree[$j+1];

                if($now[$orderfield] < $prev[$orderfield])
                {
                    $temp = $now;
                    $tree[$j] = $prev;
                    $tree[$j+1] = $now;
                }

            }

            if(count($tree[$i]['son']) > 0){

                $tree[$i]['son'] = self::tree_array_sort($tree[$i]['son']);

            }
        }



        return $tree;

    }