Skip to content

Commit

Permalink
Added 2 methods: findBy and whereTitle to MenuBuilder class
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitano committed Jun 11, 2015
1 parent 8deb3db commit 0f51bf8
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/Pingpong/Menus/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MenuBuilder implements Countable

/**
* Determine whether the ordering feature is enabled or not.
*
*
* @var boolean
*/
protected $ordering = false;
Expand All @@ -75,6 +75,38 @@ public function __construct($menu, Repository $config)
$this->config = $config;
}

/**
* Find menu item by given its title.
*
* @param string $title
* @param callable|null $callback
* @return mixed
*/
public function whereTitle($title, callable $callback = null)
{
$item = $this->findBy('title', $title);

if (is_callable($callback)) {
return call_user_func($callback, $item);
}

return $item;
}

/**
* Find menu item by given key and value.
*
* @param string $key
* @param string $value
* @return \Pingpong\Menus\MenuItem
*/
public function findBy($key, $value)
{
return collect($this->items)->filter(function ($item) use ($key, $value) {
return $item->{$key} == $value;
})->first();
}

/**
* Set view factory instance.
*
Expand Down Expand Up @@ -441,7 +473,7 @@ public function toArray()

/**
* Enable menu ordering.
*
*
* @return self
*/
public function enableOrdering()
Expand All @@ -453,7 +485,7 @@ public function enableOrdering()

/**
* Disable menu ordering.
*
*
* @return self
*/
public function disableOrdering()
Expand Down

0 comments on commit 0f51bf8

Please sign in to comment.