1 | <?php |
||
22 | class DataNormalizer |
||
23 | { |
||
24 | /** |
||
25 | * Normalize the data for a resource. |
||
26 | * |
||
27 | * @param mixed $data |
||
28 | * @return mixed |
||
29 | */ |
||
30 | 63 | public function normalize($data = null) |
|
31 | { |
||
32 | 63 | if ($this->isInstanceOf($data, [Builder::class, EloquentBuilder::class, CursorPaginator::class])) { |
|
33 | 3 | return $data->get(); |
|
34 | 60 | } elseif ($data instanceof Paginator) { |
|
35 | 2 | return $data->getCollection(); |
|
36 | 58 | } elseif ($data instanceof Relation) { |
|
37 | 4 | return $this->normalizeRelation($data); |
|
38 | } |
||
39 | |||
40 | 54 | 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 | */ |
||
49 | 4 | protected function normalizeRelation(Relation $relation) |
|
57 | |||
58 | /** |
||
59 | * Indicates if the given data is an instance of any of the given class names. |
||
60 | * |
||
61 | * @param mixed $data |
||
62 | * @param array $classes |
||
63 | * @return bool |
||
64 | */ |
||
65 | 63 | protected function isInstanceOf($data, array $classes): bool |
|
75 | } |