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\Text; |
7
|
|
|
use Opis\Closure\SerializableClosure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class TextFactory |
11
|
|
|
* @package Malezha\Menu\Factory |
12
|
|
|
* |
13
|
|
|
* @property string $text |
14
|
|
|
* @property Attributes $attributes |
15
|
|
|
* @property mixed $displayRule |
16
|
|
|
*/ |
17
|
|
|
class TextFactory extends AbstractElementFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param array $parameters |
21
|
|
|
* @return Text |
22
|
|
|
*/ |
23
|
7 |
|
public function build($parameters = []) |
24
|
|
|
{ |
25
|
7 |
|
$text = $this->app->make(Text::class, $this->mergeParameters($parameters)); |
26
|
7 |
|
if (array_key_exists('displayRule', $this->parameters)) { |
27
|
7 |
|
$text->setDisplayRule($this->parameters['displayRule']); |
28
|
|
|
} |
29
|
|
|
|
30
|
7 |
|
return $text; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct(Container $container) |
37
|
|
|
{ |
38
|
10 |
|
parent::__construct($container); |
39
|
|
|
|
40
|
10 |
|
$this->parameters = [ |
41
|
10 |
|
'text' => null, |
42
|
|
|
'displayRule' => true, |
43
|
10 |
|
'attributes' => $this->app->make(Attributes::class, ['attributes' => []]), |
44
|
10 |
|
'view' => $this->getElementConfig(Text::class)['view'], |
45
|
|
|
]; |
46
|
10 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
1 |
View Code Duplication |
public function serialize() |
|
|
|
|
53
|
|
|
{ |
54
|
1 |
|
$params = $this->parameters; |
55
|
1 |
|
if ($params['displayRule'] instanceof \Closure) { |
56
|
|
|
$params['displayRule'] = new SerializableClosure($params['displayRule']); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return serialize($params); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
1 |
View Code Duplication |
public function unserialize($serialized) |
|
|
|
|
66
|
|
|
{ |
67
|
1 |
|
parent::unserialize($serialized); |
68
|
|
|
|
69
|
1 |
|
if ($this->parameters['displayRule'] instanceof SerializableClosure) { |
70
|
|
|
$this->parameters['displayRule'] = $this->parameters['displayRule']->getClosure(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
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.