From b322e2fcd3f1d8dd3d721210b2546900f8019204 Mon Sep 17 00:00:00 2001 From: ddavis Date: Tue, 15 Aug 2023 13:42:52 -0400 Subject: [PATCH 1/3] Fix for dropdown() function. if three arguments are passed in, code was assuming that 3rd arguments is attributes and assigning the $order parameter to $attributes. When there are three arguments, 3rd argument could really be order with attributes not passed in at all. --- src/MenuBuilder.php | 3 ++- src/MenuItem.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/MenuBuilder.php b/src/MenuBuilder.php index f1adb80..a3c7c50 100644 --- a/src/MenuBuilder.php +++ b/src/MenuBuilder.php @@ -349,7 +349,8 @@ public function dropdown($title, \Closure $callback, $order = null, array $attri { $properties = compact('title', 'order', 'attributes'); - if (func_num_args() == 3) { + if (func_num_args() == 3 && is_array($order)) { + // $attributes passed in as 3rd parameter $arguments = func_get_args(); $title = Arr::get($arguments, 0); diff --git a/src/MenuItem.php b/src/MenuItem.php index 00c34ee..1465d4d 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -159,7 +159,8 @@ public function dropdown($title, \Closure $callback, $order = 0, array $attribut { $properties = compact('title', 'order', 'attributes'); - if (func_num_args() === 3) { + if (func_num_args() === 3 && is_array($order)) { + // $attributes passed in as third parameter $arguments = func_get_args(); $title = Arr::get($arguments, 0); From 7432cea7ba297490c8a4dd7317f35be5a42f282d Mon Sep 17 00:00:00 2001 From: ddavis Date: Wed, 16 Aug 2023 14:27:25 -0400 Subject: [PATCH 2/3] Allow top menu item in dropdown to have a link (or other properties) --- src/MenuBuilder.php | 39 ++++++++++++++++++++++++++++----------- src/MenuItem.php | 36 ++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/MenuBuilder.php b/src/MenuBuilder.php index a3c7c50..48355b9 100644 --- a/src/MenuBuilder.php +++ b/src/MenuBuilder.php @@ -339,24 +339,41 @@ public function add(array $attributes = array()) /** * Create new menu with dropdown. * - * @param $title - * @param callable $callback - * @param array $attributes + * @param $title_or_properties can give all properties in one argument, or give title separately + * + * @param callable $callback callback to add submenu items * * @return $this */ - public function dropdown($title, \Closure $callback, $order = null, array $attributes = array()) + + public function dropdown($title_or_properties, \Closure $callback, $order = null, array $attributes = array()) { - $properties = compact('title', 'order', 'attributes'); + if (is_array($title_or_properties)) { - if (func_num_args() == 3 && is_array($order)) { - // $attributes passed in as 3rd parameter - $arguments = func_get_args(); + if (($title_or_properties['attributes']??null) === null) + { + $title_or_properties['attributes'] = []; + } - $title = Arr::get($arguments, 0); - $attributes = Arr::get($arguments, 2); + $properties = $title_or_properties; + } + else + { + $title = $title_or_properties; + + // backwards compatible with previous code that assumes that + // if three args are passed in, third must be attributes + + if (func_num_args() === 3 && is_array($order)) { + $attributes = $order; + $properties = compact('title', 'attributes'); + } + else + { + $properties = compact('title', 'order', 'attributes'); + } - $properties = compact('title', 'attributes'); + $properties = compact('title', 'order', 'attributes'); } $item = MenuItem::make($properties); diff --git a/src/MenuItem.php b/src/MenuItem.php index 1465d4d..852b7ab 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -150,23 +150,39 @@ public function child($attributes) /** * Register new child menu with dropdown. * - * @param $title - * @param callable $callback + * @param $title_or_properties can give all properties in one argument, or give title separately + * + * @param callable $callback callback to add submenu items * * @return $this */ - public function dropdown($title, \Closure $callback, $order = 0, array $attributes = array()) + + public function dropdown($title_or_properties, \Closure $callback, $order = 0, array $attributes = array()) { - $properties = compact('title', 'order', 'attributes'); + if (is_array($title_or_properties)) { - if (func_num_args() === 3 && is_array($order)) { - // $attributes passed in as third parameter - $arguments = func_get_args(); + if (($title_or_properties['attributes']??null) === null) + { + $title_or_properties['attributes'] = []; + } - $title = Arr::get($arguments, 0); - $attributes = Arr::get($arguments, 2); + $properties = $title_or_properties; + } + else + { + $title = $title_or_properties; + + // backwards compatible with previous code that assumes that + // if three args are passed in, third must be attributes - $properties = compact('title', 'attributes'); + if (func_num_args() === 3 && is_array($order)) { + $attributes = $order; + $properties = compact('title', 'attributes'); + } + else + { + $properties = compact('title', 'order', 'attributes'); + } } $child = static::make($properties); From 8c0be4f8044599f6fa5d9fa020b59260014ae535 Mon Sep 17 00:00:00 2001 From: ddavis Date: Wed, 16 Aug 2023 14:34:14 -0400 Subject: [PATCH 3/3] fixed function documentation --- src/MenuBuilder.php | 7 ++++++- src/MenuItem.php | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/MenuBuilder.php b/src/MenuBuilder.php index 48355b9..3bd85bd 100644 --- a/src/MenuBuilder.php +++ b/src/MenuBuilder.php @@ -339,10 +339,15 @@ public function add(array $attributes = array()) /** * Create new menu with dropdown. * - * @param $title_or_properties can give all properties in one argument, or give title separately + * @param $title_or_properties can give all properties in one argument + * or give title as string title separately * * @param callable $callback callback to add submenu items * + * @param int $order + * + * @param array $attributes + * * @return $this */ diff --git a/src/MenuItem.php b/src/MenuItem.php index 852b7ab..28ec599 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -150,13 +150,17 @@ public function child($attributes) /** * Register new child menu with dropdown. * - * @param $title_or_properties can give all properties in one argument, or give title separately + * @param $title_or_properties can give all properties in one argument + * or give title as string title separately * * @param callable $callback callback to add submenu items * + * @param int $order + * + * @param array $attributes + * * @return $this */ - public function dropdown($title_or_properties, \Closure $callback, $order = 0, array $attributes = array()) { if (is_array($title_or_properties)) {