1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form\Elements\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Form\Decorator\DecoratorFactory; |
6
|
|
|
use Nip\Form\Decorator\Elements\AbstractDecorator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait HasDecoratorsTrait |
10
|
|
|
* @package Nip\Form\Elements\Traits |
11
|
|
|
*/ |
12
|
|
|
trait HasDecoratorsTrait |
13
|
|
|
{ |
14
|
|
|
protected $decorators; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $type |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
1 |
|
public function newDecorator($type = '') |
21
|
|
|
{ |
22
|
1 |
|
return DecoratorFactory::createForElement($this, $type); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $type |
27
|
|
|
* @param string $position |
28
|
|
|
* @param bool|string $name |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
1 |
|
public function addDecorator($type = '', $position = 'element', $name = false) |
32
|
|
|
{ |
33
|
1 |
|
$decorator = $this->newDecorator($type); |
34
|
1 |
|
$this->attachDecorator($decorator, $position, $name); |
|
|
|
|
35
|
1 |
|
return $decorator; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param AbstractDecorator $decorator |
40
|
|
|
* @param string $position |
41
|
|
|
* @param bool $name |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
1 |
|
public function attachDecorator( |
45
|
|
|
AbstractDecorator $decorator, |
46
|
|
|
$position = 'element', |
47
|
|
|
$name = false |
48
|
|
|
) { |
49
|
1 |
|
$decorator->setElement($this); |
50
|
1 |
|
$name = $name ? $name : $decorator->getName(); |
51
|
1 |
|
$this->decorators[$position][$name] = $decorator; |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param boolean $position |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
8 |
|
public function getDecoratorsByPosition($position) |
61
|
|
|
{ |
62
|
8 |
|
return isset($this->decorators[$position]) ? $this->decorators[$position] : []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $name |
67
|
|
|
* @param bool $position |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
1 |
|
public function getDecorator($name, $position = false) |
71
|
|
|
{ |
72
|
1 |
|
if ($position) { |
73
|
|
|
return $this->decorators[$position][$name]; |
74
|
|
|
} else { |
75
|
1 |
|
foreach ($this->decorators as $position => $decorators) { |
76
|
1 |
|
if (isset($decorators[$name])) { |
77
|
1 |
|
return $decorators[$name]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $name |
87
|
|
|
* @param bool $position |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function removeDecorator($name, $position = false) |
91
|
|
|
{ |
92
|
|
|
if ($position) { |
93
|
|
|
unset($this->decorators[$position][$name]); |
94
|
|
|
} else { |
95
|
|
|
foreach ($this->decorators as $position => $decorators) { |
96
|
|
|
if (isset($decorators[$name])) { |
97
|
|
|
unset($decorators[$name]); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|