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

contributte / forms-bootstrap / #54

11 Feb 2024 08:44PM UTC coverage: 83.494% (+0.1%) from 83.353%
#54

Pull #88

github

dakorpar
fix: compatibility with nette/component-model 3.1
Pull Request #88: fix: compatibility with nette/component-model 3.1

693 of 830 relevant lines covered (83.49%)

5.72 hits per line

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

90.91
/src/Grid/BootstrapRow.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\FormsBootstrap\Grid;
4

5
use Contributte\FormsBootstrap\BootstrapRenderer;
6
use Contributte\FormsBootstrap\Enums\RendererConfig;
7
use Contributte\FormsBootstrap\Enums\RendererOptions;
8
use Contributte\FormsBootstrap\Traits\FakeControlTrait;
9
use Nette\ComponentModel\IComponent;
10
use Nette\ComponentModel\IContainer;
11
use Nette\Forms\Container;
12
use Nette\Forms\Control;
13
use Nette\InvalidArgumentException;
14
use Nette\SmartObject;
15
use Nette\Utils\Html;
16

17
/**
18
 * Class BootstrapRow.
19
 * Represents a row in Bootstrap grid system.
20
 *
21
 * @property string               $gridBreakPoint   Bootstrap breakpoint - usually xs, sm, md, lg. sm by
22
 *           default. Use NULL for no breakpoint.
23
 * @property-read string[]        $ownedNames       list of names of components which were added to this row
24
 * @property-read BootstrapCell[] $cells            cells in this row
25
 * @property-read Html            $elementPrototype the Html div that will be rendered. You may define
26
 *                additional properties.
27
 * @property-read string          $name             name of component
28
 */
29
class BootstrapRow implements IComponent, Control
30
{
31

32
        use SmartObject;
33
        use FakeControlTrait;
34

35
        /**
36
         * Number of columns in Bootstrap grid. Default is 12, but it can be customized.
37
         *
38
         * @var int
39
         */
40
        public $numOfColumns = 12;
41

42
        /**
43
         * Global name counter
44
         *
45
         * @var int
46
         */
47
        private static $uidCounter = 0;
48

49
        /** @var string $name */
50
        private $name;
51

52
        /**
53
         * Number of columns used by added cells.
54
         *
55
         * @var int
56
         */
57
        private $columnsOccupied = 0;
58

59
        /**
60
         * Form or container this belong to
61
         *
62
         * @var Container
63
         */
64
        private $container;
65

66
        /** @var string */
67
        private $gridBreakPoint = 'sm';
68

69
        /** @var string[] */
70
        private $ownedNames = [];
71

72
        /** @var BootstrapCell[] */
73
        private $cells = [];
74

75
        /** @var Html */
76
        private $elementPrototype;
77

78
        /** @var mixed[] */
79
        private $options = [];
80

81
        /**
82
         * @param Container $container Form or container this belongs to. Components will be added to this
83
         * @param string|null      $name      Optional name of this row. If none is supplied, it is generated
84
         *                             automatically.
85
         */
86
        public function __construct(Container $container, $name = null)
87
        {
88
                $this->container = $container;
6✔
89
                if (!$name) {
6✔
90
                        $name = 'bootstrap_row_' . ++self::$uidCounter;
5✔
91
                }
92

93
                $this->name = $name;
6✔
94

95
                $this->elementPrototype = Html::el();
6✔
96
        }
97

98
        /**
99
         * Adds a new cell to which a control can be added.
100
         */
101
        public function addCell(?int $numOfColumns = BootstrapCell::COLUMNS_NONE): BootstrapCell
102
        {
103
                if ($this->columnsOccupied + ($numOfColumns ?? 0) > $this->numOfColumns) {
6✔
104
                        throw new InvalidArgumentException(
1✔
105
                                'the given number of columns with combination of already used'
1✔
106
                                . ' columns exceeds column limit (' . $this->numOfColumns . ')'
1✔
107
                        );
1✔
108
                }
109

110
                $cell = new BootstrapCell($this, $numOfColumns);
6✔
111
                $this->cells[] = $cell;
6✔
112

113
                return $cell;
6✔
114
        }
115

116
        /**
117
         * Delegate to underlying container and remember it.
118
         *
119
         * @internal
120
         */
121
        public function addComponent(IComponent $component, ?string $name = null, ?string $insertBefore = null): void
122
        {
123
                $this->container->addComponent($component, $name, $insertBefore);
3✔
124
                $this->ownedNames[] = $name;
3✔
125
        }
126

127
        /**
128
         * @return BootstrapCell[]
129
         * @see BootstrapRow::$cells
130
         */
131
        public function getCells(): array
132
        {
133
                return $this->cells;
×
134
        }
135

136
        /**
137
         * The container without content
138
         *
139
         * @see BootstrapRow::$elementPrototype
140
         */
141
        public function getElementPrototype(): Html
142
        {
143
                return $this->elementPrototype;
1✔
144
        }
145

146
        /**
147
         * @see BootstrapRow::$gridBreakPoint
148
         */
149
        public function getGridBreakPoint(): string
150
        {
151
                return $this->gridBreakPoint;
4✔
152
        }
153

154
        /**
155
         * Sets the xs, sm, md, lg part.
156
         *
157
         * @see BootstrapRow::$gridBreakPoint
158
         * @param string $gridBreakPoint . NULL for no breakpoint.
159
         */
160
        public function setGridBreakPoint(string $gridBreakPoint): BootstrapRow
161
        {
162
                $this->gridBreakPoint = $gridBreakPoint;
×
163

164
                return $this;
×
165
        }
166

167
        /**
168
         * Component name
169
         */
170
        public function getName(): ?string
171
        {
172
                return $this->name;
6✔
173
        }
174

175
        /**
176
         * Returns the container
177
         *
178
         * @return Container
179
         */
180
        public function getParent(): IContainer
181
        {
182
                return $this->container;
4✔
183
        }
184

185
        /**
186
         * Sets the container
187
         *
188
         * @param Container|NULL $parent
189
         * @param null $name ignored
190
         */
191
        public function setParent(?IContainer $parent = null, ?string $name = null): static
192
        {
193
                $this->container = $parent;
6✔
194

195
                return $this;
6✔
196
        }
197

198
        /**
199
         * Gets previously set option
200
         *
201
         * @param mixed|null $default
202
         * @return mixed|null
203
         */
204
        public function getOption(string $option, $default = null)
205
        {
206
                return $this->options[$option] ?? $default;
3✔
207
        }
208

209
        /**
210
         * Renders the row into a Html object
211
         */
212
        public function render(): Html
213
        {
214
                /** @var BootstrapRenderer $renderer */
215
                $renderer = $this->container->form->renderer;
3✔
216

217
                $element = $renderer->configElem(RendererConfig::GRID_ROW, $this->elementPrototype);
3✔
218
                foreach ($this->cells as $cell) {
3✔
219
                        $cellHtml = $cell->render();
3✔
220
                        $element->addHtml($cellHtml);
3✔
221
                }
222

223
                $this->setOption(RendererOptions::_RENDERED, true);
3✔
224

225
                return $element;
3✔
226
        }
227

228
        /**
229
         * Sets option
230
         *
231
         * @param mixed|null $value
232
         * @internal
233
         */
234
        public function setOption(string $option, $value): void
235
        {
236
                $this->options[$option] = $value;
3✔
237
        }
238

239
}
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