|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MonsieurBiz\SyliusRichEditorPlugin\Form\Constraints; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
8
|
|
|
|
|
9
|
|
|
final class RichEditorConstraints |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Return constraint depending on data |
|
13
|
|
|
* If user created the element, the field is required |
|
14
|
|
|
* If it's an edition and it contains a filename, we don't flag it as required |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $options |
|
17
|
|
|
* @param string $fieldName |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function getImageConstraints(array $options, string $fieldName) { |
|
21
|
|
|
// If is edition we don't have constraint to avoid re-upload |
|
22
|
|
|
$data = $options['data'] ?? null; |
|
23
|
|
|
if (isset($data[$fieldName])) { |
|
24
|
|
|
return []; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// No image set yet, we require file |
|
28
|
|
|
return [ |
|
29
|
|
|
new Assert\NotBlank([]), |
|
30
|
|
|
new Assert\Image([]) |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Return constraint depending on data |
|
36
|
|
|
* If user created the element, the field is required |
|
37
|
|
|
* If it's an edition and it contains a filename, we don't flag it as required |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $options |
|
40
|
|
|
* @param string $fieldName |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function getVideoConstraints(array $options, string $fieldName) { |
|
44
|
|
|
// If is edition we don't have constraint to avoid re-upload |
|
45
|
|
|
$data = $options['data'] ?? null; |
|
46
|
|
|
if (isset($data[$fieldName])) { |
|
47
|
|
|
return []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// No video set yet, we require file |
|
51
|
|
|
return [ |
|
52
|
|
|
new Assert\NotBlank([]), |
|
53
|
|
|
new Assert\File([ |
|
54
|
|
|
'mimeTypes' => ['video/mpeg', 'video/mp4', 'video/quicktime', 'video/x-ms-wmv', 'video/x-msvideo', 'video/x-flv'] |
|
55
|
|
|
]), |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|