Grav\Plugin\Admin\AdminController::findFirstAvailable PHP Метод

findFirstAvailable() защищенный Метод

Find the first available $item ('slug' | 'folder') for a page Used when copying a page, to determine the first available slot
protected findFirstAvailable ( string $item, Grav\Common\Page\Page $page ) : string
$item string
$page Grav\Common\Page\Page
Результат string The first available slot
    protected function findFirstAvailable($item, $page)
    {
        if (!$page->parent()->children()) {
            return $page->{$item}();
        }
        $withoutPrefix = function ($string) {
            $match = preg_split('/^[0-9]+\\./u', $string, 2, PREG_SPLIT_DELIM_CAPTURE);
            return isset($match[1]) ? $match[1] : $match[0];
        };
        $withoutPostfix = function ($string) {
            $match = preg_split('/-(\\d+)$/', $string, 2, PREG_SPLIT_DELIM_CAPTURE);
            return $match[0];
        };
        /* $appendedNumber = function ($string) {
                    $match  = preg_split('/-(\d+)$/', $string, 2, PREG_SPLIT_DELIM_CAPTURE);
                    $append = (isset($match[1]) ? (int)$match[1] + 1 : 2);
        
                    return $append;
                };*/
        $highest = 1;
        $siblings = $page->parent()->children();
        $findCorrectAppendedNumber = function ($item, $page_item, $highest) use($siblings, &$findCorrectAppendedNumber, &$withoutPrefix) {
            foreach ($siblings as $sibling) {
                if ($withoutPrefix($sibling->{$item}()) == ($highest === 1 ? $page_item : $page_item . '-' . $highest)) {
                    $highest = $findCorrectAppendedNumber($item, $page_item, $highest + 1);
                    return $highest;
                }
            }
            return $highest;
        };
        $base = $withoutPrefix($withoutPostfix($page->{$item}()));
        $return = $base;
        $highest = $findCorrectAppendedNumber($item, $base, $highest);
        if ($highest > 1) {
            $return .= '-' . $highest;
        }
        return $return;
    }