Passed
Pull Request — master (#222)
by
unknown
02:25
created

Files   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 23
ccs 8
cts 9
cp 0.8889
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use ArrayAccess;
8
use Countable;
9
use InvalidArgumentException;
10
use IteratorAggregate;
11
use Psr\Http\Message\UploadedFileInterface;
12
use Yiisoft\Arrays\ArrayAccessTrait;
13
14
/**
15
 * The collection of upload files
16
 */
17
final class Files implements Countable, IteratorAggregate, ArrayAccess
18
{
19
    use ArrayAccessTrait;
20
21
    /**
22
     * @var UploadedFileInterface[]
23
     */
24
    public array $data;
25
26 3
    public function __construct(mixed $data)
27
    {
28 3
        if ($data instanceof UploadedFileInterface) {
29 1
            $this->data = [$data];
30 3
        } elseif (!is_array($data)) {
31 2
            throw new InvalidArgumentException('Data should contain array of uploaded files');
32
        } else {
33 3
            foreach ($data as $file) {
34 2
                if (!$file instanceof UploadedFileInterface) {
35
                    throw new InvalidArgumentException('Data should contain UploadedFileInterface objects only');
36
                }
37
            }
38
            /** @var UploadedFileInterface[] $data */
39 3
            $this->data = $data;
40
        }
41
    }
42
}
43