Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

FileSizeValidator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Validator;
8
9 use eZ\Publish\Core\FieldType\Validator;
10 use eZ\Publish\Core\FieldType\ValidationError;
11 use eZ\Publish\Core\FieldType\Value as BaseValue;
12
18 class FileSizeValidator extends Validator
19 {
20 protected $constraints = [
21 'maxFileSize' => false,
22 ];
23
24 protected $constraintsSchema = [
25 'maxFileSize' => [
26 'type' => 'int',
27 'default' => false,
28 ],
29 ];
30
31 public function validateConstraints($constraints)
32 {
33 $validationErrors = [];
34
35 foreach ($constraints as $name => $value) {
36 switch ($name) {
37 case 'maxFileSize':
38 if ($value !== false && !is_int($value)) {
39 $validationErrors[] = new ValidationError(
40 "Validator parameter '%parameter%' value must be of integer type",
41 null,
42 [
43 '%parameter%' => $name,
44 ]
45 );
46 }
47 break;
48 default:
49 $validationErrors[] = new ValidationError(
50 "Validator parameter '%parameter%' is unknown",
51 null,
52 [
53 '%parameter%' => $name,
54 ]
55 );
56 }
57 }
58
59 return $validationErrors;
60 }
61
69 public function validate(BaseValue $value)
70 {
71 $isValid = true;
72
73 if ($this->constraints['maxFileSize'] !== false && $value->file->size > $this->constraints['maxFileSize']) {
74 $this->errors[] = new ValidationError(
75 'The file size cannot exceed %size% byte.',
76 'The file size cannot exceed %size% bytes.',
77 [
78 '%size%' => $this->constraints['maxFileSize'],
79 ]
80 );
81 $isValid = false;
82 }
83
84 return $isValid;
85 }
86 }
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\FieldType\Validator\FileSizeValidator
Definition: FileSizeValidator.php:19
eZ\Publish\Core\FieldType\Validator\FileSizeValidator\$constraints
$constraints
Definition: FileSizeValidator.php:20
eZ\Publish\Core\FieldType\Validator\FileSizeValidator\validate
validate(BaseValue $value)
Checks if $value->file has the appropriate size.
Definition: FileSizeValidator.php:69
eZ\Publish\Core\FieldType\Validator\FileSizeValidator\validateConstraints
validateConstraints($constraints)
Definition: FileSizeValidator.php:31
eZ\Publish\Core\FieldType\Validator\FileSizeValidator\$constraintsSchema
$constraintsSchema
Definition: FileSizeValidator.php:24
eZ\Publish\Core\FieldType\Validator
Base field type validator validator.
Definition: Validator.php:15
eZ\Publish\Core\FieldType\Value
Abstract class for all field value classes.
Definition: Core/FieldType/Value.php:17
eZ\Publish\Core\FieldType\Validator
Definition: EmailAddressValidator.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.