|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Fields\ValidationRules; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Chief\Media\Application\MediaRequest; |
|
6
|
|
|
|
|
7
|
|
|
class ImageFieldDimensionsRule extends AbstractMediaFieldRule |
|
8
|
|
|
{ |
|
9
|
|
|
public function validate($attribute, $value, $params, $validator): bool |
|
10
|
|
|
{ |
|
11
|
|
|
$value = $this->normalizePayload($value); |
|
12
|
|
|
|
|
13
|
|
|
foreach ([MediaRequest::NEW, MediaRequest::REPLACE] as $type) { |
|
14
|
|
|
foreach ($value[$type] as $file) { |
|
15
|
|
|
|
|
16
|
|
|
if ($file && false === $this->validateDimensions($attribute, $file, $params)) { |
|
17
|
|
|
|
|
18
|
|
|
$this->addCustomValidationMessage($attribute, $params, $validator); |
|
19
|
|
|
|
|
20
|
|
|
return false; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function validateDimensions($attribute, $value, $parameters) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->refersToExistingAsset($value)) { |
|
31
|
|
|
return $this->validateAssetDimensions($this->existingAsset($value), $parameters); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->validateSlimOutputDimensions($attribute, $value, $parameters); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Override Laravel validateDimensions to focus on the ImageField specifics |
|
39
|
|
|
*/ |
|
40
|
|
|
private function validateSlimOutputDimensions($attribute, $value, $parameters) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$file = json_decode($value)->output; |
|
43
|
|
|
|
|
44
|
|
|
$width = $file->width; |
|
45
|
|
|
$height = $file->height; |
|
46
|
|
|
|
|
47
|
|
|
return $this->dimensionsCheck($width, $height, $parameters); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $params |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
private function humanReadableParams($params): array |
|
55
|
|
|
{ |
|
56
|
|
|
$paramReplacements = [ |
|
57
|
|
|
'min_width' => 'minimum breedte: %spx', |
|
58
|
|
|
'max_width' => 'maximum breedte: %spx', |
|
59
|
|
|
'min_height' => 'minimum hoogte: %spx', |
|
60
|
|
|
'max_height' => 'maximum hoogte: %spx', |
|
61
|
|
|
'ratio' => 'verwachtte verhouding: %s', |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
$humanReadableParams = []; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($params as $param) { |
|
67
|
|
|
|
|
68
|
|
|
list($property, $value) = explode('=', $param); |
|
69
|
|
|
|
|
70
|
|
|
$humanReadableParams[] = isset($paramReplacements[$property]) |
|
71
|
|
|
? sprintf($paramReplacements[$property], $value) |
|
72
|
|
|
: $param; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $humanReadableParams; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $attribute |
|
80
|
|
|
* @param $params |
|
81
|
|
|
* @param $validator |
|
82
|
|
|
*/ |
|
83
|
|
|
private function addCustomValidationMessage($attribute, $params, $validator): void |
|
84
|
|
|
{ |
|
85
|
|
|
$validator->setCustomMessages([ |
|
86
|
|
|
'imagefield_dimensions' => 'De :attribute heeft niet de juiste afmetingen: ' . implode(', ', $this->humanReadableParams($params)), |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
if (!isset($validator->customAttributes[$attribute])) { |
|
90
|
|
|
$validator->addCustomAttributes([ |
|
91
|
|
|
$attribute => 'afbeelding', |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.