Passed
Push — master ( 9ccabb...39bdf1 )
by Petr
07:56
created

Connect::parseData()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 11
rs 9.6111
1
<?php
2
3
namespace CoreTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_connect\arrays\Row;
8
use kalanis\kw_connect\core\AConnector;
9
use kalanis\kw_connect\core\Interfaces\IRow;
10
11
12
class ConnectorTest extends CommonTestClass
13
{
14
    public function testConnector()
15
    {
16
        $data = new Connect();
17
        $this->assertInstanceOf(\ArrayAccess::class, $data);
18
        $this->assertInstanceOf(\IteratorAggregate::class, $data);
19
        $this->assertInstanceOf(\Countable::class, $data);
20
21
        $this->assertEmpty($data->count());
22
23
        $data->offsetSet('different', 'another');
24
        $data->offsetSet('wub', 'wuz');
25
        $data->offsetSet('dira', ['fkl', 'uhb']);
26
27
        $this->assertEquals('another', $data->getByKey('different')->getValue('pk'));
28
        $this->assertEquals('wuz', $data->getByKey('wub')->getValue('pk'));
29
        $this->assertEquals('uhb', $data->getByKey('dira')->getValue(1));
30
        $this->assertNull($data->getByKey('unknown'));
31
    }
32
}
33
34
35
class Connect extends AConnector
36
{
37
    protected function parseData(): void
38
    {
39
        # translate items into IRow
40
        foreach ($this->translatedData as $key => &$input) {
41
            if (is_object($input) && ($input instanceof IRow)) {
42
                continue;
43
            }
44
            if (is_array($input)) {
45
                $this->translatedData[$key] = new Row($input);
46
            } else {
47
                $this->translatedData[$key] = new Row(['pk' => $input]);
48
            }
49
        }
50
    }
51
52
    public function getByKey($key): ?IRow
53
    {
54
        $this->parseData();
55
        return parent::getByKey($key);
56
    }
57
}
58