1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/form package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Form\Renderer; |
11
|
|
|
|
12
|
|
|
use Slick\Form\ElementInterface; |
13
|
|
|
use Slick\Template\EngineInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AbstractRenderer: base renderer |
17
|
|
|
* |
18
|
|
|
* @package Slick\Form\Renderer |
19
|
|
|
* @author Filipe Silva <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractRenderer |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ElementInterface |
26
|
|
|
*/ |
27
|
|
|
protected $element; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EngineInterface |
31
|
|
|
*/ |
32
|
|
|
protected $engine; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates an abstract render with element dependency. |
36
|
|
|
* |
37
|
|
|
* @param ElementInterface|null $element |
38
|
|
|
*/ |
39
|
20 |
|
public function __construct(ElementInterface $element = null) |
40
|
|
|
{ |
41
|
20 |
|
$this->element = $element; |
42
|
20 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns current element |
46
|
|
|
* |
47
|
|
|
* @return ElementInterface |
48
|
|
|
*/ |
49
|
20 |
|
public function getElement() |
50
|
|
|
{ |
51
|
20 |
|
return $this->element; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets HTML element to render |
56
|
|
|
* |
57
|
|
|
* @param ElementInterface $element |
58
|
|
|
* |
59
|
|
|
* @return $this|self|AbstractRenderer |
60
|
|
|
*/ |
61
|
20 |
|
public function setElement(ElementInterface $element) |
62
|
|
|
{ |
63
|
20 |
|
$this->element = $element; |
64
|
20 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the current template engine |
69
|
|
|
* |
70
|
|
|
* If no template is provided a new one is created with default settings. |
71
|
|
|
* |
72
|
|
|
* @return EngineInterface |
73
|
|
|
*/ |
74
|
20 |
|
public function getEngine() |
75
|
|
|
{ |
76
|
20 |
|
if (null === $this->engine) { |
77
|
20 |
|
$this->setEngine(TemplateFactory::getEngine()); |
78
|
20 |
|
} |
79
|
20 |
|
return $this->engine; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets current template engine |
84
|
|
|
* |
85
|
|
|
* @param EngineInterface $engine |
86
|
|
|
* |
87
|
|
|
* @return $this|self|AbstractRenderer |
88
|
|
|
*/ |
89
|
20 |
|
public function setEngine($engine) |
90
|
|
|
{ |
91
|
20 |
|
$this->engine = $engine; |
92
|
20 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the elements's attributes as a string |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
18 |
View Code Duplication |
public function getAttributes() |
|
|
|
|
101
|
|
|
{ |
102
|
18 |
|
$result = []; |
103
|
18 |
|
foreach ($this->element->getAttributes() as $attribute => $value) { |
104
|
18 |
|
if (null === $value) { |
105
|
6 |
|
$result[] = $attribute; |
106
|
6 |
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
18 |
|
$result[] = "{$attribute}=\"{$value}\""; |
110
|
18 |
|
} |
111
|
18 |
|
return implode(' ', $result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Render the HTML element in the provided context |
116
|
|
|
* |
117
|
|
|
* @param array $context |
118
|
|
|
* |
119
|
|
|
* @return string The HTML string output |
120
|
|
|
*/ |
121
|
20 |
|
public function render($context = []) |
122
|
|
|
{ |
123
|
20 |
|
$this->getElement()->setRendering(true); |
124
|
20 |
|
return ''; |
125
|
|
|
} |
126
|
|
|
} |
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.