From 21d78c05f9029dc0e5fc96c2182c05836afdc78a Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Sun, 1 Dec 2024 14:44:00 +0200 Subject: [PATCH 1/7] feat: support string url as path for files --- src/Fields/File.php | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Fields/File.php b/src/Fields/File.php index 0713a0025..399f9d85d 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -229,13 +229,37 @@ protected function columnsThatShouldBeDeleted(): array public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null) { + // Handle URL input first + if ($request->has($this->attribute) && is_string($request->input($this->attribute))) { + $url = $request->input($this->attribute); + + if (filter_var($url, FILTER_VALIDATE_URL)) { + if ($this->isPrunable()) { + call_user_func( + $this->deleteCallback, + $request, + $model, + $this->getStorageDisk(), + $this->getStoragePath() + ); + } + + $model->{$this->attribute} = $url; + + if ($this->originalNameColumn) { + $model->{$this->originalNameColumn} = basename($url); + } + + return $this; + } + } + + // Existing file upload logic if (is_null($file = $request->file($this->attribute)) || ! $file->isValid()) { return $this; } if ($this->isPrunable()) { - // Delete old file if exists. - // return function () use ($model, $request) { call_user_func( $this->deleteCallback, $request, @@ -243,7 +267,6 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n $this->getStorageDisk(), $this->getStoragePath() ); - // }; } $result = call_user_func( @@ -276,6 +299,19 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n return $this; } + public function getStoringRules(): array + { + $rules = parent::getStoringRules(); + + // Modify validation to accept URLs + foreach ($rules as &$rule) { + if (is_string($rule) && Str::startsWith($rule, 'file')) { + $rule = 'sometimes|'.$rule; + } + } + + return $rules; + } /** * Get the full path that the field is stored at on disk. * From 85efc72a0df7c068cbe0796fa4d7e81d2641a197 Mon Sep 17 00:00:00 2001 From: binaryk Date: Sun, 1 Dec 2024 12:44:30 +0000 Subject: [PATCH 2/7] Fix styling --- src/Fields/File.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Fields/File.php b/src/Fields/File.php index 399f9d85d..c3a8f21cd 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -312,6 +312,7 @@ public function getStoringRules(): array return $rules; } + /** * Get the full path that the field is stored at on disk. * From 75312f52389cf101d59d18ba36970089347052cb Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 2 Dec 2024 20:08:00 +0200 Subject: [PATCH 3/7] fix: fixing str --- src/Fields/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/File.php b/src/Fields/File.php index 399f9d85d..68f5422d1 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -305,7 +305,7 @@ public function getStoringRules(): array // Modify validation to accept URLs foreach ($rules as &$rule) { - if (is_string($rule) && Str::startsWith($rule, 'file')) { + if (is_string($rule) && str($rule)->startsWith('file')) { $rule = 'sometimes|'.$rule; } } From a2c7abdfbd63a92abf63a002a9d512926d73542d Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Wed, 4 Dec 2024 16:44:28 +0200 Subject: [PATCH 4/7] feat: add defaultCallback method --- src/Fields/Field.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 5e128a565..78f581346 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -115,6 +115,8 @@ class Field extends OrganicField implements JsonSerializable */ protected $valueCallback; + protected $fillDefaultCallback; + /** * Closure be used to be called after the field value stored. */ @@ -205,6 +207,13 @@ public function fillCallback(callable|Closure $callback) return $this; } + public function defaultCallback(mixed $callback) + { + $this->defaultCallback = $callback; + + return $this; + } + /** * Fill attribute with value from the request or delegate this action to the user defined callback. * @@ -246,6 +255,12 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n $bulkRow ); + $this->fillAttributeFromDefault( + $request, + $model, + $this->label ?? $this->attribute + ); + $this->fillAttributeFromValue( $request, $model, @@ -310,6 +325,23 @@ protected function fillAttributeFromValue(RestifyRequest $request, $model, $attr return $this; } + protected function fillAttributeFromDefault(RestifyRequest $request, $model, $attribute) + { + if ($model->{$attribute}) { + return $this; + } + + if (! isset($this->fillDefaultCallback)) { + return $this; + } + + $model->{$attribute} = is_callable($this->fillDefaultCallback) + ? call_user_func($this->fillDefaultCallback, $request, $model, $attribute) + : $this->fillDefaultCallback; + + return $this; + } + /** * @return callable|string|null */ @@ -547,7 +579,7 @@ public function label($label) public function serializeToValue($request) { return [ - $this->label ?? $this->attribute => $this->value ?? $this->resolveDefaultValue($request), + $this->label ?? $this->attribute => $this->value ?? $this->resolveDefaultValue($request), ]; } @@ -563,6 +595,7 @@ public function default($callback) return $this; } + /** * Resolve the default value for the field. * From 5ecd1a5c1f0af84292b73b5d3dcf8a0380edd8c3 Mon Sep 17 00:00:00 2001 From: binaryk Date: Wed, 4 Dec 2024 14:44:57 +0000 Subject: [PATCH 5/7] Fix styling --- src/Fields/Field.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 78f581346..c02b06201 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -579,7 +579,7 @@ public function label($label) public function serializeToValue($request) { return [ - $this->label ?? $this->attribute => $this->value ?? $this->resolveDefaultValue($request), + $this->label ?? $this->attribute => $this->value ?? $this->resolveDefaultValue($request), ]; } @@ -595,7 +595,6 @@ public function default($callback) return $this; } - /** * Resolve the default value for the field. * From 22a2276d8e9a1e99c549bae8cce6bb3e55ba6038 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Wed, 4 Dec 2024 16:52:05 +0200 Subject: [PATCH 6/7] fix: docs --- docs-v2/content/en/api/fields.md | 12 ++++++++++++ src/Fields/Field.php | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs-v2/content/en/api/fields.md b/docs-v2/content/en/api/fields.md index 994c7141f..bf6da1ec9 100644 --- a/docs-v2/content/en/api/fields.md +++ b/docs-v2/content/en/api/fields.md @@ -369,6 +369,18 @@ Now, for the fields that don't have a description into the database, it will ret The default value is ONLY used for the READ, not for WRITE requests. +### Default Stored Value + +During any (update or store requests), this is called after the fill and store callbacks. + +You can pass a callable or a value, and it will be attached to the model if no value provided otherwise. + +Imagine it's like `attributes` in the model: + +```php +field('currency')->defaultCallback('EUR'), +``` + ## Customizations ### Field label diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 78f581346..a035c7fef 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -207,6 +207,16 @@ public function fillCallback(callable|Closure $callback) return $this; } + /** + * This is called after the fill and store callbacks. + * + * You can pass a callable or a value, and it will be attached to the model if no value provided otherwise. + * + * Imagine it's like `attributes` in the model. + * + * @param mixed $callback + * @return $this + */ public function defaultCallback(mixed $callback) { $this->defaultCallback = $callback; From 69782ab425bad47f3e0fd5f691db4149f905a717 Mon Sep 17 00:00:00 2001 From: binaryk Date: Wed, 4 Dec 2024 14:52:47 +0000 Subject: [PATCH 7/7] Fix styling --- src/Fields/Field.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 3e240a935..7137df4c5 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -214,7 +214,6 @@ public function fillCallback(callable|Closure $callback) * * Imagine it's like `attributes` in the model. * - * @param mixed $callback * @return $this */ public function defaultCallback(mixed $callback)