1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Widget.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:Widgets! |
9
|
|
|
* @subpackage Widgets |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 15.09.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Widgets\Widgets; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\Application; |
21
|
|
|
use Nette\ComponentModel; |
22
|
|
|
use Nette\Localization; |
23
|
|
|
use Nette\Utils; |
24
|
|
|
|
25
|
|
|
use IPub; |
26
|
|
|
use IPub\Widgets; |
27
|
|
|
use IPub\Widgets\Decorators; |
28
|
|
|
use IPub\Widgets\Entities; |
29
|
|
|
use IPub\Widgets\Exceptions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Widgets control definition |
33
|
|
|
* |
34
|
|
|
* @package iPublikuj:Widgets! |
35
|
|
|
* @subpackage Widgets |
36
|
|
|
* |
37
|
|
|
* @author Adam Kadlec <[email protected]> |
38
|
|
|
* |
39
|
|
|
* @property Application\UI\ITemplate $template |
40
|
|
|
*/ |
41
|
|
|
abstract class Widget extends Widgets\Application\UI\BaseControl implements IWidget |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var Entities\IData |
45
|
|
|
*/ |
46
|
|
|
protected $data; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Entities\IData $data |
50
|
|
|
* @param ComponentModel\IContainer $parent |
51
|
|
|
* @param string $name |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Entities\IData $data, |
55
|
|
|
ComponentModel\IContainer $parent = NULL, |
56
|
|
|
string $name = NULL |
57
|
|
|
) { |
58
|
|
|
parent::__construct($parent, $name); |
59
|
|
|
|
60
|
|
|
$this->data = $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getTitle() |
67
|
|
|
{ |
68
|
|
|
$title = $this->data->getTitle(); |
69
|
|
|
|
70
|
|
|
return $title ? $title : ''; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getDescription() |
77
|
|
|
{ |
78
|
|
|
return $this->data->getDescription(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getPriority() |
85
|
|
|
{ |
86
|
|
|
return $this->data->getPriority(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function getStatus() |
93
|
|
|
{ |
94
|
|
|
return $this->data->getStatus(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getPosition() |
101
|
|
|
{ |
102
|
|
|
return $this->data->getPosition(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Before render actions |
107
|
|
|
*/ |
108
|
|
|
protected function beforeRender() |
109
|
|
|
{ |
110
|
|
|
// Check if control has template |
111
|
|
|
if (!$this->template instanceof Nette\Bridges\ApplicationLatte\Template) { |
112
|
|
|
throw new Exceptions\InvalidStateException('Widgets container control is without template.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Get widget title |
116
|
|
|
$name = $this->getTitle(); |
117
|
|
|
|
118
|
|
|
// If widget name has space... |
119
|
|
|
$pos = mb_strpos($name, ' '); |
120
|
|
|
if ($pos !== FALSE && mb_strpos($name, '||') === FALSE) { |
121
|
|
|
$name = Utils\Html::el('span') |
122
|
|
|
->addAttributes(['class' => 'color']) |
123
|
|
|
->setText(mb_substr($name, 0, $pos)) |
124
|
|
|
->render(); |
125
|
|
|
|
126
|
|
|
// Modify widget name |
127
|
|
|
$name = $name . mb_substr($name, $pos); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// If widget name has subtitle... |
131
|
|
|
$pos = mb_strpos($name, '||'); |
132
|
|
|
if ($pos !== FALSE) { |
133
|
|
|
$title = Utils\Html::el('span') |
134
|
|
|
->addAttributes(['class' => 'title']) |
135
|
|
|
->setText(mb_substr($name, 0, $pos)) |
136
|
|
|
->render(); |
137
|
|
|
|
138
|
|
|
$subtitle = Utils\Html::el('span') |
139
|
|
|
->addAttributes(['class' => 'subtitle']) |
140
|
|
|
->setText(mb_substr($name, $pos + 2)) |
141
|
|
|
->render(); |
142
|
|
|
|
143
|
|
|
// Split name to title & subtitle |
144
|
|
|
$name = $title . $subtitle; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Set badge if exists |
148
|
|
View Code Duplication |
if ($badge = $this->data->getBadge()) { |
|
|
|
|
149
|
|
|
$badge = Utils\Html::el('span') |
150
|
|
|
->addAttributes(['class' => 'badge badge-' . $badge]) |
151
|
|
|
->render(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Set icon if exists |
155
|
|
View Code Duplication |
if ($icon = $this->data->getIcon()) { |
|
|
|
|
156
|
|
|
$icon = Utils\Html::el('span') |
157
|
|
|
->addAttributes(['class' => 'icon icon-' . $icon]) |
158
|
|
|
->render(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Assign basic widget data to template |
162
|
|
|
$this->template->add('badge', $badge); |
163
|
|
|
$this->template->add('icon', $icon); |
164
|
|
|
$this->template->add('title', [ |
165
|
|
|
'text' => $name, |
166
|
|
|
'insert' => $this->data->getParam('widget.title.insert', TRUE), |
|
|
|
|
167
|
|
|
'hidden' => $this->data->getParam('widget.title.hidden', FALSE) |
|
|
|
|
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
// Check if translator is available |
171
|
|
|
if ($this->getTranslator() instanceof Localization\ITranslator) { |
172
|
|
|
$this->template->setTranslator($this->getTranslator()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// If template was not defined before... |
176
|
|
|
if ($this->template->getFile() === NULL) { |
177
|
|
|
// Get component actual dir |
178
|
|
|
$dir = dirname($this->getReflection()->getFileName()); |
179
|
|
|
|
180
|
|
|
// ...try to get base component template file |
181
|
|
|
$templateFile = $this->templateFile !== NULL && is_file($this->templateFile) ? $this->templateFile : $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte'; |
182
|
|
|
$this->template->setFile($templateFile); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Render widget |
188
|
|
|
*/ |
189
|
|
|
public function render() |
190
|
|
|
{ |
191
|
|
|
$this->beforeRender(); |
192
|
|
|
|
193
|
|
|
// Render component template |
194
|
|
|
$this->template->render(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Convert widget name to string representation |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function __toString() |
203
|
|
|
{ |
204
|
|
|
$class = explode('\\', get_class($this)); |
205
|
|
|
|
206
|
|
|
return end($class); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.