Completed
Push — master ( fcb641...65474d )
by Derek Stephen
04:39
created

FileExtensionValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isValid() 0 8 1
A getMessages() 0 14 2
1
<?php
2
3
namespace Del\Form\Validator;
4
5
class FileExtensionValidator implements ValidatorInterface
6
{
7
    /** @var array $validExtensions */
8
    private $validExtensions;
9
10
    /**
11
     * FileExtensionValidator constructor.
12
     * @param array $validExtensions
13
     */
14 1
    public function __construct(array $validExtensions)
15
    {
16 1
        $this->validExtensions = $validExtensions;
17 1
    }
18
19
    /**
20
     * @param mixed $value
21
     * @return bool|void
22
     */
23 1
    public function isValid($value)
24
    {
25 1
        $debris = explode('.', $value);
26 1
        $extension = end($debris);
27 1
        $extension = strtolower($extension);
28
29 1
        return in_array($extension, $this->validExtensions);
30
    }
31
32
    /**
33
     * @return array|void
34
     */
35 1
    public function getMessages()
36
    {
37 1
        $validExtensions = $this->validExtensions;
38 1
        $last = array_pop($validExtensions);
39 1
        $extensions = 'The file must be a ' . $last . ' file.';
40
41 1
        if (count($validExtensions) > 0) {
42 1
            $extensions = implode(', ', $validExtensions);
43 1
            $extensions .= ' or ' . $last;
44 1
            $extensions = 'The file must be either a ' . $extensions . ' file.';
45
        }
46
47 1
        return [$extensions];
48
    }
49
}