Passed
Push — master ( 488e4f...314286 )
by Petr
02:29
created

File::checkFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 $templateInput = '<input type="file"%2$s />';
21
    /** @var string */
22
    protected $errorEntryNotFile = 'Entry %s does not contain a file';
23
24
    /** @var IFileEntry|null */
25
    protected $entry = null;
26
27 1
    protected function whichFactory(): Interfaces\IRuleFactory
28
    {
29 1
        return new Rules\File\Factory();
30
    }
31
32 8
    public function set(string $key, string $label = ''): self
33
    {
34 8
        $this->setEntry($key, null, $label);
35 8
        $this->setAttribute('id', $this->getKey());
36 8
        return $this;
37
    }
38
39 6
    public function renderInput($attributes = null): string
40
    {
41 6
        $this->addAttributes($attributes);
42 6
        $this->setAttribute('name', $this->getKey());
43 6
        return $this->wrapIt(sprintf($this->templateInput, null, $this->renderAttributes()), $this->wrappersInput);
44
    }
45
46
    /**
47
     * @param bool|float|int|string|IFileEntry|null $value
48
     */
49 4
    public function setValue($value): void
50
    {
51 4
        if ($value instanceof IFileEntry) {
52 2
            $this->entry = $value;
53
        }
54 4
    }
55
56
    /**
57
     * @throws EntryException
58
     * @return bool|float|int|mixed|string|null
59
     */
60 1
    public function getValue()
61
    {
62 1
        return $this->getFile()->getValue();
63
    }
64
65
    /**
66
     * @throws EntryException
67
     * @return string
68
     */
69 1
    public function getMimeType(): string
70
    {
71 1
        return $this->getFile()->getMimeType();
72
    }
73
74
    /**
75
     * @throws EntryException
76
     * @return string
77
     */
78 1
    public function getTempName(): string
79
    {
80 1
        return $this->getFile()->getTempName();
81
    }
82
83
    /**
84
     * @throws EntryException
85
     * @return int
86
     */
87 1
    public function getError(): int
88
    {
89 1
        return $this->getFile()->getError();
90
    }
91
92
    /**
93
     * @throws EntryException
94
     * @return int
95
     */
96 1
    public function getSize(): int
97
    {
98 1
        return $this->getFile()->getSize();
99
    }
100
101
    /**
102
     * @throws EntryException
103
     * @return IFileEntry
104
     */
105 3
    public function getFile(): IFileEntry
106
    {
107 3
        if (empty($this->entry)) {
108 1
            throw new EntryException(sprintf($this->errorEntryNotFile, $this->getKey()));
109
        }
110 2
        return $this->entry;
111
    }
112
}
113