Skip to content

Commit bc52385

Browse files
committed
update
1 parent 151b41f commit bc52385

File tree

15 files changed

+187
-136
lines changed

15 files changed

+187
-136
lines changed

src/Bulma/Field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct()
1717
public function setError(string $message): void
1818
{
1919
parent::setError($message);
20-
$this->removeClass('has-error');
20+
$this->removeClass('error');
2121
}
2222

2323
public function renderDom(\DOMDocument $doc): \DOMElement

src/Checkboxes.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ class Checkboxes implements Control
1414
protected array $options = [];
1515
/** @var string[] $values */
1616
protected array $values = [];
17+
18+
protected bool $disabled = false;
19+
protected bool $readonly = false;
1720
protected bool $required = false;
21+
protected bool $autofocus = false;
22+
protected string $autocomplete = '';
1823

1924
public function __construct()
2025
{
@@ -32,12 +37,36 @@ public function getName(): string
3237
return $this->name;
3338
}
3439

40+
public function disabled(): self
41+
{
42+
$this->disabled = true;
43+
return $this;
44+
}
45+
46+
public function readonly(): self
47+
{
48+
$this->readonly = true;
49+
return $this;
50+
}
51+
3552
public function required(): self
3653
{
3754
$this->required = true;
3855
return $this;
3956
}
4057

58+
public function autofocus(): self
59+
{
60+
$this->autofocus = true;
61+
return $this;
62+
}
63+
64+
public function autocomplete(string $value): self
65+
{
66+
$this->autocomplete = $value;
67+
return $this;
68+
}
69+
4170
/**
4271
* @param array<string,string> $options
4372
*/
@@ -66,13 +95,16 @@ public function fill(array $data): void
6695
}
6796

6897
/**
69-
* @return array<string, string|string[]|null>
98+
* @return array<string, string|string[]|null> $data
7099
*/
71-
public function extract(): array
100+
public function extract(bool $withNulls = false): array
72101
{
73102
if (!$this->name) {
74103
return [];
75104
}
105+
if ($this->disabled) {
106+
return [];
107+
}
76108
return [$this->name => $this->values];
77109
}
78110

src/Control.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ public function getId(): string;
1111
public function name(string $name): self;
1212
public function getName(): string;
1313
public function value(string $value): self;
14+
15+
public function disabled(): self;
16+
public function readonly(): self;
1417
public function required(): self;
18+
public function autofocus(): self;
19+
public function autocomplete(string $value): self;
20+
1521
/**
1622
* @param array<string, string|string[]|null> $data
1723
*/
1824
public function fill(array $data): void;
1925
/**
20-
* @return array<string, string|string[]|null>
26+
* @return array<string, string|string[]|null> $data
2127
*/
22-
public function extract(): array;
28+
public function extract(bool $withNulls = false): array;
2329
public function validate(Validator $validator): string;
2430
public function setError(string $message): void;
2531
public function renderDom(\DOMDocument $doc): \DOMElement;

src/Error.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class Error
1212

1313
public function __construct()
1414
{
15-
$this->tag('span');
16-
$this->class('help-block');
15+
$this->tag('div');
1716
}
1817

1918
public function message(string $message): self

src/Field.php

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,28 @@ class Field
1212
protected ?Label $label = null;
1313
protected ?Control $control = null;
1414
protected ?Error $error = null;
15-
/** @var Validator[] */
15+
/** @var Validator[] $validators */
1616
protected array $validators = [];
1717

1818
protected bool $hideError = false;
1919

2020
protected bool $disabled = false;
21-
protected bool $readonly = false;
22-
protected bool $required = false;
23-
protected bool $autofocus = false;
2421

2522
public function __construct()
2623
{
2724
$this->tag('div');
28-
$this->class('form-group');
2925
}
3026

3127
public function disabled(): self
3228
{
3329
$this->class('disabled');
3430
$this->disabled = true;
35-
return $this;
36-
}
37-
38-
public function readonly(): self
39-
{
40-
$this->readonly = true;
41-
return $this;
42-
}
43-
44-
public function required(): self
45-
{
46-
$this->required = true;
47-
return $this;
48-
}
49-
50-
public function autofocus(): self
51-
{
52-
$this->autofocus = true;
31+
if ($this->label) {
32+
$this->label->disabled();
33+
}
34+
if ($this->control) {
35+
$this->control->disabled();
36+
}
5337
return $this;
5438
}
5539

@@ -98,13 +82,13 @@ public function fill(array $data): void
9882
}
9983

10084
/**
101-
* @return array<string, string|string[]|null>
85+
* @return array<string, string|string[]|null> $data
10286
*/
103-
public function extract(): array
87+
public function extract(bool $withNulls = false): array
10488
{
10589
$data = [];
10690
if ($this->control && !$this->disabled) {
107-
$data = $this->control->extract();
91+
$data = $this->control->extract($withNulls);
10892
}
10993
return $data;
11094
}
@@ -126,9 +110,9 @@ public function validate(): string
126110
public function setError(string $message): void
127111
{
128112
if ($message) {
129-
$this->addClass('has-error');
113+
$this->addClass('error');
130114
} else {
131-
$this->removeClass('has-error');
115+
$this->removeClass('error');
132116
}
133117
if ($this->label) {
134118
$this->label->setError($message);
@@ -167,18 +151,6 @@ public function renderDom(\DOMDocument $doc): \DOMElement
167151
}
168152
if ($this->control) {
169153
$control = $this->control->renderDom($doc);
170-
if ($this->disabled) {
171-
$control->setAttribute('disabled', 'disabled');
172-
}
173-
if ($this->readonly) {
174-
$control->setAttribute('readonly', 'readonly');
175-
}
176-
if ($this->required) {
177-
$control->setAttribute('required', 'required');
178-
}
179-
if ($this->autofocus) {
180-
$control->setAttribute('autofocus', 'autofocus');
181-
}
182154
$field->appendChild($control);
183155
}
184156
if ($this->error) {

src/Fieldset.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Fieldset
99
{
1010
use HtmlElement;
1111

12-
/** @var Field[] */
12+
/** @var Field[] $fields */
1313
protected array $fields = [];
1414
protected ?Legend $legend = null;
1515
protected ?Header $header = null;
@@ -59,13 +59,13 @@ public function fill(array $data): void
5959
}
6060

6161
/**
62-
* @return array<string, string|string[]|null>
62+
* @return array<string, string|string[]|null> $data
6363
*/
64-
public function extract(): array
64+
public function extract(bool $withNulls = false): array
6565
{
6666
$data = [];
6767
foreach ($this->fields as $field) {
68-
foreach ($field->extract() as $name => $value) {
68+
foreach ($field->extract($withNulls) as $name => $value) {
6969
if (isset($data[$name])) {
7070
if (!is_array($data[$name])) {
7171
$data[$name] = [$data[$name]];

src/Form.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Form
99
protected string $action = '';
1010
protected string $method = 'POST';
1111
protected string $enctype = 'application/x-www-form-urlencoded';
12-
/** @var Fieldset[] */
12+
/** @var Fieldset[] $fieldsets */
1313
protected array $fieldsets = [];
1414

1515
protected bool $hideFieldsets = false;
@@ -131,14 +131,14 @@ public function fill(array $data): void
131131
}
132132

133133
/**
134-
* @return array<string, string|string[]|null>
134+
* @return array<string, string|string[]|null> $data
135135
*/
136-
public function extract(): array
136+
public function extract(bool $withNulls = false): array
137137
{
138138

139139
$data = [];
140140
foreach ($this->fieldsets as $fieldset) {
141-
foreach ($fieldset->extract() as $name => $value) {
141+
foreach ($fieldset->extract($withNulls) as $name => $value) {
142142
if (isset($data[$name])) {
143143
if (!is_array($data[$name])) {
144144
$data[$name] = [$data[$name]];

src/HtmlElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait HtmlElement
1111
protected string $id = '';
1212
/** @var string[] */
1313
protected array $classes = [];
14-
/** @var array<string, string> */
14+
/** @var array<string, string> $attributes */
1515
protected array $attributes = [];
1616

1717
protected function tag(string $tag): self

src/Input.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ class Input implements Control
1313
protected string $value = '';
1414
protected string $type = 'text';
1515
protected string $placeholder = '';
16+
17+
protected bool $disabled = false;
18+
protected bool $readonly = false;
1619
protected bool $required = false;
20+
protected bool $autofocus = false;
21+
protected string $autocomplete = '';
1722

1823
public function __construct()
1924
{
@@ -37,12 +42,36 @@ public function getName(): string
3742
return $this->name;
3843
}
3944

45+
public function disabled(): self
46+
{
47+
$this->disabled = true;
48+
return $this;
49+
}
50+
51+
public function readonly(): self
52+
{
53+
$this->readonly = true;
54+
return $this;
55+
}
56+
4057
public function required(): self
4158
{
4259
$this->required = true;
4360
return $this;
4461
}
4562

63+
public function autofocus(): self
64+
{
65+
$this->autofocus = true;
66+
return $this;
67+
}
68+
69+
public function autocomplete(string $value): self
70+
{
71+
$this->autocomplete = $value;
72+
return $this;
73+
}
74+
4675
public function value(string $value): self
4776
{
4877
$this->value = $value;
@@ -71,15 +100,21 @@ public function fill(array $data): void
71100
}
72101

73102
/**
74-
* @return array<string, string|string[]|null>
103+
* @return array<string, string|string[]|null> $data
75104
*/
76-
public function extract(): array
105+
public function extract(bool $withNulls = false): array
77106
{
78107
if (!$this->name) {
79108
return [];
80109
}
110+
if ($this->disabled) {
111+
return [];
112+
}
81113
$value = trim($this->value);
82-
return [$this->name => strlen($value) ? $value : null];
114+
if ($withNulls) {
115+
$value = strlen($value) ? $value : null;
116+
}
117+
return [$this->name => $value];
83118
}
84119

85120
public function validate(Validator $validator): string
@@ -103,6 +138,21 @@ public function renderDom(\DOMDocument $doc): \DOMElement
103138
if ($this->placeholder) {
104139
$input->setAttribute('placeholder', $this->placeholder);
105140
}
141+
if ($this->disabled) {
142+
$input->setAttribute('disabled', 'disabled');
143+
}
144+
if ($this->readonly) {
145+
$input->setAttribute('readonly', 'readonly');
146+
}
147+
if ($this->required) {
148+
$input->setAttribute('required', 'required');
149+
}
150+
if ($this->autofocus) {
151+
$input->setAttribute('autofocus', 'autofocus');
152+
}
153+
if ($this->autocomplete) {
154+
$input->setAttribute('autocomplete', 'on');
155+
}
106156
return $input;
107157
}
108158
}

src/Label.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Label
1010

1111
protected string $caption = '';
1212
protected string $for = '';
13+
protected bool $disabled = false;
1314

1415
public function __construct()
1516
{
@@ -28,8 +29,14 @@ public function for(string $for): self
2829
return $this;
2930
}
3031

31-
public function setError(string $message): void {}
32+
public function disabled(): self
33+
{
34+
$this->disabled = true;
35+
$this->class('disabled');
36+
return $this;
37+
}
3238

39+
public function setError(string $message): void {}
3340

3441
public function renderDom(\DOMDocument $doc): \DOMElement
3542
{

0 commit comments

Comments
 (0)