Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Jun 14, 2023
1 parent c08873f commit 984225f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/Http/Resources/FleetbaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
23 changes: 22 additions & 1 deletion src/Traits/HasApiModelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 984225f

Please sign in to comment.