|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Fields\ValidationRules; |
|
5
|
|
|
|
|
6
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
|
7
|
|
|
|
|
8
|
|
|
trait ValidatesExistingAssetAttributes |
|
9
|
|
|
{ |
|
10
|
|
|
protected function refersToExistingAsset($value): bool |
|
11
|
|
|
{ |
|
12
|
|
|
// Faster check to see if there is an asset id passed or not. |
|
13
|
|
|
if(is_string($value) && $this->isValidJson($value)) return false; |
|
14
|
|
|
|
|
15
|
|
|
return !is_null($this->existingAsset($value)); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
protected function existingAsset($value): ?Asset |
|
19
|
|
|
{ |
|
20
|
|
|
return Asset::where('id', $value)->first(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
private function isValidJson($string): bool |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
json_decode($string); |
|
27
|
|
|
} catch (\Exception $e) { |
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function validateAssetDimensions(Asset $asset, $parameters) |
|
35
|
|
|
{ |
|
36
|
|
|
$filepath = $asset->media->first()->getPath(); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
if (! $sizeDetails = @getimagesize($filepath)) { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->requireParameterCount(1, $parameters, 'dimensions'); |
|
|
|
|
|
|
43
|
|
|
[$width, $height] = $sizeDetails; |
|
44
|
|
|
|
|
45
|
|
|
return $this->lowlevelDimensionsCheck($width, $height, $parameters); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** Taken from the Laravel ValidatedAttributes::validateDimensions */ |
|
49
|
|
|
private function lowlevelDimensionsCheck($width, $height, $parameters): bool |
|
50
|
|
|
{ |
|
51
|
|
|
$parameters = $this->parseNamedParameters($parameters); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if ($this->failsBasicDimensionChecks($parameters, $width, $height) || |
|
|
|
|
|
|
54
|
|
|
$this->failsRatioCheck($parameters, $width, $height)) { |
|
|
|
|
|
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function validateAssetMimetypes(Asset $asset, $parameters) |
|
62
|
|
|
{ |
|
63
|
|
|
return (in_array($asset->getMimeType(), $parameters) || |
|
64
|
|
|
in_array(explode('/', $asset->getMimeType())[0].'/*', $parameters)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function validateAssetMax(Asset $asset, $parameters) |
|
68
|
|
|
{ |
|
69
|
|
|
// Asset size in bytes |
|
70
|
|
|
$assetSize = $asset->media->first()->size; |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return ($assetSize / 1024) <= $parameters[0]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function validateAssetMin(Asset $asset, $parameters) |
|
76
|
|
|
{ |
|
77
|
|
|
// Asset size in bytes |
|
78
|
|
|
$assetSize = $asset->media->first()->size; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return ($assetSize / 1024) >= $parameters[0]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.