Completed
Push — master ( 52f128...550102 )
by Travis
02:39
created

DropDown::toJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace JumpGate\Menu;
4
5
use Illuminate\Contracts\Support\Jsonable;
6
use Illuminate\Support\Collection;
7
use JumpGate\Menu\Traits\Activate;
8
use JumpGate\Menu\Traits\Insertable;
9
use JumpGate\Menu\Traits\Linkable;
10
11
/**
12
 * Class DropDown
13
 *
14
 * @package JumpGate\Menu
15
 */
16
class DropDown implements Jsonable
17
{
18
    use Activate, Insertable, Linkable;
19
20
    /**
21
     * @var string
22
     */
23
    public $slug;
24
25
    /**
26
     * @var string|null
27
     */
28
    public $name;
29
30
    /**
31
     * @var bool
32
     */
33
    public $activateWithLinks = true;
34
35
    /**
36
     * Construct a menu
37
     *
38
     * @param $dropDownName The name of the drop down
39
     */
40
    public function __construct($dropDownName = null)
41
    {
42
        $this->links = new Collection();
43
44
        if (isset($dropDownName)) {
45
            $this->name = $dropDownName;
46
        }
47
    }
48
49
    /**
50
     * Check if the current object is a drop down
51
     *
52
     * @return bool
53
     */
54
    public function isDropDown()
55
    {
56
        return true;
57
    }
58
59
    /**
60
     * Check if the dropdown has links
61
     *
62
     * @return bool
63
     */
64
    public function hasLinks()
65
    {
66
        return (count($this->links) > 0);
67
    }
68
69
    /**
70
     * This stops the drop down from becoming active
71
     * because a child link is active.
72
     */
73
    public function disableActiveParentage()
74
    {
75
        $this->activateWithLinks = false;
76
    }
77
78
    /**
79
     * Check if the drop down should become active along
80
     * with it's links.
81
     */
82
    public function activeParentage()
83
    {
84
        return $this->activateWithLinks;
85
    }
86
87
    /**
88
     * Convert dropdown to JSON.
89
     *
90
     * @param int $options
91
     *
92
     * @return string
93
     */
94
    public function toJson($options = 0)
95
    {
96
        $properties = get_object_vars($this);
97
        unset($properties['menu']);
98
99
        if (isset($properties['links'])) {
100
            $properties['links'] = json_decode($properties['links']->toJson());
101
        }
102
103
        return json_encode($properties);
104
    }
105
}
106