|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Exceptions\EntryException; |
|
7
|
|
|
use kalanis\kw_input\Interfaces\IFileEntry; |
|
8
|
|
|
use kalanis\kw_rules\Interfaces; |
|
9
|
|
|
use kalanis\kw_rules\Rules; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class File |
|
14
|
|
|
* @package kalanis\kw_forms\Controls |
|
15
|
|
|
* Render input for sending files |
|
16
|
|
|
* Implementing IValidateFile because kw_rules are really independent |
|
17
|
|
|
*/ |
|
18
|
|
|
class File extends AControl implements Interfaces\IValidateFile |
|
19
|
|
|
{ |
|
20
|
|
|
protected string $templateInput = '<input type="file"%2$s />'; |
|
21
|
|
|
protected string $errorEntryNotFile = 'Entry %s does not contain a file'; |
|
22
|
|
|
protected ?IFileEntry $entry = null; |
|
23
|
|
|
|
|
24
|
1 |
|
protected function whichRulesFactory(): Interfaces\IRuleFactory |
|
25
|
|
|
{ |
|
26
|
1 |
|
return new Rules\File\Factory(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
8 |
|
public function set(string $key, string $label = ''): self |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->setEntry($key, null, $label); |
|
32
|
8 |
|
$this->setAttribute('id', $this->getKey()); |
|
33
|
8 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
6 |
|
public function renderInput($attributes = null): string |
|
37
|
|
|
{ |
|
38
|
6 |
|
$this->addAttributes($attributes); |
|
39
|
6 |
|
$this->setAttribute('name', $this->getKey()); |
|
40
|
6 |
|
return $this->wrapIt(sprintf($this->templateInput, null, $this->renderAttributes()), $this->wrappersInput); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param bool|float|int|string|IFileEntry|null $value |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function setValue($value): void |
|
47
|
|
|
{ |
|
48
|
4 |
|
if ($value instanceof IFileEntry) { |
|
49
|
2 |
|
$this->entry = $value; |
|
50
|
|
|
} |
|
51
|
4 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws EntryException |
|
55
|
|
|
* @return bool|float|int|mixed|string|null |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function getValue() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->getFile()->getValue(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @throws EntryException |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getMimeType(): string |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->getFile()->getMimeType(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws EntryException |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getTempName(): string |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->getFile()->getTempName(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @throws EntryException |
|
82
|
|
|
* @return int |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getError(): int |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->getFile()->getError(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws EntryException |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getSize(): int |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->getFile()->getSize(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @throws EntryException |
|
100
|
|
|
* @return IFileEntry |
|
101
|
|
|
*/ |
|
102
|
3 |
|
public function getFile(): IFileEntry |
|
103
|
|
|
{ |
|
104
|
3 |
|
if (empty($this->entry)) { |
|
105
|
1 |
|
throw new EntryException(sprintf($this->errorEntryNotFile, $this->getKey())); |
|
106
|
|
|
} |
|
107
|
2 |
|
return $this->entry; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|