Skip to content

Commit

Permalink
Merge pull request #1 from ARCANESOFT/develop
Browse files Browse the repository at this point in the history
Updating the package for Laravel 5.5
  • Loading branch information
arcanedev-maroc authored Sep 1, 2017
2 parents a9c471a + 8f94cb9 commit 9322d8b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ checks:
tools:
external_code_coverage:
timeout: 600
runs: 3
runs: 2
php_code_sniffer:
enabled: true
config:
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: php
sudo: false

php:
- 5.6
- 7.0
- 7.1
- nightly
Expand All @@ -13,7 +12,7 @@ matrix:
- php: nightly

env:
- TESTBENCH_VERSION=3.4.*
- TESTBENCH_VERSION=3.5.*

before_script:
- travis_retry composer self-update
Expand Down
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -35,10 +36,5 @@
},
"scripts": {
"testbench": "composer require --dev \"orchestra/testbench=~3.0\""
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
File renamed without changes.
159 changes: 34 additions & 125 deletions src/Entities/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,17 @@

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
*
* @package Arcanesoft\Sidebar\Entitites
* @author ARCANEDEV <[email protected]>
*/
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
| -----------------------------------------------------------------
Expand All @@ -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([]);
}

/* -----------------------------------------------------------------
Expand All @@ -110,7 +48,7 @@ public function __construct($name, $title, $url, $icon = null)
*/
public function name()
{
return $this->name;
return $this->get('name');
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -133,7 +71,7 @@ public function title()
*/
public function url()
{
return $this->url;
return $this->get('url');
}

/**
Expand All @@ -143,7 +81,7 @@ public function url()
*/
public function icon()
{
return $this->icon;
return $this->get('icon');
}

/**
Expand All @@ -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;
}
Expand All @@ -168,7 +106,7 @@ public function setCurrent($name)
*/
public function getRoles()
{
return $this->roles;
return $this->get('roles', []);
}

/**
Expand All @@ -180,7 +118,7 @@ public function getRoles()
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
$this->attributes['roles'] = $roles;

return $this;
}
Expand All @@ -192,7 +130,7 @@ public function setRoles(array $roles)
*/
public function getPermissions()
{
return $this->permissions;
return $this->get('permissions', []);
}

/**
Expand All @@ -204,7 +142,7 @@ public function getPermissions()
*/
public function setPermissions(array $permissions)
{
$this->permissions = $permissions;
$this->attributes['permissions'] = $permissions;

return $this;
}
Expand All @@ -216,7 +154,7 @@ public function setPermissions(array $permissions)
*/
public function children()
{
return $this->children;
return $this->get('children', new ItemCollection);
}

/**
Expand Down Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -340,7 +278,7 @@ public function addChild(array $child)
*/
public function isActive()
{
return $this->active;
return (bool) $this->get('active', false);
}

/**
Expand All @@ -350,7 +288,7 @@ public function isActive()
*/
public function hasChildren()
{
return ! $this->children->isEmpty();
return ! $this->children()->isEmpty();
}

/**
Expand Down Expand Up @@ -379,7 +317,7 @@ public function allowed()
*/
public function hasRoles()
{
return ! empty($this->roles);
return ! empty($this->getRoles());
}

/**
Expand All @@ -389,7 +327,7 @@ public function hasRoles()
*/
public function hasPermissions()
{
return ! empty($this->permissions);
return ! empty($this->getPermissions());
}

/* -----------------------------------------------------------------
Expand All @@ -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);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/SidebarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function register()
public function boot()
{
parent::boot();

//
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/Entities/ItemCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => []
],
];

Expand Down
Loading

0 comments on commit 9322d8b

Please sign in to comment.