|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Signifly\Responder\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use Signifly\Responder\Contracts\ModelResolver as Contract; |
|
13
|
|
|
|
|
14
|
|
|
class ModelResolver implements Contract |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Resolve the model from data for a given type. |
|
18
|
|
|
* |
|
19
|
|
|
* @param mixed $data |
|
20
|
|
|
* @param string $type |
|
21
|
|
|
* @return string|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public function resolve($data, string $type): ?string |
|
24
|
|
|
{ |
|
25
|
|
|
$methodName = 'resolveFor'.Str::studly($type); |
|
26
|
|
|
if (method_exists($this, $methodName)) { |
|
27
|
|
|
return $this->$methodName($data); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
throw new InvalidArgumentException( |
|
31
|
|
|
sprintf('The type `%s` is invalid for resolving a model.', $type) |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Guard against invalid item. |
|
37
|
|
|
* |
|
38
|
|
|
* @param mixed $item |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function guardAgainstInvalidItem($item): void |
|
42
|
|
|
{ |
|
43
|
|
|
if (! $item instanceof Model) { |
|
44
|
|
|
throw new Exception('The collection should consists of models.'); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Resolve for an array. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $data |
|
52
|
|
|
* @return string|null |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function resolveForArray(array $data): ?string |
|
55
|
|
|
{ |
|
56
|
|
|
if (empty($data)) { |
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$item = Arr::first($data); |
|
61
|
|
|
|
|
62
|
|
|
return $this->resolveItem($item); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Resolve for a collection. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Illuminate\Support\Collection $data |
|
69
|
|
|
* @return string|null |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function resolveForCollection(Collection $data): ?string |
|
72
|
|
|
{ |
|
73
|
|
|
if ($data->isEmpty()) { |
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$item = $data->first(); |
|
78
|
|
|
|
|
79
|
|
|
return $this->resolveItem($item); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Resolve for paginator. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $data |
|
86
|
|
|
* @return string|null |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function resolveForPaginator(LengthAwarePaginator $data): ?string |
|
89
|
|
|
{ |
|
90
|
|
|
if ($data->isEmpty()) { |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$item = $data->getCollection()->first(); |
|
95
|
|
|
|
|
96
|
|
|
return $this->resolveItem($item); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Resolve an item. |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $item |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function resolveItem($item): string |
|
106
|
|
|
{ |
|
107
|
|
|
$this->guardAgainstInvalidItem($item); |
|
108
|
|
|
|
|
109
|
|
|
return get_class($item); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|