1
|
|
|
<?php |
2
|
|
|
namespace Malezha\Menu\Factory; |
3
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Container\Container; |
5
|
|
|
use Malezha\Menu\Contracts\Attributes; |
6
|
|
|
use Malezha\Menu\Element\Link; |
7
|
|
|
use Opis\Closure\SerializableClosure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LinkFactory |
11
|
|
|
* @package Malezha\Menu\Factory |
12
|
|
|
* |
13
|
|
|
* @property string $title |
14
|
|
|
* @property string $url |
15
|
|
|
* @property Attributes $attributes |
16
|
|
|
* @property Attributes $activeAttributes |
17
|
|
|
* @property Attributes $linkAttributes |
18
|
|
|
* @property string $view |
19
|
|
|
* @property mixed $displayRule |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class LinkFactory extends AbstractElementFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
24 |
|
public function __construct(Container $container) |
28
|
|
|
{ |
29
|
24 |
|
parent::__construct($container); |
30
|
|
|
|
31
|
24 |
|
$this->parameters = [ |
32
|
24 |
|
'title' => '', |
33
|
24 |
|
'url' => '#', |
34
|
24 |
|
'attributes' => $this->app->make(Attributes::class, ['attributes' => []]), |
35
|
24 |
|
'activeAttributes' => $this->app->make(Attributes::class, ['attributes' => []]), |
36
|
24 |
|
'linkAttributes' => $this->app->make(Attributes::class, ['attributes' => []]), |
37
|
24 |
|
'view' => $this->getElementConfig(Link::class)['view'], |
38
|
|
|
'displayRule' => true, |
39
|
|
|
]; |
40
|
24 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $parameters |
44
|
|
|
* @return Link |
45
|
|
|
*/ |
46
|
14 |
|
public function build($parameters = []) |
47
|
|
|
{ |
48
|
14 |
|
$link = $this->app->make(Link::class, $this->mergeParameters($parameters)); |
49
|
14 |
|
$this->setDisplayRule($link); |
50
|
|
|
|
51
|
14 |
|
return $link; |
52
|
|
|
} |
53
|
|
|
|
54
|
14 |
|
protected function setDisplayRule(Link $link) |
55
|
|
|
{ |
56
|
14 |
|
if (array_key_exists('displayRule', $this->parameters)) { |
57
|
14 |
|
$link->setDisplayRule($this->parameters['displayRule']); |
58
|
|
|
} |
59
|
14 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
1 |
View Code Duplication |
public function serialize() |
|
|
|
|
65
|
|
|
{ |
66
|
1 |
|
$params = $this->parameters; |
67
|
1 |
|
if ($params['displayRule'] instanceof \Closure) { |
68
|
1 |
|
$params['displayRule'] = new SerializableClosure($params['displayRule']); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return serialize($params); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
1 |
View Code Duplication |
public function unserialize($serialized) |
|
|
|
|
78
|
|
|
{ |
79
|
1 |
|
parent::unserialize($serialized); |
80
|
|
|
|
81
|
1 |
|
if ($this->parameters['displayRule'] instanceof SerializableClosure) { |
82
|
1 |
|
$this->parameters['displayRule'] = $this->parameters['displayRule']->getClosure(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.