diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 282131b..d44a72f 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -22,7 +22,7 @@ checks: tools: external_code_coverage: timeout: 600 - runs: 3 + runs: 2 php_code_sniffer: enabled: true config: diff --git a/.travis.yml b/.travis.yml index 96b482d..03a87c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: php sudo: false php: - - 5.6 - 7.0 - 7.1 - nightly @@ -13,7 +12,7 @@ matrix: - php: nightly env: - - TESTBENCH_VERSION=3.4.* + - TESTBENCH_VERSION=3.5.* before_script: - travis_retry composer self-update diff --git a/composer.json b/composer.json index f172dc5..f8c74d6 100644 --- a/composer.json +++ b/composer.json @@ -14,13 +14,14 @@ "type": "library", "license": "MIT", "require": { - "php": ">=5.6.4", + "php": ">=7.0", "arcanedev/support": "~4.0", "arcanesoft/contracts": "~2.0" }, "require-dev": { - "phpunit/phpcov": "~3.0", - "phpunit/phpunit": "~5.0" + "phpunit/phpunit": "~6.0", + "phpunit/phpcov": "~4.0", + "symfony/dom-crawler": "~3.3" }, "autoload": { "psr-4": { @@ -35,10 +36,5 @@ }, "scripts": { "testbench": "composer require --dev \"orchestra/testbench=~3.0\"" - }, - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } } } diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 100% rename from phpunit.xml rename to phpunit.xml.dist diff --git a/src/Entities/Item.php b/src/Entities/Item.php index e85cd87..36a9610 100644 --- a/src/Entities/Item.php +++ b/src/Entities/Item.php @@ -2,9 +2,8 @@ use Arcanesoft\Contracts\Auth\Models\User; use Illuminate\Contracts\Support\Arrayable; -use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Arr; -use JsonSerializable; +use Illuminate\Support\Fluent; /** * Class Item @@ -12,69 +11,8 @@ * @package Arcanesoft\Sidebar\Entitites * @author ARCANEDEV */ -class Item implements Arrayable, Jsonable, JsonSerializable +class Item extends Fluent { - /* ----------------------------------------------------------------- - | Properties - | ----------------------------------------------------------------- - */ - - /** - * The item name. - * - * @var string - */ - protected $name; - - /** - * The item title. - * - * @var string - */ - protected $title; - - /** - * The item url. - * - * @var string - */ - protected $url; - - /** - * The item icon. - * - * @var string - */ - protected $icon; - - /** - * The item active state. - * - * @var bool - */ - protected $active = false; - - /** - * The item roles. - * - * @var array - */ - protected $roles = []; - - /** - * The item permissions. - * - * @var array - */ - protected $permissions = []; - - /** - * The item children (sub-items). - * - * @var \Arcanesoft\Sidebar\Entities\ItemCollection - */ - protected $children; - /* ----------------------------------------------------------------- | Constructor | ----------------------------------------------------------------- @@ -83,19 +21,19 @@ class Item implements Arrayable, Jsonable, JsonSerializable /** * Item constructor. * - * @param string $name - * @param string $title - * @param string $url - * @param string|null $icon + * @param array $attributes */ - public function __construct($name, $title, $url, $icon = null) + public function __construct(array $attributes = []) { - $this->name = $name; - $this->title = $title; - $this->url = $url; - $this->icon = $icon; - $this->active = false; - $this->children = new ItemCollection; + $keys = ['name', 'title', 'url', 'icon']; + + parent::__construct(Arr::only($attributes, $keys) + [ + 'extra' => Arr::except($attributes, $keys) + ]); + + $this->attributes['active'] = false; + $this->attributes['children'] = new ItemCollection; + $this->setRoles([])->setPermissions([]); } /* ----------------------------------------------------------------- @@ -110,7 +48,7 @@ public function __construct($name, $title, $url, $icon = null) */ public function name() { - return $this->name; + return $this->get('name'); } /** @@ -123,7 +61,7 @@ public function title() /** @var \Illuminate\Translation\Translator $trans */ $trans = trans(); - return $trans->has($this->title) ? $trans->get($this->title) : $this->title; + return $trans->has($title = $this->get('title')) ? $trans->get($title) : $title; } /** @@ -133,7 +71,7 @@ public function title() */ public function url() { - return $this->url; + return $this->get('url'); } /** @@ -143,7 +81,7 @@ public function url() */ public function icon() { - return $this->icon; + return $this->get('icon'); } /** @@ -155,8 +93,8 @@ public function icon() */ public function setCurrent($name) { - $this->children->setCurrent($name); - $this->active = ($this->name === $name || $this->children->hasActiveItem()); + $this->attributes['children']->setCurrent($name); + $this->attributes['active'] = ($this->name() === $name || $this->children()->hasActiveItem()); return $this; } @@ -168,7 +106,7 @@ public function setCurrent($name) */ public function getRoles() { - return $this->roles; + return $this->get('roles', []); } /** @@ -180,7 +118,7 @@ public function getRoles() */ public function setRoles(array $roles) { - $this->roles = $roles; + $this->attributes['roles'] = $roles; return $this; } @@ -192,7 +130,7 @@ public function setRoles(array $roles) */ public function getPermissions() { - return $this->permissions; + return $this->get('permissions', []); } /** @@ -204,7 +142,7 @@ public function getPermissions() */ public function setPermissions(array $permissions) { - $this->permissions = $permissions; + $this->attributes['permissions'] = $permissions; return $this; } @@ -216,7 +154,7 @@ public function setPermissions(array $permissions) */ public function children() { - return $this->children; + return $this->get('children', new ItemCollection); } /** @@ -259,7 +197,7 @@ public function childrenClass($class = 'treeview') */ public static function make($name, $title, $url, $icon = null) { - return new self($name, $title, $url, $icon); + return new self(compact('name', 'title', 'url', 'icon')); } /** @@ -323,7 +261,7 @@ public function addChild(array $child) $item = self::makeFromArray($child); if ($item->allowed()) - $this->children->push($item); + $this->attributes['children']->push($item); return $this; } @@ -340,7 +278,7 @@ public function addChild(array $child) */ public function isActive() { - return $this->active; + return (bool) $this->get('active', false); } /** @@ -350,7 +288,7 @@ public function isActive() */ public function hasChildren() { - return ! $this->children->isEmpty(); + return ! $this->children()->isEmpty(); } /** @@ -379,7 +317,7 @@ public function allowed() */ public function hasRoles() { - return ! empty($this->roles); + return ! empty($this->getRoles()); } /** @@ -389,7 +327,7 @@ public function hasRoles() */ public function hasPermissions() { - return ! empty($this->permissions); + return ! empty($this->getPermissions()); } /* ----------------------------------------------------------------- @@ -398,44 +336,15 @@ public function hasPermissions() */ /** - * Get the instance as an array. + * Convert the instance to an array. * * @return array */ public function toArray() { - return [ - 'name' => $this->name(), - 'title' => $this->title(), - 'url' => $this->url(), - 'icon' => $this->icon(), - 'active' => $this->isActive(), - 'roles' => $this->roles, - 'permissions' => $this->permissions, - 'children' => $this->children->toArray(), - ]; - } - - /** - * Convert the object to its JSON representation. - * - * @param int $options - * - * @return string - */ - public function toJson($options = 0) - { - return json_encode($this->jsonSerialize(), $options); - } - - /** - * Convert the object into something JSON serializable. - * - * @return array - */ - public function jsonSerialize() - { - return $this->toArray(); + return array_map(function ($value) { + return $value instanceof Arrayable ? $value->toArray() : $value; + }, $this->attributes); } /** diff --git a/src/SidebarServiceProvider.php b/src/SidebarServiceProvider.php index eb6cf38..3221321 100644 --- a/src/SidebarServiceProvider.php +++ b/src/SidebarServiceProvider.php @@ -50,6 +50,8 @@ public function register() public function boot() { parent::boot(); + + // } /** diff --git a/tests/Entities/ItemCollectionTest.php b/tests/Entities/ItemCollectionTest.php index 1b12cf0..25a255b 100644 --- a/tests/Entities/ItemCollectionTest.php +++ b/tests/Entities/ItemCollectionTest.php @@ -51,20 +51,22 @@ public function it_can_convert_to_array_and_to_json() 'title' => 'Home', 'url' => '/', 'icon' => 'home-icon', + 'extra' => [], 'active' => false, + 'children' => [], 'roles' => [], 'permissions' => [], - 'children' => [], ], [ 'name' => 'contact', 'title' => 'CONTACT', 'url' => '/contact', 'icon' => 'contact-icon', + 'extra' => [], 'active' => false, + 'children' => [], 'roles' => [], 'permissions' => [], - 'children' => [] ], ]; diff --git a/tests/Entities/ItemTest.php b/tests/Entities/ItemTest.php index 0b9202d..5dfe020 100644 --- a/tests/Entities/ItemTest.php +++ b/tests/Entities/ItemTest.php @@ -92,10 +92,11 @@ public function it_can_convert_to_array() 'title' => 'Home', 'url' => 'http://localhost', 'icon' => 'fa fa-fw fa-home', + 'extra' => [], 'active' => false, + 'children' => [], 'roles' => [], 'permissions' => [], - 'children' => [], ]; $this->assertSame($expected, $item->toArray()); @@ -227,7 +228,7 @@ public function it_can_check_if_user_is_allowed_to_access_this_item() */ private function createItem($name, $title, $url, array $roles = [], array $permissions = []) { - return tap(new Item($name, $title, $url, $icon = 'fa fa-fw fa-home'), function (Item $item) use ($roles, $permissions) { + return tap(Item::make($name, $title, $url, $icon = 'fa fa-fw fa-home'), function (Item $item) use ($roles, $permissions) { $item->setRoles($roles); $item->setPermissions($permissions); }); diff --git a/tests/ManagerTest.php b/tests/ManagerTest.php index 556dd30..aca28f5 100644 --- a/tests/ManagerTest.php +++ b/tests/ManagerTest.php @@ -101,10 +101,11 @@ public function it_can_add_item_with_route() 'title' => 'SEO', 'url' => 'http://localhost/seo', 'icon' => 'seo-icon', + 'extra' => [], 'active' => false, + 'children' => [], 'roles' => [], 'permissions' => [], - 'children' => [], ], ];