Passed
Branch master (1fbb83)
by Petr
03:24
created

Files::setValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
use kalanis\kw_forms\Exceptions\EntryException;
7
use kalanis\kw_forms\Exceptions\RenderException;
8
use kalanis\kw_forms\Interfaces\IMultiValue;
9
use kalanis\kw_input\Interfaces\IFileEntry;
10
11
12
/**
13
 * Definition of form controls Group of Files
14
 *
15
 * <b>Examples</b>
16
 * <code>
17
 * // render form for upload 5 files
18
 * $form = new Form();
19
 * $form->addFiles('fotos', 'Select files', 5);
20
 * echo $form;
21
 *
22
 * // render form for upload 5 files with defined labels
23
 * $labels = array('file 1','file 2','file 3','file 4','file 5');
24
 * $form = new Form();
25
 * $form->addFiles('fotos', $labels, 5)->setLabel('Select files');
26
 * echo $form;
27
 *
28
 * // render form for upload 5 files with defined labels
29
 * $form = new Form();
30
 * for($i=1;$i<6;$i++) {
31
 *     $files[] = new Controls\File(null, null, 'File '.$i);
32
 * }
33
 * $form->addFiles('fotos', 'Select files', $files);
34
 * echo $form;
35
 * </code>
36
 */
37
class Files extends AControl implements IMultiValue
38
{
39
    use TShorterKey;
40
41
    protected string $templateLabel = '<label>%2$s</label>';
42
    protected string $templateInput = '%3$s';
43
44
    /**
45
     * @param string $alias
46
     * @param iterable<string|int, string> $items
47
     * @param string $label
48
     * @param array<string, string|array<string>>|string $attributes
49
     * @return $this
50
     */
51 6
    public function set(string $alias, iterable $items = [], string $label = '', $attributes = []): self
52
    {
53 6
        $this->alias = $alias;
54 6
        $this->setLabel($label);
55 6
        foreach ($items as $key => $item) {
56 4
            $al = is_numeric($key) ? sprintf('%s[]', $alias) : sprintf('%s[%s]', $alias, strval($key));
57 4
            $this->addFile($al, $item, $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type array<string,string|string[]>; however, parameter $attributes of kalanis\kw_forms\Controls\Files::addFile() does only seem to accept array<string,string|string[]>|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            $this->addFile($al, $item, /** @scrutinizer ignore-type */ $attributes);
Loading history...
58
        }
59 6
        return $this;
60
    }
61
62
    /**
63
     * Add File input
64
     * @param string $label
65
     * @param string $key
66
     * @param array<string, string|array<string>>|string $attributes
67
     */
68 4
    public function addFile(string $key, string $label = '', $attributes = []): void
69
    {
70 4
        $formFile = new File();
71 4
        $formFile->set($key, $label)->setAttributes($attributes);
72 4
        $this->addChild($formFile);
73 4
    }
74
75
    /**
76
     * @param array<string, string|int|float|IFileEntry|null> $data
77
     */
78 3
    public function setValues(array $data = []): void
79
    {
80 3
        foreach ($this->children as $child) {
81 2
            if ($child instanceof File) {
82 2
                $shortKey = $this->shorterKey($child->getKey());
83 2
                $child->setValue(
84 2
                    $data[$shortKey] ?? (
85 2
                        $data[$child->getKey()] ?? ''
86
                    )
87
                );
88
            }
89
        }
90 3
    }
91
92
    /**
93
     * @throws EntryException
94
     * @return array<string, IFileEntry>
95
     */
96 2
    public function getValues(): array
97
    {
98 2
        $result = [];
99 2
        foreach ($this->children as $child) {
100 1
            if ($child instanceof File) {
101 1
                $result[$child->getKey()] = $child->getFile();
102
            }
103
        }
104 2
        return $result;
105
    }
106
107
    /**
108
     * Render all sub-controls and wrap it all
109
     * @throws RenderException
110
     * @return string
111
     */
112 4
    public function renderChildren(): string
113
    {
114 4
        $return = '';
115 4
        foreach ($this->children as $alias => $child) {
116 4
            if ($child instanceof AControl) {
117 4
                $child->setAttribute('id', $this->getAlias() . '_' . $alias);
118
            }
119
120 4
            $return .= $this->wrapIt($child->render(), $this->wrappersChild) . PHP_EOL;
121
        }
122 4
        return $this->wrapIt($return, $this->wrappersChildren);
123
    }
124
}
125