|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Limsum\UrlMakers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Limsum\Contracts\LimsumUrlMaker; |
|
7
|
|
|
use Limsum\Http\Controllers\ColorBlockController; |
|
8
|
|
|
|
|
9
|
|
|
class ColorBlockUrl implements LimsumUrlMaker |
|
10
|
|
|
{ |
|
11
|
|
|
use HasSize, HasExtension; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Image color. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected string $color; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Supported extensions. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected array $allowedExtensions = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Predefined Types. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected array $types = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initial Config. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected array $config = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
*/ |
|
44
|
12 |
|
public function __construct(array $config) |
|
45
|
|
|
{ |
|
46
|
12 |
|
$this->config = $config; |
|
47
|
12 |
|
$this->reset(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Reset to initial state. |
|
52
|
|
|
* |
|
53
|
|
|
* @return static |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public function reset(): static |
|
56
|
|
|
{ |
|
57
|
12 |
|
$this->width = (float) Arr::get($this->config, 'default.width', 100); |
|
58
|
12 |
|
$this->height = (float) Arr::get($this->config, 'default.height', 100); |
|
59
|
12 |
|
$this->color = (string) Arr::get($this->config, 'default.color', '#808080'); |
|
60
|
12 |
|
$this->extension = (string) Arr::get($this->config, 'default.extension', 'svg'); |
|
61
|
12 |
|
$this->allowedExtensions = (array) Arr::get($this->config, 'extensions', []); |
|
62
|
12 |
|
$this->types = (array) Arr::get($this->config, 'types', []); |
|
63
|
|
|
|
|
64
|
12 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
*/ |
|
70
|
12 |
|
public function url(array|string $params = [], bool $reset = true): string |
|
71
|
|
|
{ |
|
72
|
12 |
|
if (is_string($params)) { |
|
|
|
|
|
|
73
|
1 |
|
$this->type($params); |
|
74
|
|
|
} |
|
75
|
12 |
|
if (!in_array($this->extension, $this->allowedExtensions)) { |
|
76
|
1 |
|
throw new \InvalidArgumentException("Extension [{$this->extension}] not allowed."); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
12 |
|
$url = route(config('lorem-image.route.name_prefix') . '.color-block', $this->routeParams(is_array($params) ? $params : [])); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
12 |
|
if ($reset) { |
|
82
|
10 |
|
$this->reset(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
12 |
|
return $url; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param array $params |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
12 |
|
protected function routeParams(array $params = []): array |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
12 |
|
'extension' => $params['extension'] ?? $this->extension, |
|
97
|
12 |
|
ColorBlockController::$withParameterName => $params['width'] ?? $this->width, |
|
98
|
12 |
|
ColorBlockController::$heightParameterName => $params['height'] ?? $this->height, |
|
99
|
12 |
|
ColorBlockController::$colorParameterName => $params['color'] ?? $this->color, |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $color |
|
105
|
|
|
* |
|
106
|
|
|
* @return static |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function color(string $color): static |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->color = $color; |
|
111
|
|
|
|
|
112
|
1 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return static |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function black(): static |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->color('#000000'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return static |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function white(): static |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->color('#FFFFFF'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return static |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function svg(): static |
|
135
|
|
|
{ |
|
136
|
1 |
|
return $this->extension(__FUNCTION__); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return static |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function png(): static |
|
143
|
|
|
{ |
|
144
|
2 |
|
return $this->extension(__FUNCTION__); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return static |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function jpg(): static |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->extension(__FUNCTION__); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $type |
|
157
|
|
|
* |
|
158
|
|
|
* @return static |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function type(string $type): static |
|
161
|
|
|
{ |
|
162
|
1 |
|
if (!isset($this->types[ $type ]) || !is_array($data = $this->types[ $type ])) { |
|
163
|
1 |
|
throw new \InvalidArgumentException("Type [{$type}] not supported."); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
foreach ( |
|
167
|
|
|
[ |
|
168
|
1 |
|
'width', |
|
169
|
|
|
'height', |
|
170
|
|
|
'color', |
|
171
|
|
|
'extension', |
|
172
|
|
|
] as $key |
|
173
|
|
|
) { |
|
174
|
1 |
|
if (isset($data[ $key ])) { |
|
175
|
1 |
|
$this->$key = $data[ $key ]; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
1 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|