Passed
Push — 5.x ( 1d4452...ffef10 )
by Enjoys
13:47
created

File::isMultiple()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 2
nop 0
crap 3
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
            'system'
35
        ]);
36
    }
37 3
    public function setMultiple(): self
38
    {
39 3
        $this->setAttribute(AttributeFactory::create('multiple'));
40 3
        return $this;
41
    }
42
43
    /**
44
     * @return $this
45
     */
46 31
    public function setAttribute(AttributeInterface $attribute, string $namespace = 'general'): File
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): self
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()
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
    }
96
97
98 5
    public function setMaxFileSize(int $bytes): self
99
    {
100 5
        $this->getForm()?->hidden('MAX_FILE_SIZE', (string) $bytes);
101 5
        return $this;
102
    }
103
104
105
    /**
106
     * @throws ExceptionRule
107
     */
108 31
    public function addRule(string $ruleClass, mixed ...$params): File
109
    {
110 31
        if ($ruleClass !== Rules::UPLOAD) {
111 1
            throw new ExceptionRule(
112 1
                \sprintf("К элементу [%s] можно подключить только правило: [%s]", __CLASS__, Rules::UPLOAD)
113
            );
114
        }
115 31
        return $this->parentAddRule($ruleClass, ...$params);
116
    }
117
}
118