Completed
Push — master ( 55ab89...0c5b4e )
by Song
06:39
created

Menu::roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Auth\Database;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Menu extends Model
8
{
9
    /**
10
     * @var array
11
     */
12
    protected static $branchOrder = [];
13
14
    /**
15
     * The attributes that are mass assignable.
16
     *
17
     * @var array
18
     */
19
    protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri'];
20
21
    /**
22
     * Create a new Eloquent model instance.
23
     *
24
     * @param array $attributes
25
     */
26
    public function __construct(array $attributes = [])
27
    {
28
        $this->table = config('admin.database.menu_table');
29
30
        parent::__construct($attributes);
31
    }
32
33
    /**
34
     * A Menu belongs to many roles.
35
     *
36
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
37
     */
38
    public function roles()
39
    {
40
        $pivotTable = config('admin.database.role_menu_table');
41
42
        return $this->belongsToMany(Role::class, $pivotTable, 'menu_id', 'role_id');
43
    }
44
45
    /**
46
     * Format data to tree like array.
47
     *
48
     * @param array $elements
49
     * @param int   $parentId
50
     *
51
     * @return array
52
     */
53
    public static function toTree(array $elements = [], $parentId = 0)
54
    {
55
        $branch = [];
56
57
        if (empty($elements)) {
58
            $elements = static::with('roles')->orderByRaw('`order` = 0,`order`')->get()->toArray();
59
        }
60
61
        foreach ($elements as $element) {
62
            if ($element['parent_id'] == $parentId) {
63
                $children = static::toTree($elements, $element['id']);
64
65
                if ($children) {
66
                    $element['children'] = $children;
67
                }
68
69
                $branch[] = $element;
70
            }
71
        }
72
73
        return $branch;
74
    }
75
76
    /**
77
     * Set the order of branches in the tree.
78
     *
79
     * @param array $order
80
     *
81
     * @return void
82
     */
83
    protected static function setBranchOrder(array $order)
84
    {
85
        static::$branchOrder = array_flip(array_flatten($order));
86
87
        static::$branchOrder = array_map(function ($item) {
88
            return ++$item;
89
        }, static::$branchOrder);
90
    }
91
92
    /**
93
     * Save a tree from a tree like array.
94
     *
95
     * @param array $tree
96
     * @param int   $parentId
97
     */
98
    public static function saveTree($tree = [], $parentId = 0)
99
    {
100
        if (empty(static::$branchOrder)) {
101
            static::setBranchOrder($tree);
102
        }
103
104
        foreach ($tree as $branch) {
105
            $node = static::find($branch['id']);
106
107
            $node->parent_id = $parentId;
108
            $node->order = static::$branchOrder[$branch['id']];
109
            $node->save();
110
111
            if (isset($branch['children'])) {
112
                static::saveTree($branch['children'], $branch['id']);
113
            }
114
        }
115
    }
116
117
    /**
118
     * Build options of select field in form.
119
     *
120
     * @param array  $elements
121
     * @param int    $parentId
122
     * @param string $prefix
123
     *
124
     * @return array
125
     */
126
    public static function buildSelectOptions(array $elements = [], $parentId = 0, $prefix = '')
127
    {
128
        $prefix = $prefix ?: str_repeat('&nbsp;', 6);
129
130
        $options = [];
131
132
        if (empty($elements)) {
133
            $elements = static::orderByRaw('`order` = 0,`order`')->get(['id', 'parent_id', 'title'])->toArray();
134
        }
135
136
        foreach ($elements as $element) {
137
            $element['title'] = $prefix.'&nbsp;'.$element['title'];
138
            if ($element['parent_id'] == $parentId) {
139
                $children = static::buildSelectOptions($elements, $element['id'], $prefix.$prefix);
140
141
                $options[$element['id']] = $element['title'];
142
143
                if ($children) {
144
                    $options += $children;
145
                }
146
            }
147
        }
148
149
        return $options;
150
    }
151
152
    /**
153
     * Delete current item and its children.
154
     *
155
     * @throws \Exception
156
     *
157
     * @return bool|null
158
     */
159
    public function delete()
160
    {
161
        $this->where('parent_id', $this->id)->delete();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Encore\Admin\Auth\Database\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
The method where does not exist on object<Encore\Admin\Auth\Database\Menu>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
162
163
        return parent::delete();
164
    }
165
}
166