ExtensionConstraint::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Slince\Upload\Constraint;
4
5
use Symfony\Component\HttpFoundation\File\UploadedFile;
6
7
class ExtensionConstraint implements ConstraintInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $errorMessageTemplate = 'File extension {extension} is invalid';
13
14
    /**
15
     * allowed extensions
16
     * @var array
17
     */
18
    protected $allowedExtensions = [];
19
20
    public function __construct(array $allowedExtensions)
21
    {
22
        $this->allowedExtensions = array_map('strtolower', $allowedExtensions);
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function validate(UploadedFile $file): bool
29
    {
30
        return in_array(strtolower($file->getClientOriginalExtension()), $this->allowedExtensions, true);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getErrorMessage(UploadedFile $file): string
37
    {
38
        return str_replace('{extension}', $file->getClientOriginalExtension(), $this->errorMessageTemplate);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setErrorMessage(string $messageTemplate): void
45
    {
46
        $this->errorMessageTemplate = $messageTemplate;
47
    }
48
}
49