Passed
Push — master ( de4ed9...5f6eee )
by Petr
10:41
created

TFill::fillFromIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
namespace kalanis\kw_input\Traits;
4
5
6
use kalanis\kw_input\Entries\Entry;
7
use kalanis\kw_input\Interfaces;
8
9
10
/**
11
 * Trait TFill
12
 * @package kalanis\kw_input\Traits
13
 * Fill inputs by params
14
 */
15
trait TFill
16
{
17
    use TNullBytes;
18
19
    /**
20
     * @param string $source
21
     * @param array<int|string, mixed|null> $entries
22
     * @return Interfaces\IEntry[]
23
     */
24 10
    protected function fillFromEntries(string $source, array $entries): array
25
    {
26 10
        $result = [];
27 10
        foreach ($entries as $key => $value) {
28 10
            $result[] = $this->fillEntryData($source, $this->removeNullBytes(strval($key)), $value);
29
        }
30 10
        return $result;
31
    }
32
33
    /**
34
     * @param string $source
35
     * @param iterable<int|string, mixed|null> $iterator
36
     * @return Interfaces\IEntry[]
37
     */
38 2
    protected function fillFromIterator(string $source, iterable $iterator): array
39
    {
40 2
        $result = [];
41 2
        foreach ($iterator as $key => $value) {
42 2
            $result[] = $this->fillEntryData($source, $this->removeNullBytes(strval($key)), $value);
43
        }
44 2
        return $result;
45
    }
46
47
    /**
48
     * @param string $source
49
     * @param string $key
50
     * @param mixed|null $value
51
     * @return Entry
52
     */
53 12
    protected function fillEntryData(string $source, string $key, $value): Entry
54
    {
55 12
        if (is_object($value) && ($value instanceof Interfaces\IEntry)) {
56 2
            $value = $value->getValue();
57
        }
58 12
        $entry = new Entry();
59 12
        return $entry->setEntry($source, $key, $value);
60
    }
61
}
62