1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: arthur |
5
|
|
|
* Date: 29.10.18 |
6
|
|
|
* Time: 09:35. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Transformers; |
10
|
|
|
|
11
|
|
|
use Foundation\Contracts\Transformable; |
12
|
|
|
use Foundation\Exceptions\Exception; |
13
|
|
|
use Illuminate\Database\Eloquent\Collection; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Transformer. |
19
|
|
|
* |
20
|
|
|
* @method array transformResource |
21
|
|
|
* @property string $id |
22
|
|
|
* @property string $username |
23
|
|
|
* @property string $email |
24
|
|
|
* @property string $name |
25
|
|
|
* @property string $avatar |
26
|
|
|
* @property string $provider |
27
|
|
|
*/ |
28
|
|
|
abstract class Transformer extends JsonResource implements Transformable |
29
|
|
|
{ |
30
|
|
|
use IncludesRelations, HandlesLimit; |
31
|
|
|
|
32
|
|
|
public $include = []; |
33
|
|
|
|
34
|
|
|
public $available = []; |
35
|
|
|
|
36
|
|
|
public $limit = -1; |
37
|
|
|
|
38
|
21 |
|
public function __construct($resource, $relations = []) |
39
|
|
|
{ |
40
|
21 |
|
if (!($resource instanceof Model)) |
41
|
|
|
throw new Exception('Object passed to the transformer resource method is not a eloquent model', 500); |
42
|
21 |
|
$this->resource = $resource; |
43
|
21 |
|
$relations = is_array($relations) ? $relations : []; |
44
|
21 |
|
parent::__construct(self::loadRelations($resource, $relations)); |
45
|
21 |
|
} |
46
|
|
|
|
47
|
13 |
|
public static function resource($model, array $relations = []): self |
48
|
|
|
{ |
49
|
13 |
|
return new static($model, $relations); |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
public static function collection($resource, array $relations = []) |
53
|
|
|
{ |
54
|
10 |
|
if (!($resource instanceof Collection)) |
55
|
|
|
throw new Exception('Object passed to the transformer collection method is not a collection', 500); |
56
|
|
|
|
57
|
10 |
|
$resource = self::processLimit($resource); |
58
|
10 |
|
$resource = self::loadRelations($resource, $relations); |
59
|
10 |
|
return new AnonymousTransformerCollection($resource, static::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
4 |
|
public function serialize() |
64
|
|
|
{ |
65
|
4 |
|
return json_decode(json_encode($this->jsonSerialize()), true); |
66
|
|
|
} |
67
|
|
|
|
68
|
20 |
|
public function toArray($request) |
69
|
|
|
{ |
70
|
20 |
|
if (!method_exists($this, 'transformResource')) |
71
|
|
|
throw new \Exception("transformResource method not set on " . static::class, 500); |
72
|
20 |
|
return array_merge($this->transformResource($this->resource), $this->includeRelations()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.