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

contributte / forms-bootstrap / #71

22 Apr 2024 10:51AM UTC coverage: 83.039%. Remained the same
#71

Pull #95

github

dakorpar
compativility with nette forms 3.2.2
Pull Request #95: Nette forms3.2.2

705 of 849 relevant lines covered (83.04%)

5.64 hits per line

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

84.44
/src/Inputs/DateInput.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\FormsBootstrap\Inputs;
4

5
use Contributte\FormsBootstrap\Enums\DateTimeFormat;
6
use DateTime;
7
use DateTimeInterface;
8
use Nette\NotSupportedException;
9
use Nette\Utils\Html;
10

11
/**
12
 * Class DateInput. Textual date input.
13
 *
14
 * @property string $format expected PHP format for datetime
15
 */
16
class DateInput extends TextInput
17
{
18

19
        /** @var string */
20
        public static $defaultFormat = DateTimeFormat::D_DMY_DOTS_NO_LEAD;
21

22
        /** @var string[] */
23
        public static $additionalHtmlClasses = [];
24

25
        /** @var bool */
26
        public static $addDefaultPlaceholder = true;
27

28
        /**
29
         * This errorMessage is added for invalid format
30
         *
31
         * @var string
32
         */
33
        public $invalidFormatMessage = 'invalid/incorrect format';
34

35
        /**
36
         * Input accepted format.
37
         * Default is d.m.yyyy
38
         *
39
         * @var string
40
         */
41
        private $format;
42

43
        /** @var bool */
44
        private $isValidated = false;
45

46
        /**
47
         * @param Html|string|null $label
48
         */
49
        public function __construct($label = null, ?int $maxLength = null)
50
        {
51
                if ($maxLength !== null) {
7✔
52
                        throw new NotSupportedException('Do not set $maxLength!');
×
53
                }
54

55
                parent::__construct($label, null);
7✔
56

57
                $this->addRule(fn ($input) => DateTimeFormat::validate($this->format, $input->value), $this->invalidFormatMessage);
7✔
58

59
                $this->setFormat(static::$defaultFormat);
7✔
60
        }
61

62
        /**
63
         * @inheritdoc
64
         */
65
        public function cleanErrors(): void
66
        {
67
                $this->isValidated = false;
2✔
68
        }
69

70
        /**
71
         * @inheritdoc
72
         */
73
        public function getValue(): mixed
74
        {
75
                $val = parent::getValue();
2✔
76

77
                if (empty($val) || !$this->isValidated) {
2✔
78
                        return $val;
2✔
79
                }
80

81
                $value = DateTime::createFromFormat($this->format, $val);
2✔
82
                if (!$value) {
2✔
83
                        return null;
×
84
                }
85

86
                return $value;
2✔
87
        }
88

89
        /**
90
         * @param DateTimeInterface|null $value
91
         * @return static
92
         */
93
        public function setValue($value)
94
        {
95
                if ($value instanceof DateTimeInterface) {
7✔
96
                        parent::setValue($value->format($this->format));
×
97

98
                        $this->validate();
×
99

100
                        return $this;
×
101
                } elseif (is_string($value) && DateTimeFormat::validate($this->format, $value)) {
7✔
102
                        parent::setValue($value);
1✔
103

104
                        $this->validate();
1✔
105

106
                        return $this;
1✔
107
                } elseif ($value === null) {
7✔
108
                        parent::setValue(null);
7✔
109

110
                        return $this;
7✔
111
                } else {
112

113
                        //maybe date from database?
114
                        $date = DateTime::createFromFormat(DateTimeFormat::MYSQL_WITH_MICROSECONDS, $value);
1✔
115

116
                        if ($date === false) {
1✔
117
                                $date = DateTime::createFromFormat(DateTimeFormat::MYSQL_WITHOUT_MICROSECONDS, $value);
1✔
118
                        }
119

120
                        if ($date !== false) {
1✔
121
                                parent::setValue($date->format($this->format));
1✔
122

123
                                $this->validate();
1✔
124

125
                                return $this;
1✔
126
                        }
127

128
                        // this will fail validation test, but we don't want to throw an exception here
129
                        parent::setValue($value);
×
130

131
                        return $this;
×
132
                }
133
        }
134

135
        public function getControl(): Html
136
        {
137
                $control = parent::getControl();
5✔
138
                $control->class[] = implode(' ', static::$additionalHtmlClasses);
5✔
139

140
                return $control;
5✔
141
        }
142

143
        /**
144
         * @inheritdoc
145
         */
146
        public function validate(): void
147
        {
148
                parent::validate();
2✔
149

150
                $this->isValidated = true;
2✔
151
        }
152

153
        /**
154
         * @see DateInput::$format
155
         */
156
        public function getFormat(): string
157
        {
158
                return $this->format;
1✔
159
        }
160

161
        /**
162
         * Input accepted format.
163
         * Default is d.m.yyyy h:mm
164
         */
165
        public function setFormat(string $format, ?string $placeholder = null): self
166
        {
167
                $this->format = $format;
7✔
168

169
                if ($placeholder === null && static::$addDefaultPlaceholder) {
7✔
170
                        $placeholder = DateTimeFormat::toHumanFormat($format);
7✔
171
                }
172

173
                if (!empty($placeholder)) {
7✔
174
                        $this->setPlaceholder($placeholder);
7✔
175
                }
176

177
                return $this;
7✔
178
        }
179

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