These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Control.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:ConfirmationDialog! |
||
9 | * @subpackage Components |
||
10 | * @since 1.0.0 |
||
11 | * |
||
12 | * @date 12.03.14 |
||
13 | */ |
||
14 | |||
15 | declare(strict_types = 1); |
||
16 | |||
17 | namespace IPub\ConfirmationDialog\Components; |
||
18 | |||
19 | use Nette\Application; |
||
20 | use Nette\Bridges; |
||
21 | use Nette\Localization; |
||
22 | use Nette\Utils; |
||
23 | |||
24 | use IPub\ConfirmationDialog\Exceptions; |
||
25 | |||
26 | /** |
||
27 | * Confirmation dialog control |
||
28 | * |
||
29 | * @package iPublikuj:ConfirmationDialog! |
||
30 | * @subpackage Components |
||
31 | * |
||
32 | * @author Adam Kadlec <[email protected]> |
||
33 | */ |
||
34 | 1 | final class Control extends BaseControl |
|
35 | { |
||
36 | /** |
||
37 | * @var IConfirmer |
||
38 | */ |
||
39 | private $confirmerFactory; |
||
40 | |||
41 | /** |
||
42 | * @var Confirmer |
||
43 | */ |
||
44 | private $confirmer; |
||
45 | |||
46 | /** |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $useAjax = TRUE; |
||
50 | |||
51 | /** |
||
52 | * @param string|NULL $layoutFile |
||
53 | * @param string|NULL $templateFile |
||
54 | * @param IConfirmer $confirmerFactory |
||
55 | */ |
||
56 | public function __construct( |
||
57 | string $layoutFile = NULL, |
||
58 | string $templateFile = NULL, |
||
59 | IConfirmer $confirmerFactory |
||
60 | ) { |
||
61 | 1 | list(, , , $parent, $name) = func_get_args() + [NULL, NULL, NULL, NULL, NULL]; |
|
62 | |||
63 | 1 | parent::__construct($parent, $name); |
|
64 | |||
65 | 1 | if ($layoutFile !== NULL) { |
|
66 | $this->setLayoutFile($layoutFile); |
||
67 | } |
||
68 | |||
69 | 1 | if ($templateFile !== NULL) { |
|
70 | $this->setTemplateFile($templateFile); |
||
71 | } |
||
72 | |||
73 | // Get confirmer component factory |
||
74 | 1 | $this->confirmerFactory = $confirmerFactory; |
|
75 | 1 | } |
|
76 | |||
77 | /** |
||
78 | * Change default dialog layout path |
||
79 | * |
||
80 | * @param string $layoutFile |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | public function setLayoutFile(string $layoutFile) : void |
||
85 | { |
||
86 | $this->setTemplateFilePath($layoutFile, self::TEMPLATE_LAYOUT); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Change default confirmer template path |
||
91 | * |
||
92 | * @param string $layoutFile |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | public function setTemplateFile(string $layoutFile) : void |
||
97 | { |
||
98 | 1 | $this->setTemplateFilePath($layoutFile, self::TEMPLATE_CONFIRMER); |
|
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getTemplateFile() : string |
||
105 | { |
||
106 | // ...try to get default component layout file |
||
107 | 1 | return $this->templateFile !== NULL ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte'; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Overrides signal method formatter |
||
112 | * This provide "dynamically named signals" |
||
113 | * |
||
114 | * @param string $signal |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public static function formatSignalMethod($signal) : string |
||
119 | { |
||
120 | 1 | if (Utils\Strings::startsWith($signal, 'confirm')) { |
|
121 | 1 | return 'handleShowConfirmer'; |
|
122 | } |
||
123 | |||
124 | return parent::formatSignalMethod($signal); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Add confirmation handler to "dynamicaly named signals" |
||
129 | * |
||
130 | * @param string $nam Confirmation/signal name |
||
131 | * @param callback|Utils\Callback $handler Callback called when confirmation succeed |
||
132 | * @param callback|string $question Callback ($confirmer, $params) or string containing question text |
||
133 | * @param callback|string $heading Callback ($confirmer, $params) or string containing heading text |
||
134 | * |
||
135 | * @return void |
||
136 | * |
||
137 | * @throws Exceptions\InvalidArgumentException |
||
138 | */ |
||
139 | public function addConfirmer(string $name, $handler, $question, $heading) : void |
||
140 | { |
||
141 | // Confirmer name could be only A-z |
||
142 | 1 | if (!preg_match('/[A-Za-z_]+/', $name)) { |
|
143 | throw new Exceptions\InvalidArgumentException('Confirmation control name contain invalid characters.'); |
||
144 | } |
||
145 | |||
146 | 1 | $confirmer = $this->getConfirmerControl($name); |
|
147 | |||
148 | // Check confirmer |
||
149 | 1 | if ($confirmer->isConfigured()) { |
|
150 | throw new Exceptions\InvalidArgumentException(sprintf('Confirmation control "%s" could not be created.', $name)); |
||
151 | } |
||
152 | |||
153 | // Set confirmer handler |
||
154 | 1 | $confirmer->setHandler($handler); |
|
155 | // Set confirmer heading |
||
156 | 1 | $confirmer->setHeading($heading); |
|
157 | // Set confirmer question |
||
158 | 1 | $confirmer->setQuestion($question); |
|
159 | 1 | } |
|
160 | |||
161 | /** |
||
162 | * @param string $name |
||
163 | * |
||
164 | * @return Confirmer |
||
165 | * |
||
166 | * @throws Exceptions\InvalidArgumentException |
||
167 | */ |
||
168 | public function getConfirmer(string $name) : Confirmer |
||
169 | { |
||
170 | $confirmer = $this->getConfirmerControl($name); |
||
171 | |||
172 | // Check confirmer |
||
173 | if (!$confirmer->isConfigured()) { |
||
174 | throw new Exceptions\InvalidArgumentException(sprintf('Confirmation control "%s" does not exists.', $name)); |
||
175 | } |
||
176 | |||
177 | return $confirmer; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return void |
||
182 | */ |
||
183 | public function resetConfirmer() : void |
||
184 | { |
||
185 | 1 | $this->confirmer = NULL; |
|
186 | |||
187 | // Invalidate dialog snippets |
||
188 | 1 | $this->redrawControl(); |
|
189 | 1 | } |
|
190 | |||
191 | /** |
||
192 | * @return Application\UI\Multiplier |
||
193 | * |
||
194 | * @throws Exceptions\InvalidArgumentException |
||
195 | */ |
||
196 | protected function createComponentConfirmer() : Application\UI\Multiplier |
||
197 | { |
||
198 | 1 | return new Application\UI\Multiplier((function () : Confirmer { |
|
199 | // Check if confirmer factory is available |
||
200 | 1 | if (!$this->confirmerFactory) { |
|
201 | throw new Exceptions\InvalidStateException('Confirmation control factory does not exist.'); |
||
202 | } |
||
203 | |||
204 | 1 | $confirmer = $this->confirmerFactory->create($this->templateFile); |
|
205 | |||
206 | 1 | if ($this->useAjax) { |
|
207 | 1 | $confirmer->enableAjax(); |
|
208 | |||
209 | } else { |
||
210 | $confirmer->disableAjax(); |
||
211 | } |
||
212 | |||
213 | 1 | return $confirmer; |
|
214 | 1 | })); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Show dialog for confirmation |
||
219 | * |
||
220 | * @param string $name |
||
221 | * @param array $params |
||
222 | * |
||
223 | * @return void |
||
224 | * |
||
225 | * @throws Exceptions\InvalidArgumentException |
||
226 | * @throws Exceptions\InvalidStateException |
||
227 | */ |
||
228 | public function showConfirm(string $name, array $params = []) : void |
||
229 | { |
||
230 | 1 | if (!is_string($name)) { |
|
231 | throw new Exceptions\InvalidArgumentException('$name must be string.'); |
||
232 | } |
||
233 | |||
234 | 1 | if ((!$this->confirmer = $this['confirmer-' . $name]) || !$this->confirmer->isConfigured()) { |
|
235 | throw new Exceptions\InvalidStateException(sprintf('Confirmer "%s" do not exist.', $name)); |
||
236 | } |
||
237 | |||
238 | // Prepare confirmer for displaying |
||
239 | 1 | $this->confirmer->showConfirm($params); |
|
240 | 1 | } |
|
241 | |||
242 | /** |
||
243 | * Dynamically named signal receiver |
||
244 | * |
||
245 | * @return void |
||
246 | * |
||
247 | * @throws Exceptions\InvalidArgumentException |
||
248 | * @throws Exceptions\InvalidStateException |
||
249 | */ |
||
250 | public function handleShowConfirmer() : void |
||
251 | { |
||
252 | 1 | if (!$this->getPresenter() instanceof Application\UI\Presenter) { |
|
0 ignored issues
–
show
|
|||
253 | throw new Exceptions\InvalidArgumentException('Confirmer is not attached to presenter.'); |
||
254 | } |
||
255 | |||
256 | 1 | list(, $signal) = $this->getPresenter()->getSignal(); |
|
257 | |||
258 | 1 | $name = Utils\Strings::substring($signal, 7); |
|
259 | 1 | $name{0} = strtolower($name{0}); |
|
260 | |||
261 | 1 | if (!$this['confirmer-' . $name]->isConfigured()) { |
|
262 | throw new Exceptions\InvalidArgumentException('Invalid confirmation control.'); |
||
263 | } |
||
264 | |||
265 | 1 | $params = $this->getParameters(); |
|
266 | |||
267 | 1 | $this->showConfirm($name, $params); |
|
268 | 1 | } |
|
269 | |||
270 | /** |
||
271 | * @return void |
||
272 | */ |
||
273 | public function enableAjax() : void |
||
274 | { |
||
275 | $this->useAjax = TRUE; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @return void |
||
280 | */ |
||
281 | public function disableAjax() : void |
||
282 | { |
||
283 | $this->useAjax = FALSE; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Render control |
||
288 | * |
||
289 | * @return void |
||
290 | * |
||
291 | * @throws Exceptions\InvalidStateException |
||
292 | */ |
||
293 | public function render() : void |
||
294 | { |
||
295 | // Create template |
||
296 | 1 | $template = parent::render(); |
|
297 | |||
298 | // Check if control has template |
||
299 | 1 | if ($template instanceof Bridges\ApplicationLatte\Template) { |
|
0 ignored issues
–
show
The class
Nette\Bridges\ApplicationLatte\Template does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed.
Loading history...
|
|||
300 | // Assign vars to template |
||
301 | 1 | $template->confirmer = $this->confirmer; |
|
302 | |||
303 | // If template was not defined before... |
||
304 | 1 | if ($template->getFile() === NULL) { |
|
305 | // ...try to get base component template file |
||
306 | 1 | $layoutFile = $this->layoutFile !== NULL ? $this->layoutFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'layout.latte'; |
|
307 | 1 | $template->setFile($layoutFile); |
|
308 | } |
||
309 | |||
310 | // Render component template |
||
311 | 1 | $template->render(); |
|
312 | |||
313 | } else { |
||
314 | throw new Exceptions\InvalidStateException('Dialog control is without template.'); |
||
315 | } |
||
316 | 1 | } |
|
317 | |||
318 | /** |
||
319 | * @param string $name |
||
320 | * |
||
321 | * @return Confirmer |
||
322 | * |
||
323 | * @throws Exceptions\InvalidArgumentException |
||
324 | */ |
||
325 | private function getConfirmerControl(string $name) : Confirmer |
||
326 | { |
||
327 | 1 | $confirmer = $this->getComponent('confirmer-' . $name); |
|
328 | |||
329 | 1 | if (!$confirmer instanceof Confirmer) { |
|
330 | throw new Exceptions\InvalidArgumentException(sprintf('Confirmation control "%s" does not exists.', $name)); |
||
331 | } |
||
332 | |||
333 | 1 | return $confirmer; |
|
334 | } |
||
335 | } |
||
336 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.