-
-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathCell.php
More file actions
183 lines (154 loc) · 4.44 KB
/
Cell.php
File metadata and controls
183 lines (154 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
namespace Orchid\Screen;
use Closure;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Orchid\Support\Blade;
abstract class Cell
{
use CanSee, Macroable;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $title;
/**
* @var Closure|null
*/
protected $render;
/**
* @var string
*/
protected $column;
/**
* @var string
*/
protected $popover;
/**
* Cell constructor.
*/
public function __construct(string $name)
{
$this->name = $name;
$this->column = $name;
}
/**
* @return static
*/
public static function make(string $name = '', ?string $title = null): static
{
$td = new static($name);
$td->column = $name;
$td->title = $title ?? Str::title($name);
return $td;
}
public function render(Closure $closure): static
{
$this->render = $closure;
return $this;
}
public function popover(string $text): static
{
$this->popover = $text;
return $this;
}
/**
* @throws \ReflectionException
*
* @return string
*/
protected function getNameParameterExpected(string $component, array $params = []): ?string
{
$class = new \ReflectionClass($component);
$parameters = optional($class->getConstructor())->getParameters() ?? [];
$paramsKeys = Arr::isAssoc($params) ? array_keys($params) : array_values($params);
return collect($parameters)
->filter(fn (\ReflectionParameter $parameter) => ! $parameter->isOptional())
->whenEmpty(fn () => collect($parameters))
->map(fn (\ReflectionParameter $parameter) => $parameter->getName())
->diff($paramsKeys)
->last();
}
/**
* @throws BindingResolutionException
* @throws \ReflectionException
*/
protected function renderComponent(string $component, $value, array $params = []): ?string
{
[$class, $view] = Blade::componentInfo($component);
if ($view === null) {
// for class based components, try to detect argument name
$nameArgument = $this->getNameParameterExpected($class, $params);
if ($nameArgument !== null) {
$params[$nameArgument] = $value;
}
}
$params = array_map(fn ($item) => value($item, $value), $params);
return Blade::renderComponent($component, $params);
}
/**
* Renders the component with optional parameters.
*
* @param string $component The component to render.
* @param mixed ...$params Optional parameters for the component.
*
* @return $this
*/
public function component(string $component, ...$params): static
{
/** Backward compatibility workaround.
*
* This block enables backward compatibility with previous versions
* where passing parameters as an array was supported.
*
* Example usage:
* TD::make()->component(Any::class, ['param' => 'value'])
*/
if (Arr::isList($params) && count($params) > 0) {
$params = Arr::first($params);
}
return $this->render(fn ($value) => $this->renderComponent($component, $value, $params));
}
/**
* Pass only the cell value to the component
*
* @throws \ReflectionException
*
* @return $this
*/
public function asComponent(string $component, array $params = []): static
{
return $this->render(fn ($value) => $this->renderComponent($component, $value->getContent($this->name), $params));
}
/**
* Pass only the cell value to the component
*
* @param string $component
* @param mixed ...$params
*
* @throws \ReflectionException
*
* @return $this
*/
public function usingComponent(string $component, ...$params): static
{
return $this->asComponent($component, $params);
}
/**
* @param Repository|Model $source
*
* @return mixed
*/
protected function handler($source, ?object $loop = null)
{
$callback = $this->render;
return is_null($callback) ? $source : $callback($source, $loop);
}
}