Test Failed
Push — extract-attributes ( 6e6144...c9c1de )
by Dmitriy
07:22 queued 12s
created

ErrorSummary   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 83
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 6 1
A run() 0 26 3
A collectErrors() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Widget\Widget;
11
use function array_values;
12
13
final class ErrorSummary extends Widget
14
{
15
    private FormModelInterface $data;
16
    private array $options = [];
17
18
    /**
19
     * Generates a summary of the validation errors.
20
     *
21
     * @throws \JsonException
22
     *
23
     * @return string the generated error summary
24
     */
25
    public function run(): string
26
    {
27
        $new = clone $this;
28
29
        $header = $new->options['header'] ?? '<p>Please fix the following errors:</p>';
30
        $footer = ArrayHelper::remove($new->options, 'footer', '');
31
        $encode = ArrayHelper::remove($new->options, 'encode', true);
32
        $showAllErrors = ArrayHelper::remove($new->options, 'showAllErrors', false);
33
34
        unset($new->options['header']);
35
36
        $lines = $new->collectErrors($new->data, $encode, $showAllErrors);
37
38
        if (empty($lines)) {
39
            /** still render the placeholder for client-side validation use */
40
            $content = '<ul></ul>';
41
            $new->options['style'] = isset($new->options['style'])
42
                ? rtrim((string)$new->options['style'], ';') . '; display:none' : 'display:none';
43
        } else {
44
            $content = '<ul><li>' . implode("</li>\n<li>", $lines) . '</li></ul>';
45
        }
46
47
        return Html::tag('div', $header . $content . $footer)
48
            ->attributes($new->options)
49
            ->encode(false)
50
            ->render();
51
    }
52
53
    /**
54
     * Set form model, name and options for the widget.
55
     *
56
     * @param FormModelInterface $data Form model.
57
     * @param array $options The HTML attributes for the widget container tag.
58
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
59
     *
60
     * @return self
61
     */
62
    public function config(FormModelInterface $data, array $options = []): self
63
    {
64
        $new = clone $this;
65
        $new->data = $data;
66
        $new->options = $options;
67
        return $new;
68
    }
69
70
    /**
71
     * Return array of the validation errors.
72
     *
73
     * @param bool $encode , if set to false then the error messages won't be encoded.
74
     * @param bool $showAllErrors , if set to true every error message for each attribute will be shown otherwise only
75
     * the first error message for each attribute will be shown.
76
     *
77
     * @return array of the validation errors.
78
     */
79
    private function collectErrors(FormModelInterface $form, bool $encode, bool $showAllErrors): array
80
    {
81
        $lines = $form->getErrorSummary($showAllErrors);
82
83
        /**
84
         * If there are the same error messages for different attributes, array_unique will leave gaps between
85
         * sequential keys. Applying array_values to reorder array keys.
86
         */
87
        $lines = array_values($lines);
88
89
        if ($encode) {
90
            foreach ($lines as &$line) {
91
                $line = Html::encode($line);
92
            }
93
        }
94
95
        return $lines;
96
    }
97
}
98