Skip to content

Commit

Permalink
Merge pull request #27 from Lomkit/feature/rename-params
Browse files Browse the repository at this point in the history
♻️ removed params from functions name
  • Loading branch information
GautierDele authored Aug 30, 2023
2 parents ff3006b + 4061457 commit c42c50c
Show file tree
Hide file tree
Showing 32 changed files with 72 additions and 72 deletions.
28 changes: 14 additions & 14 deletions src/Concerns/Resource/ConfiguresRestParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
trait ConfiguresRestParameters
{
/**
* The exposed fields that could be provided
* The fields that could be provided
* @param RestRequest $request
* @return array
*/
public function exposedFields(RestRequest $request): array {
public function fields(RestRequest $request): array {
return [];
}

public function getNestedExposedFields(RestRequest $request, string $prefix = '', array $loadedRelations = []) {
public function getNestedFields(RestRequest $request, string $prefix = '', array $loadedRelations = []) {
if ($prefix !== '') {
$prefix = $prefix.'.';
}

$exposedFields = array_map(
function ($exposedField) use ($prefix) {
return $prefix.$exposedField;
$fields = array_map(
function ($field) use ($prefix) {
return $prefix.$field;
},
$this->exposedFields($request)
$this->fields($request)
);

foreach (
Expand All @@ -39,8 +39,8 @@ function ($exposedField) use ($prefix) {
) {
$loadedRelations[] = $relation->relation;
array_push(
$exposedFields,
...$relation->resource()->getNestedExposedFields($request, $prefix.$relation->relation,$loadedRelations),
$fields,
...$relation->resource()->getNestedFields($request, $prefix.$relation->relation,$loadedRelations),
// We push the pivot fields if they exists
...(
collect(method_exists($relation, 'getPivotFields') ? $relation->getPivotFields() : [])
Expand All @@ -49,24 +49,24 @@ function ($exposedField) use ($prefix) {
);
}

return $exposedFields;
return $fields;
}

/**
* The exposed scopes that could be provided
* The scopes that could be provided
* @param RestRequest $request
* @return array
*/
public function exposedScopes(RestRequest $request): array {
public function scopes(RestRequest $request): array {
return [];
}

/**
* The exposed limits that could be provided
* The limits that could be provided
* @param RestRequest $request
* @return array
*/
public function exposedLimits(RestRequest $request): array {
public function limits(RestRequest $request): array {
return [
10,
25,
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Resource/Relationable.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function nestedRelations(RestRequest $request, string $prefix = '', array
}

/**
* The exposed relations that could be provided
* The relations that could be provided
* @param RestRequest $request
* @return array
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Console/stubs/resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class {{ class }} extends RestResource
* @param RestRequest $request
* @return array
*/
public function exposedFields(\Lomkit\Rest\Http\Requests\RestRequest $request): array
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request): array
{
return [
'id'
Expand All @@ -40,7 +40,7 @@ class {{ class }} extends RestResource
* @param RestRequest $request
* @return array
*/
public function exposedScopes(\Lomkit\Rest\Http\Requests\RestRequest $request): array
public function scopes(\Lomkit\Rest\Http\Requests\RestRequest $request): array
{
return [];
}
Expand All @@ -50,7 +50,7 @@ class {{ class }} extends RestResource
* @param RestRequest $request
* @return array
*/
public function exposedLimits(\Lomkit\Rest\Http\Requests\RestRequest $request): array
public function limits(\Lomkit\Rest\Http\Requests\RestRequest $request): array
{
return [
10,
Expand Down
6 changes: 3 additions & 3 deletions src/Console/stubs/user-resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class User extends RestResource
* @param RestRequest $request
* @return array
*/
public function exposedFields(\Lomkit\Rest\Http\Requests\RestRequest $request)
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
return [
'id',
Expand All @@ -42,7 +42,7 @@ class User extends RestResource
* @param RestRequest $request
* @return array
*/
public function exposedScopes(\Lomkit\Rest\Http\Requests\RestRequest $request) {
public function scopes(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [];
}

Expand All @@ -51,7 +51,7 @@ class User extends RestResource
* @param RestRequest $request
* @return array
*/
public function exposedLimits(\Lomkit\Rest\Http\Requests\RestRequest $request) {
public function limits(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [
10,
25,
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/MutateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function mutateRules(Resource $resource, $prefix = 'mutate.*', $loadedRel
$prefix.'.attributes' => [
'prohibited_if:'.$prefix.'.operation,attach',
'prohibited_if:'.$prefix.'.operation,detach',
'array:'.Arr::join($resource->exposedFields($this), ',')
'array:'.Arr::join($resource->fields($this), ',')
],
$prefix.'.key' => [
'required_if:'.$prefix.'.operation,update',
Expand Down
10 changes: 5 additions & 5 deletions src/Http/Requests/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function searchRules(Resource $resource, $prefix = '', $isRootSearchRules
[$prefix.'instructions' => ['sometimes', 'array']],
$this->instructionsRules($resource, $prefix.'instructions'),
[
'limit' => ['sometimes', 'integer', Rule::in($resource->exposedLimits($this))],
'limit' => ['sometimes', 'integer', Rule::in($resource->limits($this))],
'page' => ['sometimes', 'integer']
],
$isRootSearchRules ? ['includes' => ['sometimes', 'array']] : [],
Expand All @@ -53,7 +53,7 @@ public function filtersRules(Resource $resource, string $prefix, $isMaxDepth = f
$rules = array_merge(
[
$prefix.'.*.field' => [
Rule::in($resource->getNestedExposedFields($this)),
Rule::in($resource->getNestedFields($this)),
"required_without:$prefix.*.nested",
'string'
],
Expand Down Expand Up @@ -86,7 +86,7 @@ public function filtersRules(Resource $resource, string $prefix, $isMaxDepth = f
protected function scopesRules(Resource $resource, string $prefix) {
$rules = [
$prefix.'.*.name' => [
Rule::in($resource->exposedScopes($this)),
Rule::in($resource->scopes($this)),
'required',
'string'
],
Expand Down Expand Up @@ -126,7 +126,7 @@ protected function instructionsRules(Resource $resource, string $prefix) {
protected function sortsRules(Resource $resource, string $prefix) {
$rules = [
$prefix.'.*.field' => [
Rule::in($resource->exposedFields($this)),
Rule::in($resource->fields($this)),
'required',
'string'
],
Expand All @@ -142,7 +142,7 @@ protected function sortsRules(Resource $resource, string $prefix) {
protected function selectsRules(Resource $resource, string $prefix) {
$rules = [
$prefix.'.*.field' => [
Rule::in($resource->exposedFields($this)),
Rule::in($resource->fields($this)),
'required',
'string'
]
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function jsonSerialize(): mixed
return [
'actions' => collect($this->actions($request))->map->jsonSerialize()->toArray(),
'instructions' => collect($this->instructions($request))->map->jsonSerialize()->toArray(),
'fields' => $this->exposedFields($request),
'limits' => $this->exposedLimits($request),
'scopes' => $this->exposedScopes($request),
'fields' => $this->fields($request),
'limits' => $this->limits($request),
'scopes' => $this->scopes($request),
'relations' => collect($this->relations($request))->map->jsonSerialize()->toArray(),
'rules' => [
'all' => $this->rules($request),
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function modelToResponse(Model $model, Resource $resource, array $request
array_merge(
isset($requestArray['selects']) ?
collect($requestArray['selects'])->pluck('field')->toArray() :
$resource->exposedFields(app()->make(RestRequest::class)),
$resource->fields(app()->make(RestRequest::class)),
// Here we add the aggregates
collect($requestArray['aggregates'] ?? [])
->map(function ($aggregate) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/AggregateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function buildValidationRules($attribute, $value)

return [
$attribute.'.field' => \Illuminate\Validation\Rule::in(
$relationResource->exposedFields(app()->make(RestRequest::class))
$relationResource->fields(app()->make(RestRequest::class))
)
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Controllers/AutomaticGatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel
->get()
->map(function ($relation) use ($matchingModel, $pivotAccessor) {
return collect($relation->only(
array_merge((new BelongsToManyResource)->exposedFields(app()->make(RestRequest::class)), [$pivotAccessor])
array_merge((new BelongsToManyResource)->fields(app()->make(RestRequest::class)), [$pivotAccessor])
))
->pipe(function ($relation) use ($matchingModel, $pivotAccessor) {
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function test_getting_a_list_of_resources_including_morph_to_relation():
new ModelResource,
[
[
'morph_to_relation' => $matchingModel->morphToRelation->only((new MorphToResource)->exposedFields(app()->make(RestRequest::class))),
'morph_to_relation' => $matchingModel->morphToRelation->only((new MorphToResource)->fields(app()->make(RestRequest::class))),
],
[
'morph_to_relation' => null,
Expand Down Expand Up @@ -111,7 +111,7 @@ public function test_getting_a_list_of_resources_including_morph_one_relation():
[
[
'morph_one_relation' => $matchingModel->morphOneRelation->only(
(new MorphOneResource)->exposedFields(app()->make(RestRequest::class))
(new MorphOneResource)->fields(app()->make(RestRequest::class))
),
],
[
Expand Down Expand Up @@ -152,7 +152,7 @@ public function test_getting_a_list_of_resources_including_morph_one_of_many_rel
[
[
'morph_one_of_many_relation' => $matchingModel->morphOneOfManyRelation->only(
(new MorphOneOfManyResource)->exposedFields(app()->make(RestRequest::class))
(new MorphOneOfManyResource)->fields(app()->make(RestRequest::class))
),
],
[
Expand Down Expand Up @@ -199,7 +199,7 @@ public function test_getting_a_list_of_resources_including_morph_many_relation()
->get()
->map(function ($relation) {
return $relation->only(
(new MorphManyResource)->exposedFields(app()->make(RestRequest::class))
(new MorphManyResource)->fields(app()->make(RestRequest::class))
);
})->toArray(),
],
Expand Down Expand Up @@ -245,7 +245,7 @@ public function test_getting_a_list_of_resources_including_morph_to_many_relatio
->get()
->map(function ($relation) use ($matchingModel, $pivotAccessor) {
return collect($relation->only(
array_merge((new MorphToManyResource)->exposedFields(app()->make(RestRequest::class)), [$pivotAccessor])
array_merge((new MorphToManyResource)->fields(app()->make(RestRequest::class)), [$pivotAccessor])
))
->pipe(function ($relation) use ($matchingModel, $pivotAccessor) {
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
Expand Down Expand Up @@ -299,7 +299,7 @@ public function test_getting_a_list_of_resources_including_morphed_by_many_relat
->get()
->map(function ($relation) use ($matchingModel, $pivotAccessor) {
return collect($relation->only(
array_merge((new MorphedByManyResource)->exposedFields(app()->make(RestRequest::class)), [$pivotAccessor])
array_merge((new MorphedByManyResource)->fields(app()->make(RestRequest::class)), [$pivotAccessor])
))
->pipe(function ($relation) use ($matchingModel, $pivotAccessor) {
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function test_getting_a_list_of_resources_including_belongs_to_relation()
new ModelResource,
[
[
'belongs_to_relation' => $matchingModel->belongsToRelation->only((new BelongsToResource)->exposedFields(app()->make(RestRequest::class))),
'belongs_to_relation' => $matchingModel->belongsToRelation->only((new BelongsToResource)->fields(app()->make(RestRequest::class))),
],
[
'belongs_to_relation' => null,
Expand Down Expand Up @@ -116,11 +116,11 @@ public function test_getting_a_list_of_resources_including_belongs_to_has_many_r
[
'belongs_to_relation' => array_merge(
$matchingModelBelongsToRelation
->only((new BelongsToResource)->exposedFields(app()->make(RestRequest::class))),
->only((new BelongsToResource)->fields(app()->make(RestRequest::class))),
[
'models' => $matchingModelBelongsToRelation->models
->map(function($model) {
return $model->only((new ModelResource)->exposedFields(app()->make(RestRequest::class)));
return $model->only((new ModelResource)->fields(app()->make(RestRequest::class)));
})
->toArray(),
]
Expand Down Expand Up @@ -164,7 +164,7 @@ public function test_getting_a_list_of_resources_including_has_one_relation(): v
[
[
'has_one_relation' => $matchingModel->hasOneRelation->only(
(new HasOneResource)->exposedFields(app()->make(RestRequest::class))
(new HasOneResource)->fields(app()->make(RestRequest::class))
),
],
[
Expand Down Expand Up @@ -205,7 +205,7 @@ public function test_getting_a_list_of_resources_including_has_one_of_many_relat
[
[
'has_one_of_many_relation' => $matchingModel->hasOneOfManyRelation->only(
(new HasOneOfManyResource)->exposedFields(app()->make(RestRequest::class))
(new HasOneOfManyResource)->fields(app()->make(RestRequest::class))
),
],
[
Expand Down Expand Up @@ -252,7 +252,7 @@ public function test_getting_a_list_of_resources_including_has_many_relation():
->get()
->map(function ($relation) {
return $relation->only(
(new HasManyResource)->exposedFields(app()->make(RestRequest::class))
(new HasManyResource)->fields(app()->make(RestRequest::class))
);
})->toArray(),
],
Expand Down Expand Up @@ -298,7 +298,7 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
->get()
->map(function ($relation) use ($matchingModel, $pivotAccessor) {
return collect($relation->only(
array_merge((new BelongsToManyResource)->exposedFields(app()->make(RestRequest::class)), [$pivotAccessor])
array_merge((new BelongsToManyResource)->fields(app()->make(RestRequest::class)), [$pivotAccessor])
))
->pipe(function ($relation) use ($matchingModel, $pivotAccessor) {
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function test_getting_a_list_of_resources_including_has_one_through_relat
new ModelResource,
[
[
'has_one_through_relation' => $matchingModel->hasOneThroughRelation->only((new HasOneThroughResource())->exposedFields(app()->make(RestRequest::class))),
'has_one_through_relation' => $matchingModel->hasOneThroughRelation->only((new HasOneThroughResource())->fields(app()->make(RestRequest::class))),
],
[
'has_one_through_relation' => null,
Expand Down Expand Up @@ -105,7 +105,7 @@ public function test_getting_a_list_of_resources_including_has_many_through_rela
[
'has_many_through_relation' => $matchingModel->hasManyThroughRelation->map(function ($relation) {
return $relation->only(
(new HasManyThroughResource)->exposedFields(app()->make(RestRequest::class))
(new HasManyThroughResource)->fields(app()->make(RestRequest::class))
);
})->toArray(),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rest/Resources/AutomaticGatingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function relations(RestRequest $request): array
];
}

public function exposedFields(RestRequest $request): array
public function fields(RestRequest $request): array
{
return [
'id',
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rest/Resources/BelongsToManyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function relations(RestRequest $request): array
return [];
}

public function exposedFields(RestRequest $request): array
public function fields(RestRequest $request): array
{
return [
'id',
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rest/Resources/BelongsToResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function relations(RestRequest $request): array
];
}

public function exposedFields(RestRequest $request): array
public function fields(RestRequest $request): array
{
return [
'id',
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rest/Resources/HasManyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function relations(RestRequest $request): array
return [];
}

public function exposedFields(RestRequest $request): array
public function fields(RestRequest $request): array
{
return [
'id',
Expand Down
Loading

0 comments on commit c42c50c

Please sign in to comment.