Skip to content

Commit

Permalink
Add strong typing to RestfulModel and deprecate $localWith
Browse files Browse the repository at this point in the history
  • Loading branch information
specialtactics committed Jan 28, 2025
1 parent 5669d1b commit d0fb438
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Http/Controllers/RestfulChildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getOneFromParent($uuid, Request $request)

// Form model's with relations for parent query
$withArray = [];
foreach ($model::getItemWith() as $modelRelation) {
foreach ($model::$itemWith as $modelRelation) {
$withArray[] = $resourceRelationName . '.' . $modelRelation;
}

Expand Down
63 changes: 20 additions & 43 deletions src/Models/RestfulModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,36 @@ class RestfulModel extends Model
*
* @var array Attributes to disallow updating through an API update or put
*/
public $immutableAttributes = ['created_at', 'deleted_at'];

/**
* Acts like $with (eager loads relations), however only for immediate controller requests for that object
* This is useful if you want to use "with" for immediate resource routes, however don't want these relations
* always loaded in various service functions, for performance reasons
*
* @deprecated Use getItemWith() and getCollectionWith()
*
* @var array Relations to load implicitly by Restful controllers
*/
public static $localWith = null;
public array $immutableAttributes = ['created_at', 'updated_at', 'deleted_at'];

/**
* What relations should one model of this entity be returned with, from a relevant controller
*
* @var null|array
*/
public static $itemWith = [];
public static ?array $itemWith = [];

/**
* What relations should a collection of models of this entity be returned with, from a relevant controller
* If left null, then $itemWith will be used
*
* @var null|array
*/
public static $collectionWith = null;
public static ?array $collectionWith = null;

/**
* You can define a custom transformer for a model, if you wish to override the functionality of the Base transformer
*
* @var null|RestfulTransformer The transformer to use for this model, if overriding the default
*/
public static $transformer = null;
public static ?RestfulTransformer $transformer = null;

/**
* Return the validation rules for this model
*
* @return array Validation rules to be used for the model when creating it
*/
public function getValidationRules()
public function getValidationRules(): array
{
return [];
}
Expand All @@ -85,7 +74,7 @@ public function getValidationRules()
*
* @return array Validation roles to use for updating model
*/
public function getValidationRulesUpdating()
public function getValidationRulesUpdating(): array
{
return $this->getValidationRules();
}
Expand All @@ -95,7 +84,7 @@ public function getValidationRulesUpdating()
*
* @return array
*/
public function getValidationMessages()
public function getValidationMessages(): array
{
return [];
}
Expand All @@ -105,7 +94,7 @@ public function getValidationMessages()
*
* Add various functionality in the model lifecycle hooks
*/
public static function boot()
public static function boot(): void
{
parent::boot();

Expand All @@ -130,7 +119,7 @@ public static function boot()
if (! empty($model->immutableAttributes)) {
// For each immutable attribute, check if they have changed
foreach ($model->immutableAttributes as $attributeName) {
if ($model->getOriginal($attributeName) != $model->getAttribute($attributeName)) {
if ($model->isDirty($attributeName)) {
throw new BadRequestHttpException('Updating the "'. APIBoilerplate::formatCaseAccordingToResponseFormat($attributeName) .'" attribute is not allowed.');
}
}
Expand All @@ -143,7 +132,7 @@ public static function boot()
*
* @return BaseTransformer
*/
public static function getTransformer()
public static function getTransformer(): RestfulTransformer
{
return is_null(static::$transformer) ? new BaseTransformer : new static::$transformer;
}
Expand All @@ -158,7 +147,7 @@ public static function getTransformer()
*
* @return void
*/
public function orderAttributesUuidFirst()
public function orderAttributesUuidFirst(): void
{
if ($this->getKeyName()) {
$UuidValue = $this->getKey();
Expand All @@ -168,37 +157,25 @@ public function orderAttributesUuidFirst()
}

/**
* If using deprecated $localWith then use that
* Otherwise, use $itemWith
*
* @return array
* @return array|null
*/
public static function getItemWith()
public static function getItemWith(): ?array
{
if (is_null(static::$localWith)) {
return static::$itemWith;
} else {
return static::$localWith;
}
return static::$itemWith;
}

/**
* If using deprecated $localWith then use that
* Otherwise, if collectionWith hasn't been set, use $itemWith by default
* If collectionWith hasn't been set, use $itemWith by default
* Otherwise, use collectionWith
*
* @return array
* @return array|null
*/
public static function getCollectionWith()
public static function getCollectionWith(): ?array
{
if (is_null(static::$localWith)) {
if (! is_null(static::$collectionWith)) {
return static::$collectionWith;
} else {
return static::$itemWith;
}
if (! is_null(static::$collectionWith)) {
return static::$collectionWith;
} else {
return static::$localWith;
return static::$itemWith;
}
}

Expand Down

0 comments on commit d0fb438

Please sign in to comment.