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\Descriptionable; |
11
|
|
|
use Enjoys\Forms\Interfaces\Ruleable; |
12
|
|
|
use Enjoys\Forms\Rules; |
13
|
|
|
use Enjoys\Forms\Traits; |
14
|
|
|
|
15
|
|
|
use function iniSize2bytes; |
16
|
|
|
|
17
|
|
|
class File extends Element implements Ruleable, Descriptionable |
18
|
|
|
{ |
19
|
|
|
use Traits\Description; |
20
|
|
|
use Traits\Rules { |
21
|
|
|
addRule as private parentAddRule; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected string $type = 'file'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws ExceptionRule |
28
|
|
|
*/ |
29
|
27 |
|
public function __construct(string $name, string $title = null) |
30
|
|
|
{ |
31
|
27 |
|
parent::__construct($name, $title); |
32
|
27 |
|
$this->addRule(Rules::UPLOAD, null, [ |
33
|
|
|
'system' |
34
|
|
|
]); |
35
|
|
|
} |
36
|
1 |
|
public function setMultiple(): self |
37
|
|
|
{ |
38
|
1 |
|
$this->setAttr(AttributeFactory::create('multiple')); |
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function addAccept(string $accept): self |
43
|
|
|
{ |
44
|
2 |
|
$attribute = $this->getAttr('accept'); |
45
|
2 |
|
if ($attribute === null) { |
46
|
2 |
|
$attribute = AttributeFactory::create('accept'); |
47
|
2 |
|
$attribute->setMultiple(true); |
48
|
2 |
|
$attribute->setSeparator(','); |
49
|
2 |
|
$this->setAttr($attribute); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$attribute->add($accept); |
53
|
2 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string[] $accepts |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
1 |
|
public function setAccepts(array $accepts): self |
61
|
|
|
{ |
62
|
1 |
|
$this->removeAttr('accept'); |
63
|
1 |
|
foreach ($accepts as $accept) { |
64
|
1 |
|
$this->addAccept($accept); |
65
|
|
|
} |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function prepare() |
70
|
|
|
{ |
71
|
4 |
|
$this->getForm()->setAttr(AttributeFactory::create('enctype', 'multipart/form-data')); |
72
|
4 |
|
$this->getForm()->setMethod('post'); |
73
|
4 |
|
$this->setMaxFileSize(iniSize2bytes(ini_get('upload_max_filesize'))); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
4 |
|
public function setMaxFileSize(int $bytes): self |
78
|
|
|
{ |
79
|
4 |
|
$this->getForm()->hidden('MAX_FILE_SIZE', (string) $bytes); |
80
|
4 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws ExceptionRule |
86
|
|
|
*/ |
87
|
27 |
|
public function addRule(string $ruleClass, ?string $message = null, mixed $params = []): File |
88
|
|
|
{ |
89
|
27 |
|
if (\strtolower($ruleClass) !== \strtolower(Rules::UPLOAD)) { |
90
|
1 |
|
throw new ExceptionRule( |
91
|
1 |
|
\sprintf("К элементу [%s] можно подключить только правило: [%s]", __CLASS__, Rules::UPLOAD) |
92
|
|
|
); |
93
|
|
|
} |
94
|
27 |
|
return $this->parentAddRule($ruleClass, $message, $params); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|