Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.59% covered (success)
94.59%
35 / 37
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ShowErrorMessagesFunction
94.59% covered (success)
94.59%
35 / 37
66.67% covered (warning)
66.67%
2 / 3
15.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctionName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 functionBodyImpl
94.29% covered (success)
94.29%
33 / 35
0.00% covered (danger)
0.00%
0 / 1
13.03
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Mvc\Template\Plugin;
6
7use PeServer\Core\I18n;
8use PeServer\Core\Collection\Arr;
9use PeServer\Core\Text;
10use PeServer\Core\Mvc\Validator;
11use PeServer\Core\Html\HtmlDocument;
12use PeServer\Core\Mvc\Template\Plugin\TemplateFunctionBase;
13use PeServer\Core\Mvc\Template\Plugin\TemplatePluginArgument;
14
15/**
16 * エラーメッセージ表示。
17 *
18 * $params
19 *  * key: 対象キー。
20 */
21class ShowErrorMessagesFunction extends TemplateFunctionBase
22{
23    public function __construct(TemplatePluginArgument $argument)
24    {
25        parent::__construct($argument);
26    }
27
28    #region TemplateFunctionBase
29
30    public function getFunctionName(): string
31    {
32        return 'show_error_messages';
33    }
34
35    protected function functionBodyImpl(): string
36    {
37        if (!$this->existsError()) {
38            return Text::EMPTY;
39        }
40
41        $errors = $this->getErrors();
42
43        if (Arr::isNullOrEmpty($errors)) {
44            return Text::EMPTY;
45        }
46
47        $targetKey = Validator::COMMON;
48        $classes = ['errors'];
49
50        if (!isset($this->params['key']) || $this->params['key'] === Validator::COMMON) {
51            $classes[] = 'common-error';
52        } else {
53            $classes[] = 'value-error';
54            $targetKey = $this->params['key'];
55        }
56
57        if ($targetKey !== Validator::COMMON) {
58            if (!isset($errors[$targetKey])) {
59                return Text::EMPTY;
60            }
61            if (Arr::isNullOrEmpty($errors[$targetKey])) {
62                return Text::EMPTY;
63            }
64        }
65
66        $dom = new HtmlDocument();
67
68        $ulElement = $dom->createTagElement('ul');
69        $ulElement->setClassList($classes);
70
71        foreach ($errors as $key => $values) {
72            if ($targetKey !== $key) {
73                continue;
74            }
75
76            foreach ($values as $value) {
77                $liElement = $ulElement->addTagElement('li');
78                $liElement->addClass('error');
79
80                $messageElement = $liElement->addText($value);
81            }
82        }
83
84        if ($targetKey === Validator::COMMON) {
85            $commonElement = $dom->addTagElement('div');
86            $commonElement->setClassList(['common', 'error']);
87
88            $messageElement = $commonElement->addTagElement('p');
89            $messageElement->addText(I18n::message(I18n::COMMON_ERROR_TITLE));
90
91            if ($ulElement->raw->childElementCount) {
92                $commonElement->appendChild($ulElement);
93            }
94        } else {
95            $dom->appendChild($ulElement);
96        }
97
98        return $dom->build();
99    }
100
101    #endregion
102}