-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseTransformer.php
55 lines (47 loc) · 1.17 KB
/
BaseTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php namespace App\Http\Transformers;
use App\Services\TransformerService;
use League\Fractal\TransformerAbstract;
use Spira\Repository\Collection\Collection;
use Spira\Responder\Contract\TransformerInterface;
abstract class BaseTransformer extends TransformerAbstract implements TransformerInterface
{
/**
* @var TransformerService
*/
private $service;
public function __construct(TransformerService $service)
{
$this->service = $service;
}
/**
* @param $object
* @return mixed
*/
abstract public function transform($object);
/**
* @return TransformerService
*/
public function getService()
{
return $this->service;
}
/**
* @param $collection
* @return mixed
*/
public function transformCollection($collection)
{
if ($collection instanceof Collection) {
$collection = $collection->all();
}
return $this->getService()->collection($collection, $this);
}
/**
* @param $item
* @return mixed
*/
public function transformItem($item)
{
return $this->getService()->item($item, $this);
}
}