Skip to content

Commit d440f78

Browse files
author
symfonyaml
committed
[Validator] Add the Video constraint for validating video files
1 parent 4ed9d03 commit d440f78

File tree

11 files changed

+937
-1
lines changed

11 files changed

+937
-1
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\File;
15+
16+
/**
17+
* @author Kev <https://github.com/symfonyaml>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
class Video extends File
21+
{
22+
public const SIZE_NOT_DETECTED_ERROR = '6d55c3f4-e58e-4fe3-91ee-74b492199956';
23+
public const TOO_WIDE_ERROR = '7f87163d-878f-47f5-99ba-a8eb723a1ab2';
24+
public const TOO_NARROW_ERROR = '9afbd561-4f90-4a27-be62-1780fc43604a';
25+
public const TOO_HIGH_ERROR = '7efae81c-4877-47ba-aa65-d01ccb0d4645';
26+
public const TOO_LOW_ERROR = 'aef0cb6a-c07f-4894-bc08-1781420d7b4c';
27+
public const TOO_FEW_PIXEL_ERROR = '1b06b97d-ae48-474e-978f-038a74854c43';
28+
public const TOO_MANY_PIXEL_ERROR = 'ee0804e8-44db-4eac-9775-be91aaf72ce1';
29+
public const RATIO_TOO_BIG_ERROR = '70cafca6-168f-41c9-8c8c-4e47a52be643';
30+
public const RATIO_TOO_SMALL_ERROR = '59b8c6ef-bcf2-4ceb-afff-4642ed92f12e';
31+
public const SQUARE_NOT_ALLOWED_ERROR = '5d41425b-facb-47f7-a55a-de9fbe45cb46';
32+
public const LANDSCAPE_NOT_ALLOWED_ERROR = '6f895685-7cf2-4d65-b3da-9029c5581d88';
33+
public const PORTRAIT_NOT_ALLOWED_ERROR = '65608156-77da-4c79-a88c-02ef6d18c782';
34+
public const CORRUPTED_VIDEO_ERROR = '5d4163f3-648f-4e39-87fd-cc5ea7aad2d1';
35+
36+
// Include the mapping from the base class
37+
38+
protected const ERROR_NAMES = [
39+
self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
40+
self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
41+
self::EMPTY_ERROR => 'EMPTY_ERROR',
42+
self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
43+
self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
44+
self::FILENAME_TOO_LONG => 'FILENAME_TOO_LONG',
45+
self::SIZE_NOT_DETECTED_ERROR => 'SIZE_NOT_DETECTED_ERROR',
46+
self::TOO_WIDE_ERROR => 'TOO_WIDE_ERROR',
47+
self::TOO_NARROW_ERROR => 'TOO_NARROW_ERROR',
48+
self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
49+
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
50+
self::TOO_FEW_PIXEL_ERROR => 'TOO_FEW_PIXEL_ERROR',
51+
self::TOO_MANY_PIXEL_ERROR => 'TOO_MANY_PIXEL_ERROR',
52+
self::RATIO_TOO_BIG_ERROR => 'RATIO_TOO_BIG_ERROR',
53+
self::RATIO_TOO_SMALL_ERROR => 'RATIO_TOO_SMALL_ERROR',
54+
self::SQUARE_NOT_ALLOWED_ERROR => 'SQUARE_NOT_ALLOWED_ERROR',
55+
self::LANDSCAPE_NOT_ALLOWED_ERROR => 'LANDSCAPE_NOT_ALLOWED_ERROR',
56+
self::PORTRAIT_NOT_ALLOWED_ERROR => 'PORTRAIT_NOT_ALLOWED_ERROR',
57+
self::CORRUPTED_VIDEO_ERROR => 'CORRUPTED_VIDEO_ERROR',
58+
];
59+
60+
/**
61+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
62+
*/
63+
protected static $errorNames = self::ERROR_NAMES;
64+
65+
public array|string $mimeTypes = 'video/*';
66+
public ?int $minWidth = null;
67+
public ?int $maxWidth = null;
68+
public ?int $maxHeight = null;
69+
public ?int $minHeight = null;
70+
public int|float|null $maxRatio = null;
71+
public int|float|null $minRatio = null;
72+
public int|float|null $minPixels = null;
73+
public int|float|null $maxPixels = null;
74+
public ?bool $allowSquare = true;
75+
public ?bool $allowLandscape = true;
76+
public ?bool $allowPortrait = true;
77+
78+
// The constant for a wrong MIME type is taken from the parent class.
79+
public string $mimeTypesMessage = 'This file is not a valid video.';
80+
public string $sizeNotDetectedMessage = 'The size of the video could not be detected.';
81+
public string $maxWidthMessage = 'The video width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px.';
82+
public string $minWidthMessage = 'The video width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px.';
83+
public string $maxHeightMessage = 'The video height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px.';
84+
public string $minHeightMessage = 'The video height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px.';
85+
public string $minPixelsMessage = 'The video has too few pixels ({{ pixels }} pixels). Minimum amount expected is {{ min_pixels }} pixels.';
86+
public string $maxPixelsMessage = 'The video has too many pixels ({{ pixels }} pixels). Maximum amount expected is {{ max_pixels }} pixels.';
87+
public string $maxRatioMessage = 'The video ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}.';
88+
public string $minRatioMessage = 'The video ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}.';
89+
public string $allowSquareMessage = 'The video is square ({{ width }}x{{ height }}px). Square videos are not allowed.';
90+
public string $allowLandscapeMessage = 'The video is landscape oriented ({{ width }}x{{ height }}px). Landscape oriented videos are not allowed.';
91+
public string $allowPortraitMessage = 'The video is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented videos are not allowed.';
92+
public string $corruptedMessage = 'The video file is corrupted.';
93+
94+
public function __construct(
95+
?array $options = null,
96+
int|string|null $maxSize = null,
97+
?bool $binaryFormat = null,
98+
array|string|null $mimeTypes = null,
99+
?int $filenameMaxLength = null,
100+
?int $minWidth = null,
101+
?int $maxWidth = null,
102+
?int $maxHeight = null,
103+
?int $minHeight = null,
104+
int|float|null $maxRatio = null,
105+
int|float|null $minRatio = null,
106+
int|float|null $minPixels = null,
107+
int|float|null $maxPixels = null,
108+
?bool $allowSquare = null,
109+
?bool $allowLandscape = null,
110+
?bool $allowPortrait = null,
111+
?string $notFoundMessage = null,
112+
?string $notReadableMessage = null,
113+
?string $maxSizeMessage = null,
114+
?string $mimeTypesMessage = null,
115+
?string $disallowEmptyMessage = null,
116+
?string $filenameTooLongMessage = null,
117+
?string $uploadIniSizeErrorMessage = null,
118+
?string $uploadFormSizeErrorMessage = null,
119+
?string $uploadPartialErrorMessage = null,
120+
?string $uploadNoFileErrorMessage = null,
121+
?string $uploadNoTmpDirErrorMessage = null,
122+
?string $uploadCantWriteErrorMessage = null,
123+
?string $uploadExtensionErrorMessage = null,
124+
?string $uploadErrorMessage = null,
125+
?string $sizeNotDetectedMessage = null,
126+
?string $maxWidthMessage = null,
127+
?string $minWidthMessage = null,
128+
?string $maxHeightMessage = null,
129+
?string $minHeightMessage = null,
130+
?string $minPixelsMessage = null,
131+
?string $maxPixelsMessage = null,
132+
?string $maxRatioMessage = null,
133+
?string $minRatioMessage = null,
134+
?string $allowSquareMessage = null,
135+
?string $allowLandscapeMessage = null,
136+
?string $allowPortraitMessage = null,
137+
?string $corruptedMessage = null,
138+
?array $groups = null,
139+
mixed $payload = null
140+
) {
141+
parent::__construct(
142+
$options,
143+
$maxSize,
144+
$binaryFormat,
145+
$mimeTypes,
146+
$filenameMaxLength,
147+
$notFoundMessage,
148+
$notReadableMessage,
149+
$maxSizeMessage,
150+
$mimeTypesMessage,
151+
$disallowEmptyMessage,
152+
$filenameTooLongMessage,
153+
$uploadIniSizeErrorMessage,
154+
$uploadFormSizeErrorMessage,
155+
$uploadPartialErrorMessage,
156+
$uploadNoFileErrorMessage,
157+
$uploadNoTmpDirErrorMessage,
158+
$uploadCantWriteErrorMessage,
159+
$uploadExtensionErrorMessage,
160+
$uploadErrorMessage,
161+
$groups,
162+
$payload
163+
);
164+
165+
$this->minWidth = $minWidth ?? $this->minWidth;
166+
$this->maxWidth = $maxWidth ?? $this->maxWidth;
167+
$this->maxHeight = $maxHeight ?? $this->maxHeight;
168+
$this->minHeight = $minHeight ?? $this->minHeight;
169+
$this->maxRatio = $maxRatio ?? $this->maxRatio;
170+
$this->minRatio = $minRatio ?? $this->minRatio;
171+
$this->minPixels = $minPixels ?? $this->minPixels;
172+
$this->maxPixels = $maxPixels ?? $this->maxPixels;
173+
$this->allowSquare = $allowSquare ?? $this->allowSquare;
174+
$this->allowLandscape = $allowLandscape ?? $this->allowLandscape;
175+
$this->allowPortrait = $allowPortrait ?? $this->allowPortrait;
176+
$this->sizeNotDetectedMessage = $sizeNotDetectedMessage ?? $this->sizeNotDetectedMessage;
177+
$this->maxWidthMessage = $maxWidthMessage ?? $this->maxWidthMessage;
178+
$this->minWidthMessage = $minWidthMessage ?? $this->minWidthMessage;
179+
$this->maxHeightMessage = $maxHeightMessage ?? $this->maxHeightMessage;
180+
$this->minHeightMessage = $minHeightMessage ?? $this->minHeightMessage;
181+
$this->minPixelsMessage = $minPixelsMessage ?? $this->minPixelsMessage;
182+
$this->maxPixelsMessage = $maxPixelsMessage ?? $this->maxPixelsMessage;
183+
$this->maxRatioMessage = $maxRatioMessage ?? $this->maxRatioMessage;
184+
$this->minRatioMessage = $minRatioMessage ?? $this->minRatioMessage;
185+
$this->allowSquareMessage = $allowSquareMessage ?? $this->allowSquareMessage;
186+
$this->allowLandscapeMessage = $allowLandscapeMessage ?? $this->allowLandscapeMessage;
187+
$this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
188+
$this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;
189+
190+
if (!\in_array('video/*', (array) $this->mimeTypes, true) && !\array_key_exists('mimeTypesMessage', $options ?? []) && null === $mimeTypesMessage) {
191+
$this->mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)