FormData   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 64
c 0
b 0
f 0
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addFile() 0 14 2
A __construct() 0 8 3
A getFiles() 0 3 1
A create() 0 3 1
1
<?php
2
3
namespace EasyHttp;
4
5
/**
6
 * FormData class
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
}