waynestate/parse-menu

解析Wayne State University API中的菜单

1.2.2 2016-11-04 15:09 UTC

This package is auto-updated.

Last update: 2024-09-08 07:09:29 UTC


README

解析Wayne State University API中的菜单

Latest Stable Version Build Status Total Downloads License

安装

要安装此库,请运行以下命令,您将获取最新版本

composer require waynestate/parse-menu

用法

创建对象

# start.php

use Waynestate\Menuitems\ParseMenu;

...

$parseMenu = new ParseMenu;

对菜单进行API调用

# controller.php

try {
    // Pull a specific menu from the API for a site
    $params = array(
        'site_id' => 1,
        'menu_id' => 2,
        'ttl' => TTL,
    );
    $menus = $api->sendRequest('cms.menuitems.listing', $params);

    // Menu config
    $menu_config = array(
        'page_selected' => 1,
    );

    // Get a final array to display the main menu
    $main_menu = $parseMenu->parse($menus[2], $menu_config);
    
    // Get the breadcrumbs from the parsed menu $main_menu
    $breadcrumbs = array();
    if(count($site_menu['meta']['path']) > 0) {
        $breadcrumbs = $parseMenu->getBreadCrumbs($main_menu);
        
        // Add the site root crumb
        $root_crumb = [
            'display_name' => 'Home',
            'relative_url' => '/',
        ];
        $breadcrumbs = $parseMenu->prependBreadCrumb($breadcrumbs, $root_crumb);
    }
    
    // Just display the first level in the header
    $top_menu_config = array(
        'display_levels' => 1,
    );

    // Parse the existing menu with the specific top_menu config
    $top_menu = $parseMenu->parse($main_menu, $top_menu_config);

} catch (Exception $e) {

    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

返回值

$main_menu['meta'] = [
    'has_selected' => boolean, // default: false
    'has_submenu' => boolean, // default: false
    'depth' => integer, // default: 0
    'path' => array, // default: array()
]

$main_menu['menu'] = [
    // Array of the menu
]

$breadcrumbs = [
    // Sequential array of individual breadcrumbs in order based on the $main_menu['meta']['path']
]

配置选项

'page_selected' = Page ID for selection path (optional)
'skip_levels' = Number of levels to skip from the root (requires page_selected)
'display_levels' = Number of levels to display from the root (requires page_selected, if > 1)
'full_menu' = Return the full menu regardless if there is a page selected (boolean, default: false)
TODO: 'show_levels' = Number of levels to display from the leaf (requires page_selected)
TODO: 'add_home' = Add 'Home' as the first menu item (this may not be needed)

异常

InvalidDisplayLevelsException = If 'display_levels' > 1 and no 'page_selected' found

测试

phpunit

代码覆盖率

phpunit --coverage-html ./coverage