diff --git a/src/Contracts/ItemRenderer.php b/src/Contracts/ItemRenderer.php index 1e0226dcd058e9932eb4d3405028808101f0e561..be0e484d4a6e4efb052dab7fca230f500907ef4f 100644 --- a/src/Contracts/ItemRenderer.php +++ b/src/Contracts/ItemRenderer.php @@ -1,4 +1,6 @@ menu = $menu; - $this->name = $name; - $this->title = $title; + $this->menu = $menu; + $this->name = $name; + $this->title = $title; $this->attributes = Arr::except($options, $this->reserved); - $this->parent = $this->resolveParent(Arr::get($options, 'parent', null)); - $this->renderer = Arr::get($options, 'renderer', null); + $this->parent = $this->resolveParent(Arr::get($options, 'parent', null)); + $this->renderer = Arr::get($options, 'renderer', null); - $path = Arr::only($options, array('url', 'route', 'action')); + $path = Arr::only($options, ['url', 'route', 'action']); if (!empty($path)) { $this->link = new Link($path, $this->menu->config->activeClass); } @@ -78,6 +80,26 @@ class Item $this->checkActivation(); } + /** + * Search in meta data if a property doesn't exist otherwise return the property + * + * @param string + * + * @return string + */ + public function __get($prop) + { + if (property_exists($this, $prop)) { + return $this->$prop; + } + + if ($this->children()->has($prop)) { + return $this->children()->get($prop); + } + + return $this->data($prop); + } + /** * Creates a sub Item * @@ -89,7 +111,7 @@ class Item */ public function addSubItem($name, $title, $options = []) { - $options = is_array($options) ? $options : ['url' => $options]; + $options = is_array($options) ? $options : ['url' => $options]; $options['parent'] = $this; return $this->menu->addItem($name, $title, $options); @@ -239,7 +261,7 @@ class Item */ public function hasParent() { - return (bool)$this->parent; + return (bool) $this->parent; } /** @@ -247,7 +269,7 @@ class Item */ public function activate() { - if ($this->menu->config->activeElement == 'item') { + if ('item' == $this->menu->config->activeElement) { $this->setToActive(); } else { if ($this->link) { @@ -330,7 +352,7 @@ class Item */ public function hasLink() { - return (bool)$this->link; + return (bool) $this->link; } /** @@ -362,26 +384,6 @@ class Item $this->hasData($property); } - /** - * Search in meta data if a property doesn't exist otherwise return the property - * - * @param string - * - * @return string - */ - public function __get($prop) - { - if (property_exists($this, $prop)) { - return $this->$prop; - } - - if ($this->children()->has($prop)) { - return $this->children()->get($prop); - } - - return $this->data($prop); - } - /** * Activate the item if it's enabled in menu config and item's url matches the request URI */ @@ -441,7 +443,7 @@ class Item throw new MenuItemNotFoundException( sprintf( 'Item named `%s` could not be found in the `%s` menu', - (string)$parent, + (string) $parent, $this->menu->name ) ); diff --git a/src/ItemCollection.php b/src/ItemCollection.php index 8c4c3b6c405055fd8723275bb3c8dff904dedb6d..bb53897feeacd35ed3b380f59f323cea1095ef10 100644 --- a/src/ItemCollection.php +++ b/src/ItemCollection.php @@ -1,4 +1,6 @@ filterByProperty($attribute, $value); + } /** * Alias to addItem. Needed for Laravel 5.8 compatibility * @see https://github.com/artkonekt/menu/issues/3 @@ -183,28 +206,6 @@ class ItemCollection extends Collection }); } - /** - * Search the items based on an attribute - * - * @param string $method - * @param array $args - * - * @return \Konekt\Menu\ItemCollection - */ - public function __call($method, $args) - { - preg_match('/^[W|w]here([a-zA-Z0-9_]+)$/', $method, $matches); - - if (!$matches) { - trigger_error('Call to undefined method ' . __CLASS__ . '::' . $method . '()', E_USER_ERROR); - } - - $attribute = strtolower($matches[1]); - $value = $args ? $args[0] : null; - - return $this->filterByProperty($attribute, $value); - } - /** * @param $property * @param $value diff --git a/src/Link.php b/src/Link.php index a422fae7b5ae9b153359e148a7eda25ad03a5f27..b0533a687ecac4531dea2df29030ad80a4a34c87 100644 --- a/src/Link.php +++ b/src/Link.php @@ -1,4 +1,6 @@ path = $path; + $this->path = $path; $this->activeClass = $activeClass; } + /** + * Check for a method of the same name if the attribute doesn't exist. + * + * @param string $property + * + * @return mixed + */ + public function __get($property) + { + return $this->attr($property); + } + + public function __set($property, $value) + { + return $this->attr($property, $value); + } + /** * Make the anchor active * @@ -49,7 +68,7 @@ class Link */ public function activate() { - $this->isActive = true; + $this->isActive = true; $this->attributes['class'] = Utils::addHtmlClass( Arr::get($this->attributes, 'class', ''), $this->activeClass @@ -92,23 +111,6 @@ class Link return null; } - /** - * Check for a method of the same name if the attribute doesn't exist. - * - * @param string $property - * - * @return mixed - */ - public function __get($property) - { - return $this->attr($property); - } - - public function __set($property, $value) - { - return $this->attr($property, $value); - } - /** * Get the action for "url" option. * @@ -118,7 +120,7 @@ class Link { $url = $this->path['url']; - $uri = is_array($url) ? $url[0] : $url; + $uri = is_array($url) ? $url[0] : $url; $params = is_array($url) ? array_slice($url, 1) : null; if (Utils::isAbsoluteUrl($uri)) { diff --git a/src/Menu.php b/src/Menu.php index ac88f2f541e3213e848a1535751ae483917d0377..764dfe9188cce3d086b867c06adf60a304a081b6 100644 --- a/src/Menu.php +++ b/src/Menu.php @@ -1,4 +1,6 @@ name = $name; + $this->name = $name; $this->config = $config; - $this->items = new ItemCollection(); + $this->items = new ItemCollection(); + } + + /** + * Returns menu item by name + * + * @return \Konekt\Menu\Item + */ + public function __get($prop) + { + return $this->items->get($prop); } /** @@ -65,7 +77,7 @@ class Menu public function addItem($name, $title, $options = []) { $options = is_string($options) ? ['url' => $options] : $options; - $item = new Item($this, $name, $title, $options); + $item = new Item($this, $name, $title, $options); $this->items->addItem($item); return $item; @@ -107,14 +119,4 @@ class Menu { $this->items = new ItemCollection(); } - - /** - * Returns menu item by name - * - * @return \Konekt\Menu\Item - */ - public function __get($prop) - { - return $this->items->get($prop); - } } diff --git a/src/MenuConfiguration.php b/src/MenuConfiguration.php index 67fa88510be6fe730c2c4591180a91a6e78e2648..0038c1ccb480515cec08361b6f2e1e24d9235945 100644 --- a/src/MenuConfiguration.php +++ b/src/MenuConfiguration.php @@ -1,4 +1,6 @@ autoActivate = Arr::get($options, 'auto_activate', true); + $this->autoActivate = Arr::get($options, 'auto_activate', true); $this->activateParents = Arr::get($options, 'activate_parents', true); - $this->activeClass = Arr::get($options, 'active_class', 'active'); - $this->activeElement = strtolower(Arr::get($options, 'active_element', 'item')); - $this->cascadeData = Arr::get($options, 'cascade_data', true); + $this->activeClass = Arr::get($options, 'active_class', 'active'); + $this->activeElement = strtolower(Arr::get($options, 'active_element', 'item')); + $this->cascadeData = Arr::get($options, 'cascade_data', true); if (!in_array($this->activeElement, self::ACTIVE_ELEMENT_TYPES)) { throw new InvalidMenuConfigurationException( diff --git a/src/MenuFactory.php b/src/MenuFactory.php index a64a03a19dbdbe3ccd11d8de755c6a7447403a66..a21172be516a5b04c9ddad0b4cfac8db77a72a9b 100644 --- a/src/MenuFactory.php +++ b/src/MenuFactory.php @@ -1,4 +1,6 @@ name, $menu); } } else { - View::share((string)$options['share'], $menu); + View::share((string) $options['share'], $menu); } } diff --git a/src/MenuServiceProvider.php b/src/MenuServiceProvider.php index 25c8842aa72b1735e2d70d8722fb2453fb5685ef..bb77416f0e10d187dff8a2303a526f32db6be511 100644 --- a/src/MenuServiceProvider.php +++ b/src/MenuServiceProvider.php @@ -1,4 +1,6 @@ $value) { $element = is_numeric($key) ? - (string)$value : - (is_null($value) ? (string)$key : $key . '="' . e($value) . '"'); + (string) $value : + (is_null($value) ? (string) $key : $key . '="' . e($value) . '"'); if (!empty($element)) { $attrs[] = $element; } diff --git a/tests/Dummies/User.php b/tests/Dummies/User.php index 227b98b7fca394636d4847890cc04f71c90e6fdb..a1a95df71b17fcf00b569059575c7ae7ec6bf855 100644 --- a/tests/Dummies/User.php +++ b/tests/Dummies/User.php @@ -1,4 +1,6 @@ setUpDatabase(); + $this->setUpAuth(); + + $this->menu = MenuFactory::create('nav', []); + + $this->createUsers(); + } + /** * @test */ @@ -80,7 +94,7 @@ class AuthorizationTest extends TestCase $this->assertTrue($item->isAllowed()); - $this->be(($this->anotherUser)); + $this->be($this->anotherUser); $this->assertFalse($item->isAllowed()); } @@ -138,29 +152,17 @@ class AuthorizationTest extends TestCase $this->assertFalse($item->isAllowed($this->user)); } - protected function setUp(): void - { - parent::setUp(); - - $this->setUpDatabase(); - $this->setUpAuth(); - - $this->menu = MenuFactory::create('nav', []); - - $this->createUsers(); - } - protected function createUsers() { $this->user = User::create([ - 'email' => 'giovanni@gatto.it', - 'name' => 'Giovanni Gatto', + 'email' => 'giovanni@gatto.it', + 'name' => 'Giovanni Gatto', 'password' => bcrypt('Putin') ])->fresh(); $this->anotherUser = User::create([ - 'email' => 'frederico@latte.it', - 'name' => 'Frederico Latte', + 'email' => 'frederico@latte.it', + 'name' => 'Frederico Latte', 'password' => bcrypt('Erdogan') ]); } @@ -176,9 +178,9 @@ class AuthorizationTest extends TestCase { $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); } } diff --git a/tests/Feature/CustomRendererTest.php b/tests/Feature/CustomRendererTest.php index fdc3a9b8c3431f7d108f31dbba31bb2ad06d4b29..b0473266320f9b938cf17501824520301ad0fcd3 100644 --- a/tests/Feature/CustomRendererTest.php +++ b/tests/Feature/CustomRendererTest.php @@ -1,4 +1,6 @@ 'link', - 'active_class' => 'is-active' + 'active_class' => 'is-active' ]); $menu->addItem('dashboard', 'Dashboard', '/dashboard'); diff --git a/tests/Feature/FacadeTest.php b/tests/Feature/FacadeTest.php index 01829c47660d65df0e7219d337a56cae6d39bcb7..b8458fb70614ba680db67198dd4b6cc431ce01ce 100644 --- a/tests/Feature/FacadeTest.php +++ b/tests/Feature/FacadeTest.php @@ -1,4 +1,6 @@ \n"; foreach ($items as $item) { diff --git a/tests/TestCase.php b/tests/TestCase.php index d36c744152ef887c0a0f6a7ad3a95862cdc86ca6..233414b342129f45ec5d0577bce5b17ab26be5a1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,4 +1,6 @@ setupRoutes(); + } /** @test */ public function has_active_child_returns_false_when_there_are_no_active_items_at_all() { @@ -113,13 +121,6 @@ class ActiveItemsCollectionTest extends TestCase $this->assertTrue($parent1->hasActiveChild()); } - protected function setUp(): void - { - parent::setUp(); - - $this->setupRoutes(); - } - private function setupRoutes() { \Route::get('/articles/{slug}', function ($slug) { diff --git a/tests/Unit/ItemCollectionTest.php b/tests/Unit/ItemCollectionTest.php index 655124898a7656b0fb54339016f18c88a53464a4..46d89c2f522602cb1de45d89352f856b4797d705 100644 --- a/tests/Unit/ItemCollectionTest.php +++ b/tests/Unit/ItemCollectionTest.php @@ -1,4 +1,6 @@ menu = MenuFactory::create('sidebar', ['auto_activate' => false]); + $this->menu->addItem('home', 'Home', ['url' => '/', 'category' => 'internal']); + + $this->menu->addItem('about', 'About', ['url' => '/about', 'category' => 'internal']); + $this->menu->about->addSubItem('about-us', 'About Us', ['url' => '/about/us', 'category' => 'internal']); + $this->menu->about->addSubItem('about-our-product', 'About Our Product', ['url' => '/about/our-product', 'category' => 'internal']); + + $this->menu->addItem('contact', 'Contact', ['url' => '/contact', 'category' => 'internal']); + $this->menu->addItem('google', 'Google', 'https://google.com')->data('engine', 'google'); + } + public function testMagicWhereMethod() { $this->assertEquals(5, $this->menu->items->whereCategory('internal')->count()); @@ -100,19 +117,4 @@ class ItemCollectionTest extends TestCase $this->assertEquals($originalCount - 4, $this->menu->items->count()); } - - protected function setUp(): void - { - parent::setUp(); - - $this->menu = MenuFactory::create('sidebar', ['auto_activate' => false]); - $this->menu->addItem('home', 'Home', ['url' => '/', 'category' => 'internal']); - - $this->menu->addItem('about', 'About', ['url' => '/about', 'category' => 'internal']); - $this->menu->about->addSubItem('about-us', 'About Us', ['url' => '/about/us', 'category' => 'internal']); - $this->menu->about->addSubItem('about-our-product', 'About Our Product', ['url' => '/about/our-product', 'category' => 'internal']); - - $this->menu->addItem('contact', 'Contact', ['url' => '/contact', 'category' => 'internal']); - $this->menu->addItem('google', 'Google', 'https://google.com')->data('engine', 'google'); - } } diff --git a/tests/Unit/ItemTest.php b/tests/Unit/ItemTest.php index 668b6c01da91421aa50869d547495d342975b821..af03d0779821e67338e7c608cceb1f8a1cc0219a 100644 --- a/tests/Unit/ItemTest.php +++ b/tests/Unit/ItemTest.php @@ -1,4 +1,6 @@ about, $menu->addItem('our-goals', 'Our Goals', [ 'parent' => 'about', - 'url' => '/our-goals' + 'url' => '/our-goals' ])->parent ); } @@ -136,16 +149,4 @@ class ItemTest extends TestCase return $result; } - - protected function setUp(): void - { - parent::setUp(); - \Route::get('/articles/{slug}', function ($slug) { - return 'Hello, ' . $slug; - }); - - \Route::get('/about', function () { - return 'About Us'; - }); - } } diff --git a/tests/Unit/LinkTest.php b/tests/Unit/LinkTest.php index a0157fdb36f764392c15c694cdd72eda295cfd2a..183f2d3fd57e79a8dcc84b93b1f32a5dfd7f1eee 100644 --- a/tests/Unit/LinkTest.php +++ b/tests/Unit/LinkTest.php @@ -1,4 +1,6 @@ false, + 'auto_activate' => false, 'activate_parents' => false, - 'cascade_data' => false, - 'active_element' => 'link', - 'active_class' => 'is-active' + 'cascade_data' => false, + 'active_element' => 'link', + 'active_class' => 'is-active' ]); $this->assertFalse($config->autoActivate); diff --git a/tests/Unit/RepositoryTest.php b/tests/Unit/RepositoryTest.php index fa4ddb127f394841ad04eed143c46b018df5fc15..1b4da7275a2ed2d0b67f0c411656e6e7782dd455 100644 --- a/tests/Unit/RepositoryTest.php +++ b/tests/Unit/RepositoryTest.php @@ -1,4 +1,6 @@