Skip to content

Commit

Permalink
Bump to symfony 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Aug 11, 2023
1 parent 7d4836a commit 1f4002b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .idea/files.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
],
"require": {
"php": "^7.4 | ^8.0",
"php": "^8.0",
"league/mime-type-detection": "^1.7",
"gabrielelana/byte-units": "^0.5.0"
},
Expand Down
31 changes: 19 additions & 12 deletions src/Validator/Constraint/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Arxy\FilesBundle\Validator\Constraint;

use Attribute;
use Exception;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
Expand All @@ -16,29 +17,35 @@
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
class File extends Constraint
{
public ?int $maxSize = null;
public string $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}.';

/** @var array<int, string> */
public array $mimeTypes = [];
public string $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';

/**
* @param array<string, mixed> $options
* @param array<int, string> $groups
* @param mixed $payload
* @param array<string>|string $mimeTypes
* @param array<string>|null $groups
*/
public function __construct(array $options = null, array $groups = null, $payload = null)
{
if (isset($options['maxSize']) && is_string($options['maxSize'])) {
$options['maxSize'] = $this->normalizeBinaryFormat($options['maxSize']);
public function __construct(
int|string|null $maxSize,
public string $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}.',
array|string $mimeTypes = [],
public string $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.',
array $groups = null,
) {
if (is_string($maxSize)) {
$maxSize = $this->normalizeBinaryFormat($maxSize);
}
$this->maxSize = $maxSize;

if (isset($options['mimeTypes']) && !is_array($options['mimeTypes'])) {
$options['mimeTypes'] = [$options['mimeTypes']];
if (!is_array($mimeTypes)) {
$mimeTypes = [$mimeTypes];
}
parent::__construct($options, $groups, $payload);
$this->mimeTypes = $mimeTypes;
parent::__construct(groups: $groups);
}

private function normalizeBinaryFormat(string $maxSize): int
Expand Down

0 comments on commit 1f4002b

Please sign in to comment.