8000 updates for php8 · laravel-enso/forms@31c7c1c · GitHub
[go: up one dir, main page]

Skip to content

Commit 31c7c1c

Browse files
committed
updates for php8
1 parent f165fe2 commit 31c7c1c

File tree

16 files changed

+115
-136
lines changed

16 files changed

+115
-136
lines changed

composer.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
"homepage": "https://github.com/laravel-enso/forms",
1111
"type": "library",
1212
"license": "MIT",
13-
"authors": [{
14-
"name": "Adrian Ocneanu",
15-
"email": "aocneanu@gmail.com",
16-
"homepage": "https://laravel-enso.com",
17-
"role": "Developer"
18-
}],
13+
"authors": [
14+
{
15+
"name": "Adrian Ocneanu",
16+
"email": "aocneanu@gmail.com",
17+
"homepage": "https://laravel-enso.com",
18+
"role": "Developer"
19+
}
20+
],
1921
"require": {
20-
"php": ">=7.4.0",
21-
"laravel/framework": "^7.0|^8.0",
22+
"php": ">=8.0",
23+
"laravel/framework": "^8.0",
2224
"laravel-enso/enums": "^2.0",
2325
"laravel-enso/helpers": "^2.0"
2426
},
@@ -35,4 +37,4 @@
3537
"aliases": {}
3638
}
3739
}
38-
}
40+
}

src/AppServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function boot()
1515
__DIR__.'/../config' => config_path('enso'),
1616
], ['forms-config', 'enso-config']);
1717

18-
(new Collection(['Forms/Builders/ModelForm', 'Forms/Templates/template']))
18+
Collection::wrap(['Forms/Builders/ModelForm', 'Forms/Templates/template'])
1919
->each(fn ($stub) => $this->publishes([
2020
__DIR__."/../stubs/{$stub}.stub" => app_path("{$stub}.php"),
2121
], 'forms-resources'));

src/Services/Builder.php

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313

1414
class Builder
1515
{
16-
private Obj $template;
17-
private Collection $dirty;
18-
private ?Model $model;
19-
20-
public function __construct(Obj $template, Collection $dirty, ?Model $model = null)
21-
{
22-
$this->template = $template;
23-
$this->model = $model;
24-
$this->dirty = $dirty;
16+
public function __construct(
17+
private Obj $template,
18+
private Collection $dirty,
19+
private ?Model $model = null
20+
) {
2521
}
2622

2723
public function run(): void
@@ -56,28 +52,21 @@ private function value($field)
5652

5753
$meta = $field->get('meta');
5854

59-
switch ($meta->get('type')) {
60-
case 'input':
61-
return $this->inputValue($value, $meta);
62-
case 'datepicker':
63-
return $this->dateValue($value, $meta);
64-
case 'select':
65-
return $this->selectValue($value, $meta);
66-
default:
67-
return $value;
68-
}
55+
return match ($meta->get('type')) {
56+
'input' => $this->inputValue($value, $meta),
57+
'datepicker' => $this->dateValue($value, $meta),
58+
'select' => $this->selectValue($value, $meta),
59+
default => $value
60+
};
6961
}
7062

7163
private function inputValue($value, $meta)
7264
{
73-
switch ($meta->get('content')) {
74-
case 'text':
75-
return $value ?? '';
76-
case 'encrypt':
77-
return isset($value) ? Fields::EncryptValue : null;
78-
default:
79-
return $value;
80-
}
65+
return match ($meta->get('content')) {
66+
'text' => $value ?? '',
67+
'encrypt' => isset($value) ? Fields::EncryptValue : null,
68+
default => $value,
69+
};
8170
}
8271

8372
private function dateValue($value, $meta)
@@ -115,20 +104,13 @@ private function computeMeta($field)
115104
{
116105
$meta = $field->get('meta');
117106

118-
switch ($meta->get('type')) {
119-
case 'select':
120-
$this->computeSelect($meta);
121-
break;
122-
case 'input':
123-
$this->computeInput($field);
124-
break;
125-
case 'datepicker':
126-
$this->computeDate($meta);
127-
break;
128-
case 'wysiwyg':
129-
$this->computeWysiwyg($meta);
130-
break;
131-
}
107+
match ($meta->get('type')) {
108+
'select' => $this->computeSelect($meta),
109+
'input' => $this->computeInput($field),
110+
'datepicker' => $this->computeDate($meta),
111+
'wysiwyg' => $this->computeWysiwyg($meta),
112+
default => null,
113+
};
132114
}
133115

134116
private function computeInput($field): void
@@ -183,10 +165,8 @@ private function attributeValue($field)
183165
private function computeActions(): self
184166
{
185167
$actions = $this->template->get('actions')
186-
->reduce(fn ($collector, $action) => $collector->set(
187-
$action,
188-
$this->actionConfig($action)
189-
), new Obj());
168+
->reduce(fn ($collector, $action) => $collector
169+
->set($action, $this->actionConfig($action)), new Obj());
190170

191171
$this->template->set('actions', $actions);
192172

@@ -200,8 +180,7 @@ private function actionConfig($action): array
200180
? $this->template->get('routes')->get($action)
201181
: $this->template->get('routePrefix').'.'.$action;
202182

203-
[$routeOrPath, $value] = (new Collection(['create', 'show', 'back']))
204-
->contains($action)
183+
[$routeOrPath, $value] = in_array($action, ['create', 'show', 'back'])
205184
? ['route', $route]
206185
: ['path', route($route, $this->template->get('routeParams'), false)];
207186

src/Services/Form.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class Form
1818
{
1919
use When;
2020

21-
private ?Model $model;
2221
private Obj $template;
2322
private Collection $dirty;
23+
private ?Model $model;
2424

2525
public function __construct(string $filename)
2626
{
@@ -125,8 +125,9 @@ public function columns(string $field, int $value): self
125125

126126
public function hide($fields): self
127127
{
128-
(new Collection($fields))->each(fn ($field) => $this->field($field)
129-
->get('meta')->set('hidden', true));
128+
Collection::wrap($fields)
129+
->each(fn ($field) => $this->field($field)
130+
->get('meta')->set('hidden', true));
130131

131132
return $this;
132133
}
@@ -161,24 +162,27 @@ public function showTab($tabs): self
161162

162163
public function show($fields): self
163164
{
164-
(new Collection($fields))->each(fn ($field) => $this->field($field)
165-
->get('meta')->set('hidden', false));
165+
Collection::wrap($fields)
166+
->each(fn ($field) => $this->field($field)
167+
->get('meta')->set('hidden', false));
166168

167169
return $this;
168170
}
169171

170172
public function disable($fields): self
171173
{
172-
(new Collection($fields))->each(fn ($field) => $this->field($field)
173-
->get('meta')->set('disabled', true));
174+
Collection::wrap($fields)
175+
->each(fn ($field) => $this->field($field)
176+
->get('meta')->set('disabled', true));
174177

175178
return $this;
176179
}
177180

178181
public function readonly($fields): self
179182
{
180-
(new Collection($fields))->each(fn ($field) => $this->field($field)
181-
->get('meta')->set('readonly', true));
183+
Collection::wrap($fields)
184+
->each(fn ($field) => $this->field($field)
185+
->get('meta')->set('readonly', true));
182186

183187
return $this;
184188
}
@@ -224,15 +228,15 @@ public function labels(bool $labels): self
224228

225229
public function sectionVisibility($fields, bool $hidden): self
226230
{
227-
(new Collection($fields))
231+
Collection::wrap($fields)
228232
->each(fn ($field) => $this->section($field)->put('hidden', $hidden));
229233

230234
return $this;
231235
}
232236

233237
public function tabVisibility($tabs, bool $hidden): self
234238
{
235-
$tabs = (new Collection($tabs));
239+
$tabs = new Collection($tabs);
236240

237241
$this->template->get('sections')->each(fn ($section) => $tabs->when(
238242
$tabs->contains($section->get('tab')),
@@ -280,7 +284,7 @@ private function section(string $field): Obj
280284
->contains(fn ($sectionField) => $sectionField->get('name') === $field));
281285

282286
if (! $section) {
283-
$this->throwMissingFieldException($field);
287+
Template::fieldMissing($field);
284288
}
285289

286290
return $section;
@@ -294,21 +298,16 @@ private function field(string $fieldName): Obj
294298
->first(fn ($field) => $field->get('name') === $fieldName);
295299

296300
if (! $field) {
297-
$this->throwMissingFieldException($fieldName);
301+
Template::fieldMissing($fieldName);
298302
}
299303

300304
return $field;
301305
}
302306

303307
private function needsValidation(): bool
304308
{
305-
return (new Collection([App::environment(), 'always']))->contains(
306-
Config::get('enso.forms.validations')
307-
);
308-
}
309+
$validations = Config::get('enso.forms.validations');
309310

310-
private function throwMissingFieldException($fieldName): void
311-
{
312-
throw Template::fieldMissing($fieldName);
311+
return in_array($validations, [App::environment(), 'always']);
313312
}
314313
}

src/Services/Validators/Actions.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99

1010
class Actions
1111
{
12-
private Obj $template;
13-
14-
public function __construct(Obj $template)
12+
public function __construct(private Obj $template)
1513
{
16-
$this->template = $template;
1714
}
1815

1916
public function validate(): void
2017
{
21-
$attributes = (new Collection(Attributes::Create))
18+
$attributes = Collection::wrap(Attributes::Create)
2219
->merge(Attributes::Update)
2320
->unique()
2421
->values();
@@ -28,7 +25,8 @@ public function validate(): void
2825

2926
if ($diff->isNotEmpty()) {
3027
throw Template::unknownActions(
31-
$diff->implode(', '), $attributes->implode(', ')
28+
$diff->implode(', '),
29+
$attributes->implode(', ')
3230
);
3331
}
3432
}

src/Services/Validators/Fields.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99

1010
class Fields
1111
{
12-
private Obj $template;
13-
14-
public function __construct(Obj $template)
12+
public function __construct(private Obj $template)
1513
{
16-
$this->template = $template;
1714
}
1815

1916
public function validate(): void
@@ -41,8 +38,8 @@ private function format($section): void
4138
{
4239
$valid = $section->get('fields') instanceof Obj
4340
&& $section->get('fields')
44-
->filter(fn ($field) => ! $field instanceof Obj)
45-
->isEmpty();
41+
->filter(fn ($field) => ! $field instanceof Obj)
42+
->isEmpty();
4643

4744
if (! $valid) {
4845
throw Template::invalidFieldsFormat();
@@ -51,8 +48,7 @@ private function format($section): void
5148

5249
private function attributes($field): self
5350
{
54-
$diff = (new Collection(Attributes::List))
55-
->diff($field->keys());
51+
$diff = Collection::wrap(Attributes::List)->diff($field->keys());
5652

5753
if ($diff->isNotEmpty()) {
5854
throw Template::missingFieldAttributes($diff->implode('", "'));
@@ -77,8 +73,10 @@ private function value($field): void
7773
return;
7874
}
7975

80-
if ($meta->get('type') === 'select' && $meta->get('multiple')
81-
&& ! is_array($field->get('value')) && ! is_object($field->get('value'))) {
76+
$invalidSelectValue = $meta->get('type') === 'select' && $meta->get('multiple')
77+
&& ! is_array($field->get('value')) && ! is_object($field->get('value'));
78+
79+
if ($invalidSelectValue) {
8280
throw Template::invalidSelectValue($field->get('name'));
8381
}
8482
}

src/Services/Validators/Meta.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
class Meta
1212
{
13-
private Obj $field;
1413
private ?Obj $meta;
1514

16-
public function __construct(Obj $field)
15+
public function __construct(private Obj $field)
1716
{
18-
$this->field = $field;
1917
$this->meta = $field->get('meta');
2018
}
2119

@@ -33,7 +31,7 @@ public function validate(): void
3331

3432
private function mandatoryAttributes(): self
3533
{
36-
$diff = (new Collection(Attributes::Mandatory))
34+
$diff = Collection::wrap(Attributes::Mandatory)
3735
->diff($this->meta->keys());
3836

3937
if ($diff->isNotEmpty()) {
@@ -48,7 +46,7 @@ private function mandatoryAttributes(): self
4846

4947
private function optionalAttributes(): self
5048
{
51-
$attributes = (new Collection(Attributes::Mandatory))
49+
$attributes = Collection::wrap(Attributes::Mandatory)
5250
->merge(Attributes::Optional);
5351

5452
$diff = $this->meta->keys()
@@ -89,7 +87,7 @@ private function format(): self
8987

9088
private function type(): void
9189
{
92-
if (! (new Collection(Attributes::Types))->contains($this->meta->get('type'))) {
90+
if (! in_array($this->meta->get('type'), Attributes::Types)) {
9391
throw Template::invalidFieldType($this->meta->get('type'));
9492
}
9593
}

src/Services/Validators/Routes.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
class Routes
1010
{
11-
private Obj $template;
12-
13-
public function __construct(Obj $template)
11+
public function __construct(private Obj $template)
1412
{
15-
$this->template = $template;
1613
}
1714

1815
public function validate()

0 commit comments

Comments
 (0)
0