Skip to content

Commit

Permalink
Working on Resources.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Nov 9, 2017
1 parent a31e149 commit c9d720d
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Http/Resources/Json/AnonymousResourceCollection.php
@@ -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);
}
}
42 changes: 42 additions & 0 deletions src/Http/Resources/Json/Resource.php
@@ -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;
}
}
30 changes: 30 additions & 0 deletions src/Http/Resources/Json/ResourceCollection.php
@@ -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;
}
}
109 changes: 109 additions & 0 deletions src/Http/Resources/Json/Transformable.php
@@ -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;
}
21 changes: 21 additions & 0 deletions src/Http/Transformer.php 100644 → 100755
Expand Up @@ -8,6 +8,13 @@ abstract class Transformer extends BaseTransformer
{
use Traits\Transformable;

/**
* Resource instance.
*
* @var Orchestra\Http\Resources\Json\Resource
*/
protected $resource;

/**
* Invoke the transformation.
*
Expand All @@ -21,6 +28,20 @@ public static function with($instance, array $options = [])
return (new static())->options($options)->handle($instance);
}

/**
* Set resource.
*
* @param \Orchestra\Http\Resources\Json|resource $resource
*
* @return $this
*/
public function resource($resource)
{
$this->resource = $resource;

return $this;
}

/**
* Invoke the transformer.
*
Expand Down

0 comments on commit c9d720d

Please sign in to comment.