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

nette / forms / 10256859813

05 Aug 2024 10:20PM UTC coverage: 93.004% (-0.07%) from 93.07%
10256859813

push

github

dg
added HTML attribute data-nette-error

1 of 1 new or added line in 1 file covered. (100.0%)

28 existing lines in 6 files now uncovered.

2087 of 2244 relevant lines covered (93.0%)

0.93 hits per line

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

97.92
/src/Forms/Controls/ChoiceControl.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Nette\Forms\Controls;
11

12
use Nette;
13
use Nette\Utils\Arrays;
14

15

16
/**
17
 * Choice control that allows single item selection.
18
 *
19
 * @property   array $items
20
 * @property-read mixed $selectedItem
21
 */
22
abstract class ChoiceControl extends BaseControl
23
{
24
        /** @var bool[] */
25
        protected array $disabledChoices = [];
26
        private bool $checkDefaultValue = true;
27

28
        /** @var list<array{int|string, string|\Stringable}> */
29
        private array $choices = [];
30

31

32
        public function __construct($label = null, ?array $items = null)
1✔
33
        {
34
                parent::__construct($label);
1✔
35
                if ($items !== null) {
1✔
36
                        $this->setItems($items);
1✔
37
                }
38
        }
1✔
39

40

41
        public function loadHttpData(): void
42
        {
43
                $value = $this->getHttpData(Nette\Forms\Form::DataText);
1✔
44
                $this->value = $value === null ? null : Arrays::toKey($value);
1✔
45
        }
1✔
46

47

48
        /**
49
         * Sets selected item (by key).
50
         * @param  string|int|\BackedEnum|\Stringable|null  $value
51
         * @internal
52
         */
53
        public function setValue($value): static
54
        {
55
                if ($value === null) {
1✔
56
                        $this->value = null;
1✔
57
                        return $this;
1✔
58
                } elseif ($value instanceof \BackedEnum) {
1✔
59
                        $value = $value->value;
1✔
60
                } elseif (!is_string($value) && !is_int($value) && !$value instanceof \Stringable) { // do ChoiceControl
1✔
UNCOV
61
                        throw new Nette\InvalidArgumentException(sprintf('Value must be scalar|enum|Stringable, %s given.', get_debug_type($value)));
×
62
                }
63

64
                $value = Arrays::toKey((string) $value);
1✔
65
                if ($this->checkDefaultValue && !Arrays::some($this->choices, fn($choice) => $choice[0] === $value)) {
1✔
66
                        $set = Nette\Utils\Strings::truncate(implode(', ', array_map(fn($choice) => var_export($choice[0], return: true), $this->choices)), 70, '...');
1✔
67
                        throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->getName()}'.");
1✔
68
                }
69

70
                $this->value = $value;
1✔
71
                return $this;
1✔
72
        }
73

74

75
        /**
76
         * Returns selected key.
77
         * @return string|int|null
78
         */
79
        public function getValue(): mixed
80
        {
81
                return $this->value !== null
1✔
82
                        && !isset($this->disabledChoices[$this->value])
1✔
83
                        && ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
1✔
84
                        ? $res
1✔
85
                        : null;
1✔
86
        }
87

88

89
        /**
90
         * Returns selected key (not checked).
91
         */
92
        public function getRawValue(): string|int|null
93
        {
94
                return $this->value;
1✔
95
        }
96

97

98
        /**
99
         * Is any item selected?
100
         */
101
        public function isFilled(): bool
102
        {
103
                return $this->getValue() !== null;
1✔
104
        }
105

106

107
        /**
108
         * Sets items from which to choose.
109
         */
110
        public function setItems(array $items, bool $useKeys = true): static
1✔
111
        {
112
                $this->choices = [];
1✔
113
                foreach ($items as $k => $v) {
1✔
114
                        $this->choices[] = [$useKeys ? $k : Arrays::toKey((string) $v), $v];
1✔
115
                }
116
                return $this;
1✔
117
        }
118

119

120
        /**
121
         * Returns items from which to choose.
122
         */
123
        public function getItems(): array
124
        {
125
                return array_column($this->choices, 1, 0);
1✔
126
        }
127

128

129
        /**
130
         * Returns selected value.
131
         */
132
        public function getSelectedItem(): mixed
133
        {
134
                return $this->value !== null
1✔
135
                        && !isset($this->disabledChoices[$this->value])
1✔
136
                        && ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
1✔
137
                        ? $res
1✔
138
                        : null;
1✔
139
        }
140

141

142
        /**
143
         * Disables or enables control or items.
144
         */
145
        public function setDisabled(bool|array $value = true): static
1✔
146
        {
147
                if (!is_array($value)) {
1✔
148
                        $this->disabledChoices = [];
1✔
149
                        return parent::setDisabled($value);
1✔
150
                }
151
                $this->disabledChoices = array_fill_keys($value, value: true);
1✔
152
                return parent::setDisabled(false);
1✔
153
        }
154

155

156
        public function checkDefaultValue(bool $value = true): static
1✔
157
        {
158
                $this->checkDefaultValue = $value;
1✔
159
                return $this;
1✔
160
        }
161
}
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

© 2025 Coveralls, Inc