Skip to content

Commit 017b3c3

Browse files
committed
feat(customizable): add autoTransform functionality to parseValue methods
- Implemented validation for string, integer, and float types in custom fields. - Updated the getValidationRule method in relevant custom field classes. - Added tests to ensure validation works as expected.
1 parent 0c9132c commit 017b3c3

14 files changed

+166
-24
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARG PHP_EXTENSIONS="pdo_sqlite intl"
2-
FROM thecodingmachine/php:8.0-v4-slim-apache AS php
2+
FROM thecodingmachine/php:8.1-v4-slim-apache AS php
33

44
USER root
55

@@ -16,4 +16,4 @@ RUN sudo mv phive.phar /usr/local/bin/phive
1616

1717
USER docker
1818

19-
RUN phive install --force-accept-unsigned --trust-gpg-keys 67F861C3D889C656 phpDocumentor
19+
RUN echo 'y' | phive install --force-accept-unsigned --trust-gpg-keys 67F861C3D889C656 phpDocumentor

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.4'
2-
31
services:
42
php:
53
build: .

src/Concerns/Customizable.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ trait Customizable
1818
{
1919
protected array $validatedCustomFieldValues = [];
2020

21+
protected function isAutoTransform(): bool
22+
{
23+
return false;
24+
}
25+
2126
/**
2227
* Boot Model Observer.
2328
*/
@@ -51,7 +56,7 @@ public function validateCustomFields(): void
5156
return;
5257
}
5358
$customFieldsRules = $customFields->flatMap(function (CustomField $field) {
54-
return $field->getValidationRule();
59+
return $field->getValidationRule($this->isAutoTransform());
5560
})->all();
5661
$validator = Validator::make($unvalidatedCustomFields, $customFieldsRules);
5762
$this->validatedCustomFieldValues = $validator->validate();
@@ -130,7 +135,7 @@ public function loadCustomFieldValues(): void
130135

131136
$this->customFieldValues->each(function (CustomFieldValue $customFieldValue) {
132137
$attribute = "custom_{$customFieldValue->customField->key}";
133-
$this->setAttribute($attribute, $customFieldValue->customField->parseValue($customFieldValue->value));
138+
$this->setAttribute($attribute, $customFieldValue->customField->parseValue($customFieldValue->value, $this->isAutoTransform()));
134139
});
135140
}
136141

src/Models/BooleanCustomField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class BooleanCustomField extends CustomField
88
{
99
use HasParent;
1010

11-
public function getValidationRule(): array
11+
public function getValidationRule($autoTransform = false): array
1212
{
1313
$rules = ['boolean'];
1414
$rules[] = $this->required ? 'required' : 'nullable';
@@ -17,7 +17,7 @@ public function getValidationRule(): array
1717
];
1818
}
1919

20-
public function parseValue($value): bool
20+
public function parseValue($value, $autoTransform = false): bool
2121
{
2222
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
2323
}

src/Models/CustomField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public function values(): HasMany
6464
return $this->hasMany(CustomFieldValue::class);
6565
}
6666

67-
public function getValidationRule(): array
67+
public function getValidationRule($autoTransform = false): array
6868
{
6969
return [];
7070
}
7171

72-
public function parseValue($value): mixed
72+
public function parseValue($value, $autoTransform = false): mixed
7373
{
7474
return $value;
7575
}

src/Models/DateTimeCustomField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class DateTimeCustomField extends CustomField
99
{
1010
use HasParent;
1111

12-
public function getValidationRule(): array
12+
public function getValidationRule($autoTransform = false): array
1313
{
1414
$rules = ['date'];
1515
$rules[] = $this->required ? 'required' : 'nullable';
1616
return [
1717
$this->key => $rules,
1818
];
1919
}
20-
public function parseValue($value): string
20+
public function parseValue($value, $autoTransform = false): string
2121
{
2222
return Carbon::parse($value)->toDateTimeString();
2323
}

src/Models/FloatCustomField.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FloatCustomField extends CustomField
88
{
99
use HasParent;
1010

11-
public function getValidationRule(): array
11+
public function getValidationRule($autoTransform = false): array
1212
{
1313
$rules = ['numeric'];
1414
$rules[] = $this->required ? 'required' : 'nullable';
@@ -17,9 +17,8 @@ public function getValidationRule(): array
1717
];
1818
}
1919

20-
public function parseValue($value): float
20+
public function parseValue($value, $autoTransform = false): float
2121
{
2222
return (float) $value;
2323
}
24-
2524
}

src/Models/IntegerCustomField.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class IntegerCustomField extends CustomField
88
{
99
use HasParent;
1010

11-
public function getValidationRule(): array
11+
public function getValidationRule($autoTransform = false): array
1212
{
1313
$rules = ['integer'];
1414
$rules[] = $this->required ? 'required' : 'nullable';
@@ -17,9 +17,8 @@ public function getValidationRule(): array
1717
];
1818
}
1919

20-
public function parseValue($value): int
20+
public function parseValue($value, $autoTransform = false): int
2121
{
2222
return (int) $value;
2323
}
24-
2524
}

src/Models/SelectCustomField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SelectCustomField extends CustomField
99
{
1010
use HasParent;
1111

12-
public function getValidationRule(): array
12+
public function getValidationRule($autoTransform = false): array
1313
{
1414
$options = $this->available_options->pluck('value')->toArray();
1515
$rules = [Rule::in($options)];
@@ -18,7 +18,7 @@ public function getValidationRule(): array
1818
$this->key => $rules,
1919
];
2020
}
21-
public function parseValue($value): string
21+
public function parseValue($value, $autoTransform = false): string
2222
{
2323
return $value;
2424
}

src/Models/TextCustomField.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ class TextCustomField extends CustomField
88
{
99
use HasParent;
1010

11-
public function getValidationRule(): array
11+
public function getValidationRule($autoTransform = false): array
1212
{
13-
$rules = ['string'];
13+
if (!$autoTransform) {
14+
$rules = ['string'];
15+
}
16+
1417
$rules[] = $this->required ? 'required' : 'nullable';
1518
return [
1619
$this->key => $rules,
1720
];
1821
}
1922

20-
public function parseValue($value): string
23+
public function parseValue($value, $autoTransform = false): string
2124
{
25+
if ($autoTransform) {
26+
return (string) $value;
27+
}
28+
2229
return $value;
2330
}
2431
}

0 commit comments

Comments
 (0)