|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Forms\Elements; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\Forms\AttributeFactory; |
|
8
|
|
|
use Enjoys\Forms\Element; |
|
9
|
|
|
use Enjoys\Forms\Exception\ExceptionRule; |
|
10
|
|
|
use Enjoys\Forms\Interfaces\AttributeInterface; |
|
11
|
|
|
use Enjoys\Forms\Interfaces\Descriptionable; |
|
12
|
|
|
use Enjoys\Forms\Interfaces\Ruleable; |
|
13
|
|
|
use Enjoys\Forms\Rules; |
|
14
|
|
|
use Enjoys\Forms\Traits; |
|
15
|
|
|
|
|
16
|
|
|
use function iniSize2bytes; |
|
17
|
|
|
|
|
18
|
|
|
class File extends Element implements Ruleable, Descriptionable |
|
19
|
|
|
{ |
|
20
|
|
|
use Traits\Description; |
|
21
|
|
|
use Traits\Rules { |
|
22
|
|
|
addRule as private parentAddRule; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected string $type = 'file'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws ExceptionRule |
|
29
|
|
|
*/ |
|
30
|
31 |
|
public function __construct(string $name, ?string $title = null) |
|
31
|
|
|
{ |
|
32
|
31 |
|
parent::__construct($name, $title); |
|
33
|
31 |
|
$this->addRule(Rules::UPLOAD, [ |
|
34
|
31 |
|
'system' |
|
35
|
31 |
|
]); |
|
36
|
|
|
} |
|
37
|
3 |
|
public function setMultiple(): static |
|
38
|
|
|
{ |
|
39
|
3 |
|
$this->setAttribute(AttributeFactory::create('multiple')); |
|
40
|
3 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @psalm-suppress MethodSignatureMismatch |
|
45
|
|
|
*/ |
|
46
|
31 |
|
public function setAttribute(AttributeInterface $attribute, string $namespace = 'general'): static |
|
47
|
|
|
{ |
|
48
|
31 |
|
parent::setAttribute($attribute, $namespace); |
|
49
|
31 |
|
$this->isMultiple(); |
|
50
|
31 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
31 |
|
private function isMultiple(): void |
|
54
|
|
|
{ |
|
55
|
31 |
|
if ($this->getAttribute('multiple') !== null && !str_ends_with($this->getName(), '[]')) { |
|
56
|
3 |
|
$id = $this->getAttribute('id') ?? AttributeFactory::create('id', $this->getName()); |
|
57
|
3 |
|
$this->setName($this->getName() . '[]'); |
|
58
|
|
|
// т.к. id уже переписан, восстанавливаем его |
|
59
|
3 |
|
$this->setAttribute($id); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function addAccept(string $accept): self |
|
64
|
|
|
{ |
|
65
|
2 |
|
$attribute = $this->getAttribute('accept'); |
|
66
|
2 |
|
if ($attribute === null) { |
|
67
|
2 |
|
$attribute = AttributeFactory::create('accept'); |
|
68
|
2 |
|
$attribute->setMultiple(true); |
|
69
|
2 |
|
$attribute->setSeparator(','); |
|
70
|
2 |
|
$this->setAttribute($attribute); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
$attribute->add($accept); |
|
74
|
2 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string[] $accepts |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function setAccepts(array $accepts): static |
|
82
|
|
|
{ |
|
83
|
1 |
|
$this->removeAttribute('accept'); |
|
84
|
1 |
|
foreach ($accepts as $accept) { |
|
85
|
1 |
|
$this->addAccept($accept); |
|
86
|
|
|
} |
|
87
|
1 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
public function prepare(): bool |
|
91
|
|
|
{ |
|
92
|
5 |
|
$this->getForm()?->setAttribute(AttributeFactory::create('enctype', 'multipart/form-data')); |
|
93
|
5 |
|
$this->getForm()?->setMethod('post'); |
|
94
|
5 |
|
$this->setMaxFileSize(iniSize2bytes(ini_get('upload_max_filesize'))); |
|
95
|
5 |
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
5 |
|
public function setMaxFileSize(int $bytes): static |
|
100
|
|
|
{ |
|
101
|
5 |
|
$this->getForm()?->hidden('MAX_FILE_SIZE', (string) $bytes); |
|
102
|
5 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @throws ExceptionRule |
|
108
|
|
|
*/ |
|
109
|
31 |
|
public function addRule(string $ruleClass, mixed ...$params): static |
|
110
|
|
|
{ |
|
111
|
31 |
|
if ($ruleClass !== Rules::UPLOAD) { |
|
112
|
1 |
|
throw new ExceptionRule( |
|
113
|
1 |
|
\sprintf("К элементу [%s] можно подключить только правило: [%s]", __CLASS__, Rules::UPLOAD) |
|
114
|
1 |
|
); |
|
115
|
|
|
} |
|
116
|
31 |
|
return $this->parentAddRule($ruleClass, ...$params); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|