Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ToolJsonLogic | |
0.00% |
0 / 37 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
startup | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
validateImpl | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
executeImpl | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models\Domain\Page\Tool; |
6 | |
7 | use Exception; |
8 | use PeServer\App\Models\Domain\Page\PageLogicBase; |
9 | use PeServer\Core\Mvc\LogicCallMode; |
10 | use PeServer\Core\Mvc\LogicParameter; |
11 | |
12 | class ToolJsonLogic extends PageLogicBase |
13 | { |
14 | public function __construct(LogicParameter $parameter) |
15 | { |
16 | parent::__construct($parameter); |
17 | } |
18 | |
19 | #region PageLogicBase |
20 | |
21 | protected function startup(LogicCallMode $callMode): void |
22 | { |
23 | $this->registerParameterKeys([ |
24 | 'tool_json_input', |
25 | 'tool_json_kind', |
26 | 'tool_json_result', |
27 | 'has_result', |
28 | ], true); |
29 | |
30 | $this->setValue('has_result', false); |
31 | } |
32 | |
33 | protected function validateImpl(LogicCallMode $callMode): void |
34 | { |
35 | if ($callMode === LogicCallMode::Initialize) { |
36 | return; |
37 | } |
38 | |
39 | $this->validation('tool_json_input', function (string $key, string $value) { |
40 | $this->validator->isNotEmpty($key, $value); |
41 | }); |
42 | |
43 | $this->validation('tool_json_kind', function (string $key, string $value) { |
44 | $this->validator->isNotEmpty($key, $value); |
45 | $this->validator->isMatch($key, '/format|minify/', $value); |
46 | }); |
47 | } |
48 | |
49 | protected function executeImpl(LogicCallMode $callMode): void |
50 | { |
51 | if ($callMode === LogicCallMode::Initialize) { |
52 | $this->setValue('tool_json_kind', 'encode'); |
53 | return; |
54 | } |
55 | |
56 | $input = $this->getRequest('tool_json_input'); |
57 | $kind = $this->getRequest('tool_json_kind'); |
58 | $result = ''; |
59 | |
60 | $json = null; |
61 | try { |
62 | $json = json_decode($input, true, 1024, JSON_THROW_ON_ERROR); |
63 | } catch (Exception $ex) { |
64 | $this->addError('tool_json_input', strval(($ex))); |
65 | return; |
66 | } |
67 | |
68 | switch ($kind) { |
69 | case 'format': |
70 | $result = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
71 | break; |
72 | |
73 | case 'minify': |
74 | $result = json_encode($json); |
75 | break; |
76 | |
77 | default: |
78 | throw new Exception('kind'); |
79 | } |
80 | |
81 | $this->setValue('has_result', true); |
82 | $this->setValue('tool_json_result', $result); |
83 | } |
84 | |
85 | #endregion |
86 | } |