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

Files::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
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 13
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
final class Files implements Countable, IteratorAggregate, ArrayAccess
15
{
16
    use ArrayAccessTrait;
17
18
    /**
19
     * @var UploadedFileInterface[]
20
     */
21
    public array $data;
22
23 3
    public function __construct(mixed $data)
24
    {
25 3
        if ($data instanceof UploadedFileInterface) {
26 1
            $this->data = [$data];
27 3
        } elseif (!is_array($data)) {
28 2
            throw new InvalidArgumentException('Data should contain array of uploaded files');
29
        } else {
30 3
            foreach ($data as $file) {
31 2
                if (!$file instanceof UploadedFileInterface) {
32
                    throw new InvalidArgumentException('Data should contain UploadedFileInterface objects only');
33
                }
34
            }
35 3
            $this->data = $data;
36
        }
37
    }
38
}
39