ValidUpload::validates()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 17
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 2
crap 12
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
}