|
1
|
|
|
<?php |
|
2
|
|
|
namespace Malezha\Menu\Element; |
|
3
|
|
|
|
|
4
|
|
|
use Malezha\Menu\Contracts\Attributes; |
|
5
|
|
|
use Malezha\Menu\Contracts\Builder; |
|
6
|
|
|
use Malezha\Menu\Contracts\HasBuilder; |
|
7
|
|
|
use Malezha\Menu\Contracts\MenuRender; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SubMenu |
|
11
|
|
|
* @package Malezha\Menu\Element |
|
12
|
|
|
* |
|
13
|
|
|
* @property-read Attributes $activeAttributes |
|
14
|
|
|
* @property-read Builder $builder |
|
15
|
|
|
* @inheritdoc |
|
16
|
|
|
*/ |
|
17
|
|
|
class SubMenu extends Link implements HasBuilder |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Builder |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $builder; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* SubMenu constructor. |
|
26
|
|
|
* @param string $title |
|
27
|
|
|
* @param string $url |
|
28
|
|
|
* @param Attributes $attributes |
|
29
|
|
|
* @param Attributes $activeAttributes |
|
30
|
|
|
* @param Attributes $linkAttributes |
|
31
|
|
|
* @param string $view |
|
32
|
|
|
* @param string $currentUrl |
|
33
|
|
|
* @param MenuRender $render |
|
34
|
|
|
* @param Builder $builder |
|
35
|
|
|
*/ |
|
36
|
7 |
|
public function __construct($title, $url, Attributes $attributes, Attributes $activeAttributes, |
|
37
|
|
|
Attributes $linkAttributes, $view, $currentUrl, MenuRender $render, Builder $builder) |
|
38
|
|
|
{ |
|
39
|
7 |
|
parent::__construct($title, $url, $attributes, $activeAttributes, $linkAttributes, $view, $currentUrl, $render); |
|
40
|
|
|
|
|
41
|
7 |
|
$this->builder = $builder; |
|
42
|
7 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritDoc |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getBuilder() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->builder; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function renderWith() |
|
56
|
|
|
{ |
|
57
|
4 |
|
$renderView = func_get_arg(0); |
|
58
|
|
|
|
|
59
|
4 |
|
return array_merge(parent::renderWith(), [ |
|
60
|
4 |
|
'builder' => $this->builder, |
|
61
|
4 |
|
'renderView' => $renderView, |
|
62
|
|
|
]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function render($view = null) |
|
69
|
|
|
{ |
|
70
|
4 |
|
return $this->render->make($this->view) |
|
71
|
4 |
|
->with($this->renderWith($view)) |
|
72
|
4 |
|
->render(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritDoc |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function toArray() |
|
79
|
|
|
{ |
|
80
|
2 |
|
return array_merge(parent::toArray(), [ |
|
81
|
2 |
|
'builder' => $this->builder->toArray(), |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
protected function propertiesForSerialization() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return array_merge(parent::propertiesForSerialization(), [ |
|
|
|
|
|
|
88
|
1 |
|
'builder' => $this->builder, |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.