• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / forms / 26455457147

26 May 2026 02:44PM UTC coverage: 93.345%. Remained the same
26455457147

push

github

dg
fixed PHPStan errors

48 of 51 new or added lines in 12 files covered. (94.12%)

34 existing lines in 10 files now uncovered.

2104 of 2254 relevant lines covered (93.35%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.78
/src/Forms/Controls/CheckboxList.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Forms\Controls;
9

10
use Nette;
11
use Nette\Utils\Html;
12
use Stringable;
13
use function array_flip, array_key_first, array_keys, array_merge, explode, func_num_args, in_array, is_array, key, substr;
14

15

16
/**
17
 * Set of checkboxes.
18
 *
19
 * @property-read Html $separatorPrototype
20
 * @property-read Html $containerPrototype
21
 * @property-read Html $itemLabelPrototype
22
 */
23
class CheckboxList extends MultiChoiceControl
24
{
25
        protected Html $separator;
26
        protected Html $container;
27
        protected Html $itemLabel;
28

29

30
        /** @param  ?mixed[]  $items */
31
        public function __construct(string|Stringable|null $label = null, ?array $items = null)
1✔
32
        {
33
                parent::__construct($label, $items);
1✔
34
                $this->control->type = 'checkbox';
1✔
35
                $this->container = Html::el();
1✔
36
                $this->separator = Html::el('br');
1✔
37
                $this->itemLabel = Html::el('label');
1✔
38
                $this->setOption('type', 'checkbox');
1✔
39
        }
1✔
40

41

42
        public function loadHttpData(): void
43
        {
44
                $data = $this->getForm()->getHttpData(Nette\Forms\Form::DataText, substr($this->getHtmlName(), 0, -2));
1✔
45
                $data = match (true) {
1✔
46
                        $data === null => $this->getHttpData(Nette\Forms\Form::DataText),
1✔
47
                        is_string($data) => explode(',', $data),
1✔
NEW
48
                        default => [],
×
49
                };
50
                $this->value = array_keys(array_flip($data));
1✔
51
        }
1✔
52

53

54
        public function getControl(): Html
55
        {
56
                $input = parent::getControl();
1✔
57
                $items = $this->getItems();
1✔
58
                if (!$items) {
1✔
59
                        return Html::el();
1✔
60
                }
61
                return $this->container->setHtml(
1✔
62
                        Nette\Forms\Helpers::createInputList(
1✔
63
                                $this->translate($items),
1✔
64
                                array_merge($input->attrs, [
1✔
65
                                        'id' => null,
1✔
66
                                        'checked?' => $this->value,
1✔
67
                                        'disabled:' => $this->disabled,
1✔
68
                                        'required' => null,
69
                                        'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
1✔
70
                                ]),
71
                                $this->itemLabel->attrs,
1✔
72
                                $this->separator,
1✔
73
                        ),
74
                );
75
        }
76

77

78
        public function getLabel($caption = null): Html
1✔
79
        {
80
                return parent::getLabel($caption)->for(null);
1✔
81
        }
82

83

84
        /**
85
         * Returns the HTML input element for a specific checkbox item by key.
86
         */
87
        public function getControlPart($key = null): Html
88
        {
89
                $key = key([(string) $key => null]);
1✔
90
                return parent::getControl()->addAttributes([
1✔
91
                        'id' => $this->getHtmlId() . '-' . $key,
1✔
92
                        'checked' => in_array($key, (array) $this->value, strict: true),
1✔
93
                        'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
1✔
94
                        'required' => null,
95
                        'value' => $key,
1✔
96
                ]);
97
        }
98

99

100
        /**
101
         * Returns the label element for the whole checkbox list, or the item label for a specific key.
102
         */
103
        public function getLabelPart($key = null): Html
1✔
104
        {
105
                $itemLabel = clone $this->itemLabel;
1✔
106
                return func_num_args()
1✔
107
                        ? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key)
1✔
108
                        : $this->getLabel();
1✔
109
        }
110

111

112
        /**
113
         * Returns separator HTML element template.
114
         */
115
        public function getSeparatorPrototype(): Html
116
        {
117
                return $this->separator;
1✔
118
        }
119

120

121
        /**
122
         * Returns container HTML element template.
123
         */
124
        public function getContainerPrototype(): Html
125
        {
126
                return $this->container;
1✔
127
        }
128

129

130
        /**
131
         * Returns item label HTML element template.
132
         */
133
        public function getItemLabelPrototype(): Html
134
        {
135
                return $this->itemLabel;
1✔
136
        }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc