Passed
Push — master ( e89a22...38e786 )
by Petr
08:13
created

Files   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 92
ccs 35
cts 36
cp 0.9722
rs 10
c 1
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 3
A renderChildren() 0 11 3
A addFile() 0 5 1
A getValues() 0 9 3
A setValues() 0 14 5
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 $templateLabel = '<label>%2$s</label>';
42
    protected $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> $array
77
     */
78 3
    public function setValues(array $array = []): 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
                    isset($array[$shortKey])
85
                        // @codeCoverageIgnoreStart
86
                        ? $array[$shortKey] // should not happen - set prevents this
87
                        // @codeCoverageIgnoreEnd
88
                        : (
89 2
                    isset($array[$child->getKey()])
90 1
                        ? $array[$child->getKey()]
91 2
                        : ''
92
                    )
93
                );
94
            }
95
        }
96 3
    }
97
98
    /**
99
     * @throws EntryException
100
     * @return array<string, IFileEntry>
101
     */
102 2
    public function getValues(): array
103
    {
104 2
        $result = [];
105 2
        foreach ($this->children as $child) {
106 1
            if ($child instanceof File) {
107 1
                $result[$child->getKey()] = $child->getFile();
108
            }
109
        }
110 2
        return $result;
111
    }
112
113
    /**
114
     * Render all sub-controls and wrap it all
115
     * @throws RenderException
116
     * @return string
117
     */
118 4
    public function renderChildren(): string
119
    {
120 4
        $return = '';
121 4
        foreach ($this->children as $alias => $child) {
122 4
            if ($child instanceof AControl) {
123 4
                $child->setAttribute('id', $this->getAlias() . '_' . $alias);
124
            }
125
126 4
            $return .= $this->wrapIt($child->render(), $this->wrappersChild) . PHP_EOL;
127
        }
128 4
        return $this->wrapIt($return, $this->wrappersChildren);
129
    }
130
}
131