Passed
Push — master ( 91fe13...12bba4 )
by Shahrad
10:04
created

FormData::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace EasyHttp;
4
5
/**
6
 * Class FormData
7
 *
8
 * @link    https://github.com/shahradelahi/easy-http
9
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
10
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
11
 */
12
class FormData
13
{
14
15
    /**
16
     * @var array
17
     */
18
    private array $files = [];
19
20
    /**
21
     * FromData constructor.
22
     *
23
     * @param string|array $file_path
24
     */
25
    public function __construct(string|array $file_path = [])
26
    {
27
        if (is_string($file_path)) {
0 ignored issues
show
introduced by
The condition is_string($file_path) is always false.
Loading history...
28
            $file_path = [$file_path];
29
        }
30
31
        foreach ($file_path as $name => $path) {
32
            $this->addFile($name, $path);
33
        }
34
    }
35
36
    /**
37
     * Get the field has passed through the class
38
     *
39
     * @return array
40
     */
41
    public function getFiles(): array
42
    {
43
        return $this->files ?? [];
44
    }
45
46
    /**
47
     * This method will create an array with instances of CURLFile class
48
     *
49
     * @param string|array $file_path
50
     * @return array
51
     */
52
    public static function create(string|array $file_path): array
53
    {
54
        return (new FormData($file_path))->getFiles();
55
    }
56
57
    /**
58
     * @param string $name
59
     * @param string|\CURLFile $file
60
     * @return $this
61
     */
62
    public function addFile(string $name, string|\CURLFile $file): FormData
63
    {
64
        if ($file instanceof \CURLFile) {
65
            $this->files[$name] = $file;
66
            return $this;
67
        }
68
69
        $this->files[$name] = new \CURLFile(
70
            realpath($file),
71
            Client::get_file_type($file),
72
            basename($file)
73
        );
74
75
        return $this;
76
    }
77
78
}