|
1
|
|
|
<?php |
|
2
|
|
|
namespace Malezha\Menu\Element; |
|
3
|
|
|
|
|
4
|
|
|
use Malezha\Menu\Contracts\Attributes; |
|
5
|
|
|
use Malezha\Menu\Contracts\DisplayRule as DisplayRuleInterface; |
|
6
|
|
|
use Malezha\Menu\Contracts\HasAttributes as HasAttributesInterface; |
|
7
|
|
|
use Malezha\Menu\Contracts\MenuRender; |
|
8
|
|
|
use Malezha\Menu\Traits\DisplayRule; |
|
9
|
|
|
use Malezha\Menu\Traits\HasAttributes; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Text |
|
13
|
|
|
* @package Malezha\Menu\Element |
|
14
|
|
|
* |
|
15
|
|
|
* @property string $text |
|
16
|
|
|
* @property bool|callable $displayRule |
|
17
|
|
|
* @property-read Attributes $attributes |
|
18
|
|
|
*/ |
|
19
|
|
|
class Text extends AbstractElement implements DisplayRuleInterface, HasAttributesInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use DisplayRule, HasAttributes; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $text; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Text constructor. |
|
30
|
|
|
* @param $text |
|
31
|
|
|
* @param Attributes $attributes |
|
32
|
|
|
* @param $view |
|
33
|
|
|
* @param MenuRender $render |
|
34
|
|
|
*/ |
|
35
|
7 |
|
public function __construct($text, Attributes $attributes, $view, MenuRender $render) |
|
36
|
|
|
{ |
|
37
|
7 |
|
$this->text = $text; |
|
38
|
7 |
|
$this->render = $render; |
|
39
|
7 |
|
$this->view = $view; |
|
40
|
7 |
|
$this->attributes = $attributes; |
|
41
|
7 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function render($view = null) |
|
47
|
|
|
{ |
|
48
|
3 |
|
return $this->render->make($this->view) |
|
49
|
3 |
|
->with($this->renderWith()) |
|
50
|
3 |
|
->render(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
3 |
|
protected function renderWith() |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
3 |
|
'text' => $this->text, |
|
60
|
3 |
|
'canDisplay' => $this->canDisplay(), |
|
61
|
3 |
|
'attributes' => $this->buildAttributes(), |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
/** |
|
66
|
|
|
* @inheritDoc |
|
67
|
1 |
|
*/ |
|
68
|
1 |
|
public function toArray() |
|
69
|
1 |
|
{ |
|
70
|
1 |
|
return array_merge(parent::toArray(), [ |
|
|
|
|
|
|
71
|
|
|
'text' => $this->text, |
|
72
|
|
|
'attributes' => $this->attributes->toArray(), |
|
73
|
|
|
'displayRule' => $this->canDisplay(), |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
} |
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.