-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Lomkit/feature/directives
Feature/instructions
- Loading branch information
Showing
24 changed files
with
1,144 additions
and
116 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Lomkit\Rest\Concerns; | ||
|
||
trait Fieldable | ||
{ | ||
/** | ||
* The fields. | ||
* | ||
* @param \Lomkit\Rest\Http\Requests\RestRequest $request | ||
* @return array | ||
*/ | ||
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* The fields. | ||
* | ||
* @param \Lomkit\Rest\Http\Requests\RestRequest $request | ||
* @param string $name | ||
* @return array | ||
*/ | ||
public function field(\Lomkit\Rest\Http\Requests\RestRequest $request, string $name) | ||
{ | ||
return collect($this->fields($request)) | ||
->first(function ($value, $fieldName) use ($name) { return $fieldName === $name; }); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Lomkit\Rest\Concerns; | ||
|
||
use Lomkit\Rest\Http\Resource; | ||
|
||
trait Resourcable | ||
{ | ||
/** | ||
* The resource. | ||
* | ||
* @var Resource | ||
*/ | ||
public Resource $resource; | ||
|
||
/** | ||
* Set the resource. | ||
* | ||
* @param Resource $resource | ||
* @return array | ||
*/ | ||
public function resource(Resource $resource) { | ||
return tap($this, function() use ($resource) { | ||
$this->resource = $resource; | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Lomkit\Rest\Instructions; | ||
|
||
use Illuminate\Support\Str; | ||
use Lomkit\Rest\Concerns\Fieldable; | ||
use Lomkit\Rest\Concerns\Makeable; | ||
use Lomkit\Rest\Concerns\Metable; | ||
use Lomkit\Rest\Concerns\Resourcable; | ||
use Lomkit\Rest\Http\Requests\RestRequest; | ||
use Lomkit\Rest\Query\Builder; | ||
|
||
class Instruction | ||
{ | ||
use Makeable, Metable, Fieldable, Resourcable; | ||
|
||
/** | ||
* The displayable name of the instruction. | ||
* | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* Get the name of the instruction. | ||
* | ||
* @return string | ||
*/ | ||
public function name() | ||
{ | ||
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Instruction')->snake(' ')->title(); | ||
} | ||
|
||
/** | ||
* Get the URI key for the instruction. | ||
* | ||
* @return string | ||
*/ | ||
public function uriKey() | ||
{ | ||
return Str::slug($this->name(), '-', null); | ||
} | ||
|
||
/** | ||
* Prepare the action for JSON serialization. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$request = app()->make(RestRequest::class); | ||
|
||
return [ | ||
'name' => $this->name(), | ||
'uriKey' => $this->uriKey(), | ||
'fields' => $this->fields($request), | ||
'meta' => $this->meta() | ||
]; | ||
} | ||
|
||
/** | ||
* Perform the instruction on the given query. | ||
* | ||
* @param array $fields | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @return void | ||
*/ | ||
public function handle(array $fields, \Illuminate\Database\Eloquent\Builder $query) | ||
{ | ||
// ... | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Lomkit\Rest\Instructions; | ||
|
||
use Lomkit\Rest\Http\Requests\RestRequest; | ||
use Lomkit\Rest\Instructions\Instruction; | ||
|
||
trait Instructionable | ||
{ | ||
/** | ||
* The instructions that should be linked | ||
* @param RestRequest $request | ||
* @return array | ||
*/ | ||
public function instructions(RestRequest $request): array { | ||
return []; | ||
} | ||
|
||
public function instructionExists(RestRequest $request, string $instructionKey): bool { | ||
return collect($this->instructions($request)) | ||
->contains(function (Instruction $instruction) use ($instructionKey) { | ||
return $instruction->uriKey() === $instructionKey; | ||
}); | ||
} | ||
|
||
public function instruction(RestRequest $request, string $instructionKey) { | ||
$instruction = collect($this->instructions($request)) | ||
->first(function (Instruction $instruction) use ($instructionKey) { | ||
return $instruction->uriKey() === $instructionKey; | ||
}); | ||
|
||
if (!is_null($instruction)) { | ||
$instruction | ||
->resource($this); | ||
} | ||
|
||
return $instruction; | ||
} | ||
} |
Oops, something went wrong.