Skip to content

Commit d0fb438

Browse files
Add strong typing to RestfulModel and deprecate $localWith
1 parent 5669d1b commit d0fb438

File tree

2 files changed

+21
-44
lines changed

2 files changed

+21
-44
lines changed

src/Http/Controllers/RestfulChildController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function getOneFromParent($uuid, Request $request)
9696

9797
// Form model's with relations for parent query
9898
$withArray = [];
99-
foreach ($model::getItemWith() as $modelRelation) {
99+
foreach ($model::$itemWith as $modelRelation) {
100100
$withArray[] = $resourceRelationName . '.' . $modelRelation;
101101
}
102102

src/Models/RestfulModel.php

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,36 @@ class RestfulModel extends Model
3434
*
3535
* @var array Attributes to disallow updating through an API update or put
3636
*/
37-
public $immutableAttributes = ['created_at', 'deleted_at'];
38-
39-
/**
40-
* Acts like $with (eager loads relations), however only for immediate controller requests for that object
41-
* This is useful if you want to use "with" for immediate resource routes, however don't want these relations
42-
* always loaded in various service functions, for performance reasons
43-
*
44-
* @deprecated Use getItemWith() and getCollectionWith()
45-
*
46-
* @var array Relations to load implicitly by Restful controllers
47-
*/
48-
public static $localWith = null;
37+
public array $immutableAttributes = ['created_at', 'updated_at', 'deleted_at'];
4938

5039
/**
5140
* What relations should one model of this entity be returned with, from a relevant controller
5241
*
5342
* @var null|array
5443
*/
55-
public static $itemWith = [];
44+
public static ?array $itemWith = [];
5645

5746
/**
5847
* What relations should a collection of models of this entity be returned with, from a relevant controller
5948
* If left null, then $itemWith will be used
6049
*
6150
* @var null|array
6251
*/
63-
public static $collectionWith = null;
52+
public static ?array $collectionWith = null;
6453

6554
/**
6655
* You can define a custom transformer for a model, if you wish to override the functionality of the Base transformer
6756
*
6857
* @var null|RestfulTransformer The transformer to use for this model, if overriding the default
6958
*/
70-
public static $transformer = null;
59+
public static ?RestfulTransformer $transformer = null;
7160

7261
/**
7362
* Return the validation rules for this model
7463
*
7564
* @return array Validation rules to be used for the model when creating it
7665
*/
77-
public function getValidationRules()
66+
public function getValidationRules(): array
7867
{
7968
return [];
8069
}
@@ -85,7 +74,7 @@ public function getValidationRules()
8574
*
8675
* @return array Validation roles to use for updating model
8776
*/
88-
public function getValidationRulesUpdating()
77+
public function getValidationRulesUpdating(): array
8978
{
9079
return $this->getValidationRules();
9180
}
@@ -95,7 +84,7 @@ public function getValidationRulesUpdating()
9584
*
9685
* @return array
9786
*/
98-
public function getValidationMessages()
87+
public function getValidationMessages(): array
9988
{
10089
return [];
10190
}
@@ -105,7 +94,7 @@ public function getValidationMessages()
10594
*
10695
* Add various functionality in the model lifecycle hooks
10796
*/
108-
public static function boot()
97+
public static function boot(): void
10998
{
11099
parent::boot();
111100

@@ -130,7 +119,7 @@ public static function boot()
130119
if (! empty($model->immutableAttributes)) {
131120
// For each immutable attribute, check if they have changed
132121
foreach ($model->immutableAttributes as $attributeName) {
133-
if ($model->getOriginal($attributeName) != $model->getAttribute($attributeName)) {
122+
if ($model->isDirty($attributeName)) {
134123
throw new BadRequestHttpException('Updating the "'. APIBoilerplate::formatCaseAccordingToResponseFormat($attributeName) .'" attribute is not allowed.');
135124
}
136125
}
@@ -143,7 +132,7 @@ public static function boot()
143132
*
144133
* @return BaseTransformer
145134
*/
146-
public static function getTransformer()
135+
public static function getTransformer(): RestfulTransformer
147136
{
148137
return is_null(static::$transformer) ? new BaseTransformer : new static::$transformer;
149138
}
@@ -158,7 +147,7 @@ public static function getTransformer()
158147
*
159148
* @return void
160149
*/
161-
public function orderAttributesUuidFirst()
150+
public function orderAttributesUuidFirst(): void
162151
{
163152
if ($this->getKeyName()) {
164153
$UuidValue = $this->getKey();
@@ -168,37 +157,25 @@ public function orderAttributesUuidFirst()
168157
}
169158

170159
/**
171-
* If using deprecated $localWith then use that
172-
* Otherwise, use $itemWith
173-
*
174-
* @return array
160+
* @return array|null
175161
*/
176-
public static function getItemWith()
162+
public static function getItemWith(): ?array
177163
{
178-
if (is_null(static::$localWith)) {
179-
return static::$itemWith;
180-
} else {
181-
return static::$localWith;
182-
}
164+
return static::$itemWith;
183165
}
184166

185167
/**
186-
* If using deprecated $localWith then use that
187-
* Otherwise, if collectionWith hasn't been set, use $itemWith by default
168+
* If collectionWith hasn't been set, use $itemWith by default
188169
* Otherwise, use collectionWith
189170
*
190-
* @return array
171+
* @return array|null
191172
*/
192-
public static function getCollectionWith()
173+
public static function getCollectionWith(): ?array
193174
{
194-
if (is_null(static::$localWith)) {
195-
if (! is_null(static::$collectionWith)) {
196-
return static::$collectionWith;
197-
} else {
198-
return static::$itemWith;
199-
}
175+
if (! is_null(static::$collectionWith)) {
176+
return static::$collectionWith;
200177
} else {
201-
return static::$localWith;
178+
return static::$itemWith;
202179
}
203180
}
204181

0 commit comments

Comments
 (0)