-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Definition with related. * Apply fixes from StyleCI (#313) * wip * Apply fixes from StyleCI (#314) * wip * Apply fixes from StyleCI (#315) * Matches improvement. * Apply fixes from StyleCI (#317)
- Loading branch information
Showing
12 changed files
with
181 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Eager; | ||
|
||
use Binaryk\LaravelRestify\Fields\EagerField; | ||
use Binaryk\LaravelRestify\Repositories\Repository; | ||
use Binaryk\LaravelRestify\Traits\Make; | ||
|
||
class Related | ||
{ | ||
use Make; | ||
|
||
private string $relation; | ||
|
||
private ?EagerField $field; | ||
|
||
public function __construct(string $relation, EagerField $field = null) | ||
{ | ||
$this->relation = $relation; | ||
$this->field = $field; | ||
} | ||
|
||
public function isEager(): bool | ||
{ | ||
return ! is_null($this->field); | ||
} | ||
|
||
public function getRelation(): string | ||
{ | ||
return $this->relation; | ||
} | ||
|
||
public function resolveField(Repository $repository): EagerField | ||
{ | ||
return $this->field->resolve($repository); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Eager; | ||
|
||
use Binaryk\LaravelRestify\Fields\EagerField; | ||
use Binaryk\LaravelRestify\Fields\Field; | ||
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; | ||
use Illuminate\Support\Collection; | ||
|
||
class RelatedCollection extends Collection | ||
{ | ||
public function intoAssoc(): self | ||
{ | ||
return $this->mapWithKeys(function ($value, $key) { | ||
return [ | ||
is_numeric($key) ? $value : $key => $value, | ||
]; | ||
}); | ||
} | ||
|
||
public function forEager(RestifyRequest $request): self | ||
{ | ||
return $this->filter(fn ($value, $key) => $value instanceof EagerField) | ||
->filter(fn (Field $field) => $field->authorize($request)) | ||
->unique('attribute'); | ||
} | ||
|
||
public function inRequest(RestifyRequest $request): self | ||
{ | ||
return $this | ||
->filter(fn ($field, $key) => in_array($key, str_getcsv($request->input('related')))) | ||
->unique(); | ||
} | ||
|
||
public function mapIntoRelated(RestifyRequest $request) | ||
{ | ||
return $this->map(function ($value, $key) { | ||
return Related::make($key, $value instanceof EagerField ? $value : null); | ||
}); | ||
} | ||
|
||
public function authorized(RestifyRequest $request) | ||
{ | ||
return $this->intoAssoc() | ||
->filter(fn ($key, $value) => $key instanceof EagerField ? $key->authorize($request) : true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.