diff --git a/src/MenuBuilder.php b/src/MenuBuilder.php index f1adb80..3bd85bd 100644 --- a/src/MenuBuilder.php +++ b/src/MenuBuilder.php @@ -339,23 +339,46 @@ 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 as string title separately + * + * @param callable $callback callback to add submenu items + * + * @param int $order + * + * @param array $attributes * * @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) { - $arguments = func_get_args(); + if (($title_or_properties['attributes']??null) === null) + { + $title_or_properties['attributes'] = []; + } + + $properties = $title_or_properties; + } + else + { + $title = $title_or_properties; - $title = Arr::get($arguments, 0); - $attributes = Arr::get($arguments, 2); + // 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 00c34ee..28ec599 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -150,22 +150,43 @@ 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 as string title separately + * + * @param callable $callback callback to add submenu items + * + * @param int $order + * + * @param array $attributes * * @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) { - $arguments = func_get_args(); + if (($title_or_properties['attributes']??null) === null) + { + $title_or_properties['attributes'] = []; + } + + $properties = $title_or_properties; + } + else + { + $title = $title_or_properties; - $title = Arr::get($arguments, 0); - $attributes = Arr::get($arguments, 2); + // 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);