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

Files::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
crap 5.0342
rs 9.6111
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