Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
- Loading branch information
Showing
5 changed files
with
230 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Orchestra\Http\Resources\Json; | ||
|
||
class AnonymousResourceCollection extends ResourceCollection | ||
{ | ||
/** | ||
* The name of the resource being collected. | ||
* | ||
* @var string | ||
*/ | ||
public $collects; | ||
|
||
/** | ||
* Create a new anonymous resource collection. | ||
* | ||
* @param mixed $resource | ||
* @param string $collects | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($resource, string $collects) | ||
{ | ||
$this->collects = $collects; | ||
|
||
parent::__construct($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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Orchestra\Http\Resources\Json; | ||
|
||
use Illuminate\Http\Resources\Json\Resource as BaseResource; | ||
|
||
class Resource extends BaseResource | ||
{ | ||
use Tranformable; | ||
|
||
/** | ||
* Create new anonymous resource collection. | ||
* | ||
* @param mixed $resource | ||
* | ||
* @return \Orchestra\Http\Resources\Json\AnonymousResourceCollection | ||
*/ | ||
public static function collection($resource) | ||
{ | ||
return new AnonymousResourceCollection($resource, get_called_class()); | ||
} | ||
|
||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return mixed | ||
*/ | ||
public function toArray($request) | ||
{ | ||
if (method_exists($request, 'version')) { | ||
$this->version($request->version()); | ||
} | ||
|
||
if (is_null($transformed = $this->toArrayUsingTransformer($request))) { | ||
return parent::toArray($request); | ||
} | ||
|
||
return $transformed; | ||
} | ||
} |
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 Orchestra\Http\Resources\Json; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection as BaseResource; | ||
|
||
class ResourceCollection extends BaseResource | ||
{ | ||
use Transformable; | ||
|
||
/** | ||
* Transform the resource into a JSON array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return mixed | ||
*/ | ||
public function toArray($request) | ||
{ | ||
if (method_exists($request, 'version')) { | ||
$this->version($request->version()); | ||
} | ||
|
||
if (is_null($transformed = $this->toArrayUsingTransformer($request))) { | ||
return parent::toArray($request); | ||
} | ||
|
||
return $transformed; | ||
} | ||
} |
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,109 @@ | ||
<?php | ||
|
||
namespace Orchestra\Http\Resources\Json; | ||
|
||
trait Transformable | ||
{ | ||
/** | ||
* Transformer name. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $transformer; | ||
|
||
/** | ||
* Version code. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $version; | ||
|
||
/** | ||
* Set transformer name. | ||
* | ||
* @param string|null $transformer | ||
* | ||
* @return $this | ||
*/ | ||
public function transformer(string $transformer = null): self | ||
{ | ||
$this->transformer = $transformer; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set version. | ||
* | ||
* @param string|null $version | ||
* | ||
* @return $this | ||
*/ | ||
public function version(string $version = null): self | ||
{ | ||
$this->version = $version; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get transformer. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getTransformer() | ||
{ | ||
return $this->transformer; | ||
} | ||
|
||
/** | ||
* Get version. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getVersion() | ||
{ | ||
return $this->version; | ||
} | ||
|
||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return array|null | ||
*/ | ||
public function toArrayUsingTransformer($request) | ||
{ | ||
$transformer = $this->resolveTransformer(); | ||
|
||
if (class_exists($transformer, false)) { | ||
$service = new $transformer($request); | ||
|
||
if ($service instanceof Transformer) { | ||
return $service->fromResource($this)->handle($this->instance); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Resolve transformer. | ||
* | ||
* @return string | ||
*/ | ||
protected function resolveTransformer(): string | ||
{ | ||
$transformer = $this->transformer ?? class_basename($this->instance); | ||
|
||
return app('orchestra.http.version')->resolve( | ||
$this->getNamespace(), $this->version, $transformer | ||
); | ||
} | ||
|
||
/** | ||
* Get namespace. | ||
* | ||
* @return string | ||
*/ | ||
abstract public function getNamespace(): string; | ||
} |
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