Completed
Pull Request — 2.0 (#31)
by Nicolas
02:57
created

Menuitem::setParentIdAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Modules\Menu\Entities;
4
5
use Dimsav\Translatable\Translatable;
6
use Illuminate\Database\Eloquent\Model;
7
use TypiCMS\NestableTrait;
8
9
class Menuitem extends Model
10
{
11
    use Translatable, NestableTrait;
12
13
    public $translatedAttributes = ['title', 'uri', 'url', 'status', 'locale'];
14
    protected $fillable = [
15
        'menu_id',
16
        'page_id',
17
        'parent_id',
18
        'position',
19
        'target',
20
        'module_name',
21
        'title',
22
        'uri',
23
        'url',
24
        'status',
25
        'is_root',
26
        'icon',
27
        'link_type',
28
        'locale',
29
        'class',
30
    ];
31
    protected $table = 'menu__menuitems';
32
33
    /**
34
     * For nested collection
35
     *
36
     * @var array
37
     */
38
    public $children = [];
39
40
    public function menu()
41
    {
42
        return $this->belongsTo(Menu::class);
43
    }
44
45
    /**
46
     * Make the current menu item child of the given root item
47
     * @param Menuitem $rootItem
48
     */
49
    public function makeChildOf(Menuitem $rootItem)
50
    {
51
        $this->parent_id = $rootItem->id;
0 ignored issues
show
Documentation introduced by
The property parent_id does not exist on object<Modules\Menu\Entities\Menuitem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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 introduced by
The property id does not exist on object<Modules\Menu\Entities\Menuitem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
52
        $this->save();
53
    }
54
55
    /**
56
     * Check if the current menu item is the root
57
     * @return bool
58
     */
59
    public function isRoot()
60
    {
61
        return (bool) $this->is_root;
0 ignored issues
show
Documentation introduced by
The property is_root does not exist on object<Modules\Menu\Entities\Menuitem>. 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...
62
    }
63
64
    /**
65
     * Check if page_id is empty and returning null instead empty string
66
     * @return number
67
     */
68
    public function setPageIdAttribute($value)
69
    {
70
        $this->attributes['page_id'] = ! empty($value) ? $value : null;
71
    }
72
73
    /**
74
     * Check if parent_id is empty and returning null instead empty string
75
     * @return number
76
     */
77
    public function setParentIdAttribute($value)
78
    {
79
        $this->attributes['parent_id'] = ! empty($value) ? $value : null;
80
    }
81
}
82