|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\Admin\Printer; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Admin\Component\TermField\TermFieldInterface; |
|
6
|
|
|
use Leonidas\Contracts\Admin\Printer\TermFieldPrinterInterface; |
|
7
|
|
|
use Leonidas\Contracts\Ui\ViewInterface; |
|
8
|
|
|
use Leonidas\Library\Admin\Component\TermField\View\AddTermFieldView; |
|
9
|
|
|
use Leonidas\Library\Admin\Component\TermField\View\EditTermFieldView; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class BasicTermFieldPrinter implements TermFieldPrinterInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected const ADD_TERM_SCREEN = 'edit-tags'; |
|
15
|
|
|
|
|
16
|
|
|
protected const EDIT_TERM_SCREEN = 'term'; |
|
17
|
|
|
|
|
18
|
|
|
public function print(array $fields, ServerRequestInterface $request): string |
|
19
|
|
|
{ |
|
20
|
|
|
$output = ''; |
|
21
|
|
|
|
|
22
|
|
|
foreach ($fields as $field) { |
|
23
|
|
|
$this->printOne($field, $request); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $output; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function printOne(TermFieldInterface $field, ServerRequestInterface $request): string |
|
30
|
|
|
{ |
|
31
|
|
|
if (!$field->shouldBeRendered($request)) { |
|
32
|
|
|
return ''; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $this->defineView($request)->render([ |
|
36
|
|
|
'label' => $field->getLabel(), |
|
37
|
|
|
'description' => $field->getDescription(), |
|
38
|
|
|
'field' => $field->renderInputField($request), |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function defineView(ServerRequestInterface $request): ViewInterface |
|
43
|
|
|
{ |
|
44
|
|
|
switch ($request->getAttribute('context')) { |
|
45
|
|
|
case static::ADD_TERM_SCREEN: |
|
46
|
|
|
$view = new AddTermFieldView(); |
|
47
|
|
|
|
|
48
|
|
|
break; |
|
49
|
|
|
|
|
50
|
|
|
case static::EDIT_TERM_SCREEN: |
|
51
|
|
|
$view = new EditTermFieldView(); |
|
52
|
|
|
|
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $view; // @phpstan-ignore-line |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|