ValidUpload   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validates() 0 19 3
1
<?php
2
3
/**
4
 * This file is part of slick/form package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Form\Input\Validator;
11
12
use Psr\Http\Message\UploadedFileInterface;
13
use Slick\Form\Input\File;
14
use Slick\Validator\ValidatorInterface;
15
16
/**
17
 * Valid Upload validator
18
 *
19
 * @package Slick\Form\Input\Validator
20
 * @author  Filipe Silva <[email protected]>
21
 */
22
class ValidUpload extends RequiredUpload implements ValidatorInterface
23
{
24
25
    /**
26
     * Returns true if and only if $value meets the validation requirements
27
     *
28
     * The context specified can be used in the validation process so that
29
     * the same value can be valid or invalid depending on that data.
30
     *
31
     * @param mixed $value
32
     * @param array|mixed|File $context
33
     *
34
     * @return bool
35
     */
36
    public function validates($value, $context = [])
37
    {
38
        $valid = true;
39
        /** @var File $input */
40
        $input = $context['input'];
41
        /** @var UploadedFileInterface $file */
42
        $file = $input->getValue();
43
        if (
44
            $file->getError() != UPLOAD_ERR_OK &&
45
            $file->getError() != UPLOAD_ERR_NO_FILE
46
        ) {
47
            $valid = false;
48
            $this->addMessage(
49
                $this->messageTemplate,
50
                $this->uploadErrors[$file->getError()]
51
            );
52
        }
53
        return $valid;
54
    }
55
}