JoinedTable::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\TableLoader\Loader;
5
6
use Stratadox\IdentityMap\IdentityMap;
7
use Stratadox\IdentityMap\MapsObjectsByIdentity;
8
9
/**
10
 * Converts a joined table into related objects.
11
 *
12
 * @author Stratadox
13
 */
14
final class JoinedTable implements LoadsTables
15
{
16
    private $makeObjects;
17
    private $relationships;
18
19
    private function __construct(
20
        MakesObjects $makeObjects,
21
        WiresObjects $relationships
22
    ) {
23
        $this->makeObjects = $makeObjects;
24
        $this->relationships = $relationships;
25
    }
26
27
    /**
28
     * Makes a new joined table converter.
29
     *
30
     * @param MakesObjects $makesObjects  The object extractor to use.
31
     * @param WiresObjects $relationships The relationship wiring.
32
     *
33
     * @return LoadsTables                The joined table converter.
34
     */
35
    public static function converter(
36
        MakesObjects $makesObjects,
37
        WiresObjects $relationships
38
    ): LoadsTables {
39
        return new self($makesObjects, $relationships);
40
    }
41
42
    /** @inheritdoc */
43
    public function from(
44
        array $input,
45
        MapsObjectsByIdentity $map = null
46
    ): ContainsResultingObjects {
47
        $map = $map ?: IdentityMap::startEmpty();
48
        $objects = $this->makeObjects->from($input, $map);
49
        $this->relationships->wire($objects, $input);
50
        return $objects;
51
    }
52
}
53