1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ViewModels; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Illuminate\Http\JsonResponse; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
12
|
|
|
use Illuminate\Contracts\Support\Responsable; |
13
|
|
|
use ReflectionProperty; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
|
16
|
|
|
class ViewModel implements Arrayable, Responsable |
17
|
|
|
{ |
18
|
|
|
protected $ignore = []; |
19
|
|
|
|
20
|
|
|
protected $view = ''; |
21
|
|
|
|
22
|
|
|
public function toArray(): array |
23
|
|
|
{ |
24
|
|
|
return $this |
25
|
|
|
->items() |
26
|
|
|
->all(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function toResponse($request): Response |
30
|
|
|
{ |
31
|
|
|
if ($request->wantsJson()) { |
32
|
|
|
return new JsonResponse($this->items()->toJson()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($this->view) { |
36
|
|
|
return response()->view($this->view, $this); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return new JsonResponse($this->items()->toJson()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function view(string $view): ViewModel |
43
|
|
|
{ |
44
|
|
|
$this->view = $view; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function items(): Collection |
50
|
|
|
{ |
51
|
|
|
$class = new ReflectionClass($this); |
52
|
|
|
|
53
|
|
|
$publicProperties = collect($class->getProperties(ReflectionProperty::IS_PUBLIC)) |
54
|
|
|
->reject(function (ReflectionProperty $property) { |
55
|
|
|
return $this->shouldIgnore($property->getName()); |
56
|
|
|
}) |
57
|
|
|
->mapWithKeys(function (ReflectionProperty $property) { |
58
|
|
|
return [$property->getName() => $this->{$property->getName()}]; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$publicMethods = collect($class->getMethods(ReflectionMethod::IS_PUBLIC)) |
62
|
|
|
->reject(function (ReflectionMethod $method) { |
63
|
|
|
return $this->shouldIgnore($method->getName()); |
64
|
|
|
}) |
65
|
|
|
->mapWithKeys(function (ReflectionMethod $method) { |
66
|
|
|
return [$method->getName() => $this->createVariableFromMethod($method)]; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
return $publicProperties->merge($publicMethods); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function shouldIgnore(string $methodName): bool |
73
|
|
|
{ |
74
|
|
|
if (Str::startsWith($methodName, '__')) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return in_array($methodName, $this->ignoredMethods()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function ignoredMethods(): array |
82
|
|
|
{ |
83
|
|
|
return array_merge([ |
84
|
|
|
'toArray', |
85
|
|
|
'toResponse', |
86
|
|
|
'view', |
87
|
|
|
], $this->ignore); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function createVariableFromMethod(ReflectionMethod $method) |
91
|
|
|
{ |
92
|
|
|
if ($method->getNumberOfParameters() === 0) { |
93
|
|
|
return $this->{$method->getName()}(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return Closure::fromCallable([$this, $method->getName()]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: