Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/mouf
/composer.lock
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: php
matrix:
include:
- php: 7.2
env: PREFER_LOWEST=""
- php: 7.1
env: PREFER_LOWEST=""
# - php: 7.1
# env: PREFER_LOWEST="--prefer-lowest"

before_script:
- composer update $PREFER_LOWEST --no-interaction
- mkdir -p build/logs
script:
- composer cs-check
- composer phpstan
27 changes: 16 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@
],
"require" : {
"mouf/utils.common.conditioninterface" : "2.*",
"php" : ">=5.3.0",
"mouf/html.renderer" : "~1.0"
"php" : ">=7.1",
"mouf/html.renderer" : "^2",
"thecodingmachine/funky": "^1"
},
"require-dev": {
"phpstan/phpstan": "^0.10.3",
"thecodingmachine/phpstan-strict-rules": "^0.10.3",
"squizlabs/php_codesniffer": "^3.3.1",
"mouf/utils.i18n.fine.translation-interface": "^4"
},
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"phpstan": "phpstan analyse src -c phpstan.neon --level=5 --no-progress -vvv"
},
"autoload" : {
"psr-0" : {
"Mouf" : "src/"
}
},
"extra" : {
"mouf" : {
"install" : [{
"file" : "src/install.php",
"type" : "file"
}
]
}
}
"minimum-stability": "dev",
"prefer-stable": true
}
3 changes: 3 additions & 0 deletions discovery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Interop\\Container\\ServiceProviderInterface": "Mouf\\Html\\Widgets\\Menu\\MenuRendererServiceProvider"
}
21 changes: 21 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="Expressive Skeleton coding standard">
<description>Expressive Skeleton coding standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="colors"/>

<!-- inherit rules from: -->
<rule ref="PSR2"/>

<!-- Paths to check -->
<file>src</file>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="300"/>
<property name="absoluteLineLimit" value="500"/>
</properties>
</rule>
</ruleset>
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
ignoreErrors:
- "#you should not use the \\$_REQUEST superglobal#"
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
226 changes: 116 additions & 110 deletions src/Mouf/Html/Widgets/Menu/Menu.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
* Copyright (c) 2012-2013 David Negrier
*
*
* See the file LICENSE.txt for copying permission.
*/
namespace Mouf\Html\Widgets\Menu;
Expand All @@ -15,117 +15,123 @@
* It is important to note that a menu item does not render directly in HTML (it has no toHtml method).
* Instead, you must use another class (a Menu renderer class) to display the menu.
* Usually, menu renderers are embedded into templates.
*
*
*/
class Menu implements MenuInterface, HtmlElementInterface {
use Renderable;

/**
* The children menu item of this menu (if any).
*
* @var array<MenuItemInterface>
*/
private $children;

/**
* This condition must be matched to display the menu.
* Otherwise, the menu is not displayed.
* The displayCondition is optional. If no condition is set, the menu will always be displayed.
*
* @var ConditionInterface
*/
private $displayCondition;

private $sorted = false;

/**
* Constructor.
*
* @param array<MenuItemInterface> $children
*/
public function __construct($children=null) {
$this->children = $children;
}
class Menu implements MenuInterface, HtmlElementInterface
{
use Renderable;

/**
* The children menu item of this menu (if any).
*
* @var array<MenuItemInterface>
*/
private $children;

/**
* This condition must be matched to display the menu.
* Otherwise, the menu is not displayed.
* The displayCondition is optional. If no condition is set, the menu will always be displayed.
*
* @var ConditionInterface
*/
private $displayCondition;

private $sorted = false;

/**
* Constructor.
*
* @param array<MenuItemInterface> $children
*/
public function __construct(array $children = [])
{
$this->children = $children;
}

/**
* Returns a list of children elements for the menu (if there are some).
* @return array<MenuItemInterface>
*/
public function getChildren() {
if ($this->sorted == false && $this->children) {
// First, let's make 2 arrays: the array of children with a priority, and the array without.
$childrenWithPriorities = array();
$childrenWithoutPriorities = array();
foreach ($this->children as $child) {
/* @var $child MenuItemInterface */
$priority = $child->getPriority();
if ($priority === null || $priority === "") {
$childrenWithoutPriorities[] = $child;
} else {
$childrenWithPriorities[] = $child;
}
}

usort($childrenWithPriorities, array($this, "compareMenuItems"));
$this->children = array_merge($childrenWithPriorities, $childrenWithoutPriorities);
$this->sorted = true;
}
return $this->children;
}
/**
* Returns a list of children elements for the menu (if there are some).
* @return array<MenuItemInterface>
*/
public function getChildren(): array
{
if ($this->sorted === false && $this->children !== []) {
// First, let's make 2 arrays: the array of children with a priority, and the array without.
$childrenWithPriorities = array();
$childrenWithoutPriorities = array();
foreach ($this->children as $child) {
/* @var $child MenuItemInterface */
$priority = $child->getPriority();
if ($priority === null) {
$childrenWithoutPriorities[] = $child;
} else {
$childrenWithPriorities[] = $child;
}
}

usort($childrenWithPriorities, array($this, "compareMenuItems"));
$this->children = array_merge($childrenWithPriorities, $childrenWithoutPriorities);
$this->sorted = true;
}
return $this->children;
}

public function compareMenuItems(MenuItem $item1, MenuItem $item2) {
$priority1 = $item1->getPriority();
$priority2 = $item2->getPriority();
/*if ($priority1 === null && $priority2 === null) {
// If no priority is set, let's keep the default ordering (which happens is usort by always returning positive numbers...)
return 1;
}*/
return $priority1 - $priority2;
}

/**
* The children menu item of this menu (if any).
*
* @Property
* @param array<MenuItemInterface> $children
*/
public function setChildren(array $children) {
$this->sorted = false;
$this->children = $children;
}

/**
* Adds one child menu item to this menu item.
*
* @param MenuItem $child
*/
public function addChild(MenuItem $child) {
$this->sorted = false;
$this->children[] = $child;
}

/**
* If set, this display condition is tested. If it returns false, the menu will be hidden.
*
* @Property
* @param ConditionInterface $displayCondition
*/
public function setDisplayCondition(ConditionInterface $displayCondition) {
$this->displayCondition = $displayCondition;
}

public function compareMenuItems(MenuItem $item1, MenuItem $item2): int
{
$priority1 = $item1->getPriority();
$priority2 = $item2->getPriority();
/*if ($priority1 === null && $priority2 === null) {
// If no priority is set, let's keep the default ordering (which happens is usort by always returning positive numbers...)
return 1;
}*/
return $priority1 <=> $priority2;
}

/**
* The children menu item of this menu (if any).
*
* @Property
* @param array<MenuItemInterface> $children
*/
public function setChildren(array $children): void
{
$this->sorted = false;
$this->children = $children;
}

/**
* Adds one child menu item to this menu item.
*
* @param MenuItem $child
*/
public function addChild(MenuItem $child): void
{
$this->sorted = false;
$this->children[] = $child;
}

/**
* If set, this display condition is tested. If it returns false, the menu will be hidden.
*
* @Property
* @param ConditionInterface $displayCondition
*/
public function setDisplayCondition(ConditionInterface $displayCondition): void
{
$this->displayCondition = $displayCondition;
}


/**
* If this function returns true, the menu item should not be displayed.
*
* @return bool
*/
public function isHidden() {
if ($this->displayCondition == null) {
return false;
}
return !$this->displayCondition->isOk();
}
/**
* If this function returns true, the menu item should not be displayed.
*
* @return bool
*/
public function isHidden(): bool
{
if ($this->displayCondition == null) {
return false;
}
return !$this->displayCondition->isOk();
}
}
?>
35 changes: 17 additions & 18 deletions src/Mouf/Html/Widgets/Menu/MenuInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
* Copyright (c) 2012 David Negrier
*
*
* See the file LICENSE.txt for copying permission.
*/
namespace Mouf\Html\Widgets\Menu;
Expand All @@ -11,24 +11,23 @@
* It is important to note that a menu does not render directly in HTML (it has no toHtml method).
* Instead, you must use another class (a Menu renderer class) to display the menu.
* Usually, menu renderers are embedded into templates.
*
*
* @author david
*
*/
interface MenuInterface {

/**
* Returns a list of children elements for the menu (if there are some).
* @return array<MenuItemInterface>
*/
function getChildren();

/**
* If this function returns true, the menu item should not be displayed.
*
* @return bool
*/
function isHidden();
interface MenuInterface
{

/**
* Returns a list of children elements for the menu (if there are some).
* @return array<MenuItemInterface>
*/
public function getChildren(): array;

/**
* If this function returns true, the menu item should not be displayed.
*
* @return bool
*/
public function isHidden(): bool;
}

?>
Loading