• 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

84.62
/src/Grid/BootstrapCell.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\Traits\BootstrapContainerTrait;
8
use Nette\ComponentModel\IComponent;
9
use Nette\Forms\Control;
10
use Nette\Forms\ControlGroup;
11
use Nette\SmartObject;
12
use Nette\Utils\Html;
13

14
/**
15
 * Class BootstrapCell.
16
 * Represents a row-column pair = table cell in Bootstrap grid system. This is the part with col-*-* class.
17
 * Only one component can be present.
18
 *
19
 * @property-read int  $numOfColumns     Number of Bootstrap columns to occupy
20
 * @property-read Control $childControls|null     Nested child control if any
21
 * @property-read Html $elementPrototype the Html div that will be rendered. You may define additional
22
 *                properties.
23
 */
24
class BootstrapCell
25
{
26

27
        use SmartObject;
28
        use BootstrapContainerTrait;
29

30
        /**
31
         * Only use 'col' class (auto stretch)
32
         */
33
        public const COLUMNS_NONE = 0;
34

35
        /**
36
         * Use 'col-auto'
37
         */
38
        public const COLUMNS_AUTO = null;
39

40
        /** @var ControlGroup|null */
41
        protected $currentGroup;
42

43
        /** @var int|null */
44
        private $numOfColumns;
45

46
        /** @var Control[]|null */
47
        private $childControls = [];
48

49
        /** @var BootstrapRow */
50
        private $row;
51

52
        /** @var Html */
53
        private $elementPrototype;
54

55
        /**
56
         * @param BootstrapRow   $row          Row this is a child of
57
         * @param int|null $numOfColumns Number of Bootstrap columns to occupy. You can use an integer or
58
         * BootstrapCell::COLUMNS_* constant (see their docs for more)
59
         */
60
        public function __construct(BootstrapRow $row, ?int $numOfColumns)
61
        {
62
                $this->numOfColumns = $numOfColumns;
6✔
63
                $this->row = $row;
6✔
64

65
                $this->elementPrototype = Html::el('div');
6✔
66
        }
67

68
        /**
69
         * Gets the prototype of this cell so you can define additional attributes. Col-* class is added during
70
         * rendering and is not present, so don't add it...
71
         */
72
        public function getElementPrototype(): Html
73
        {
74
                return $this->elementPrototype;
×
75
        }
76

77
        /**
78
         * @see BootstrapCell::$numOfColumns
79
         */
80
        public function getNumOfColumns(): ?int
81
        {
82
                return $this->numOfColumns;
×
83
        }
84

85
        /**
86
         * Renders the cell into Html object
87
         */
88
        public function render(): Html
89
        {
90
                $element = $this->elementPrototype;
4✔
91

92
                /** @var BootstrapRenderer $renderer */
93
                $renderer = $this->row->getParent()->form->renderer;
4✔
94

95
                $element = $renderer->configElem(RendererConfig::GRID_CELL, $element);
4✔
96
                $element->class[] = $this->createClass();
4✔
97

98
                foreach ($this->childControls as $childControl) {
4✔
99
                        $pairHtml = $renderer->renderPair($childControl);
3✔
100
                        $element->addHtml($pairHtml);
3✔
101
                }
102

103
                return $element;
4✔
104
        }
105

106
        /**
107
         * Delegate to underlying component.
108
         *
109
         * @param null $insertBefore
110
         */
111
        public function addComponent(IComponent $component, ?string $name, $insertBefore = null): void
112
        {
113
                /** @noinspection PhpInternalEntityUsedInspection */
114
                $this->row->addComponent($component, $name, $insertBefore);
3✔
115
                $this->childControls[] = $component;
3✔
116
        }
117

118
        public function getCurrentGroup(): ?ControlGroup
119
        {
120
                return $this->currentGroup;
1✔
121
        }
122

123
        public function setCurrentGroup(?ControlGroup $currentGroup): self
124
        {
125
                $this->currentGroup = $currentGroup;
1✔
126

127
                return $this;
1✔
128
        }
129

130
        /**
131
         * @return static
132
         */
133
        public function addHtmlClass(string $class)
134
        {
135
                $this->elementPrototype->class[] = $class;
1✔
136

137
                return $this;
1✔
138
        }
139

140
        /**
141
         * Creates column class based on numOfColumns
142
         */
143
        protected function createClass(): string
144
        {
145
                $cols = $this->numOfColumns;
4✔
146

147
                if ($cols === self::COLUMNS_NONE) {
4✔
148
                        return 'col';
×
149
                }
150

151
                if ($cols === self::COLUMNS_AUTO) {
4✔
152
                        return 'col-auto';
×
153
                }
154

155
                return $this->row->gridBreakPoint !== null ? 'col-' . $this->row->gridBreakPoint . '-' . $this->numOfColumns : 'col-' . $this->numOfColumns;
4✔
156
        }
157

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