Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
File
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
16
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form\Element\Input;
15
16use Pop\Form\Element;
17
18/**
19 * Form file element class
20 *
21 * @category   Pop
22 * @package    Pop\Form
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28
29class File extends Element\Input
30{
31
32    /**
33     * Constructor
34     *
35     * Instantiate the file input form element
36     *
37     * @param  string  $name
38     * @param  ?string $value
39     * @param  ?string $indent
40     */
41    public function __construct(string $name, ?string $value = null, ?string $indent = null)
42    {
43        parent::__construct($name, 'file', $value, $indent);
44    }
45
46    /**
47     * Validate the form element object
48     *
49     * @param  array $formValues
50     * @return bool
51     */
52    public function validate(array $formValues = []): bool
53    {
54        if (($_FILES) && (isset($_FILES[$this->name]['name']))) {
55            $value = $_FILES[$this->name]['name'];
56            $size  = $_FILES[$this->name]['size'];
57        } else {
58            $value = null;
59            $size  = null;
60        }
61
62        // Check if the element is required
63        if (($this->required) && empty($value)) {
64            $this->errors[] = 'This field is required.';
65        }
66
67        // Check field validators
68        if (count($this->validators) > 0) {
69            foreach ($this->validators as $validator) {
70                if ($validator instanceof \Pop\Validator\ValidatorInterface) {
71                    $class =  get_class($validator);
72                    if (($size !== null) &&
73                        (('Pop\Validator\LessThanEqual' == $class) || ('Pop\Validator\GreaterThanEqual' == $class) ||
74                         ('Pop\Validator\LessThan' == $class) || ('Pop\Validator\GreaterThan' == $class))) {
75                        if (!$validator->evaluate($size)) {
76                            $this->errors[] = $validator->getMessage();
77                        }
78                    } else {
79                        if (!$validator->evaluate($value)) {
80                            $this->errors[] = $validator->getMessage();
81                        }
82                    }
83                } else if (is_callable($validator)) {
84                    $this->validateCallable($validator, $value, $formValues);
85                }
86            }
87        }
88
89        return (count($this->errors) == 0);
90    }
91
92}