From 984225fad9707d486c66c32fa8985112844a7bff Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Wed, 14 Jun 2023 16:08:04 +0800 Subject: [PATCH] Small improvements --- composer.json | 2 +- src/Http/Resources/FleetbaseResource.php | 20 ++++++++++++++++++++ src/Traits/HasApiModelBehavior.php | 23 ++++++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index abbf803..d19a770 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.0.9-alpha", + "version": "1.1.0-alpha", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Http/Resources/FleetbaseResource.php b/src/Http/Resources/FleetbaseResource.php index 9647a28..f21e837 100644 --- a/src/Http/Resources/FleetbaseResource.php +++ b/src/Http/Resources/FleetbaseResource.php @@ -5,6 +5,7 @@ use Fleetbase\Support\Http; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Arr; +use Illuminate\Support\Str; class FleetbaseResource extends JsonResource { @@ -56,4 +57,23 @@ public function isEmpty() { return is_null($this->resource) || is_null($this->resource->resource); } + + /** + * Get all internal id properties, only when internal request. + * + * @return array + */ + public function getInternalIds(): array + { + $attributes = $this->getAttributes(); + $internalIds = []; + + foreach ($attributes as $key => $value) { + if (Str::endsWith($key, '_uuid')) { + $internalIds[$key] = $this->when(Http::isInternalRequest(), $value); + } + } + + return $internalIds; + } } diff --git a/src/Traits/HasApiModelBehavior.php b/src/Traits/HasApiModelBehavior.php index 9ec0757..26912f3 100644 --- a/src/Traits/HasApiModelBehavior.php +++ b/src/Traits/HasApiModelBehavior.php @@ -231,7 +231,7 @@ public function updateRecordFromRequest(Request $request, $id, ?callable $onBefo $keys = array_keys($input); foreach ($keys as $key) { - if (!$this->isFillable($key) && !$this->isRelation($key) && !$this->isRelation(Str::camel($key)) && !$this->isFilterParam($key) && !in_array($key, $this->appends ?? [])) { + if ($this->isInvalidUpdateParam($key)) { throw new \Exception('Invalid param "' . $key . '" in update request!'); } } @@ -924,4 +924,25 @@ public function isColumn(string $columnName): bool { return Schema::hasColumn($this->getTable(), $columnName); } + + /** + * Determines whether a given parameter key is invalid for an update operation. + * + * This function checks if the provided key is not one of the timestamp fields, not a fillable attribute, + * not a relation (either in its given form or in its camel case equivalent), not a filter parameter, + * and not an appended attribute. + * + * @param string $key The parameter key to evaluate. + * @return bool Returns true if the key is not valid for updating; false otherwise. + */ + public function isInvalidUpdateParam(string $key): bool + { + $isNotTimestamp = !in_array($key, ['created_at', 'updated_at', 'deleted_at']); + $isNotFillable = !$this->isFillable($key); + $isNotRelation = !$this->isRelation($key) && !$this->isRelation(Str::camel($key)); + $isNotFilterParam = !$this->isFilterParam($key); + $isNotAppenededAttribute = !in_array($key, $this->appends ?? []); + + return $isNotTimestamp && $isNotFillable && $isNotRelation && $isNotFilterParam && $isNotAppenededAttribute; + } }