|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use Flugg\Responder\Pagination\CursorPaginator; |
|
6
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
13
|
|
|
use Illuminate\Database\Query\Builder; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class is responsible for normalizing resource data. |
|
17
|
|
|
* |
|
18
|
|
|
* @package flugger/laravel-responder |
|
19
|
|
|
* @author Alexander Tømmerås <[email protected]> |
|
20
|
|
|
* @license The MIT License |
|
21
|
|
|
*/ |
|
22
|
|
|
class DataNormalizer |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Normalize the data for a resource. |
|
26
|
|
|
* |
|
27
|
|
|
* @param mixed $data |
|
28
|
|
|
* @return mixed |
|
29
|
6 |
|
*/ |
|
30
|
|
|
public function normalize($data = null) |
|
31
|
6 |
|
{ |
|
32
|
2 |
|
if ($this->isInstanceOf($data, [Builder::class, EloquentBuilder::class, CursorPaginator::class])) { |
|
33
|
|
|
return $data->get(); |
|
34
|
1 |
|
} elseif ($data instanceof Paginator) { |
|
35
|
|
|
return $data->getCollection(); |
|
36
|
2 |
|
} elseif ($data instanceof Relation) { |
|
37
|
|
|
return $this->normalizeRelation($data); |
|
38
|
|
|
} |
|
39
|
1 |
|
|
|
40
|
|
|
return $data; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Normalize a relationship. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
|
47
|
|
|
* @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|null |
|
48
|
2 |
|
*/ |
|
49
|
|
|
protected function normalizeRelation(Relation $relation) |
|
50
|
2 |
|
{ |
|
51
|
|
|
if ($this->isInstanceOf($data, [BelongsTo::class, HasOne::class, MorphOne::class, MorphTo::class])) { |
|
|
|
|
|
|
52
|
|
|
return $relation->first(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $relation->get(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
2 |
|
* Indicates if the given data is an instance of any of the given class names. |
|
60
|
|
|
* |
|
61
|
2 |
|
* @param mixed $data |
|
62
|
|
|
* @param array $classes |
|
63
|
2 |
|
* @return bool |
|
64
|
2 |
|
*/ |
|
65
|
2 |
|
protected function isInstanceOf($data, array $classes): bool |
|
66
|
|
|
{ |
|
67
|
|
|
foreach ($classes as $class) { |
|
68
|
|
|
if ($data instanceof $class) { |
|
69
|
1 |
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.