Joined::table()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\TableLoader\Builder;
5
6
use Stratadox\ImmutableCollection\ImmutableCollection;
7
use Stratadox\TableLoader\Loader\Extract;
8
use Stratadox\TableLoader\Loader\JoinedTable;
9
use Stratadox\TableLoader\Loader\LoadsTables;
10
use Stratadox\TableLoader\Loader\Wired;
11
12
/**
13
 * Builder for JoinedTable converters.
14
 * @see JoinedTable
15
 *
16
 * @author Stratadox
17
 */
18
final class Joined extends ImmutableCollection implements MakesTableLoader
19
{
20
    public function __construct(DefinesObjectMapping ...$objectMappings)
21
    {
22
        parent::__construct(...$objectMappings);
23
    }
24
25
    /**
26
     * Makes a new builder for joined table mappers.
27
     *
28
     * @param DefinesObjectMapping ...$objectMappings
29
     * @return MakesTableLoader
30
     */
31
    public static function table(
32
        DefinesObjectMapping ...$objectMappings
33
    ): MakesTableLoader {
34
        return new self(...$objectMappings);
35
    }
36
37
    public function current(): DefinesObjectMapping
38
    {
39
        return parent::current();
40
    }
41
42
    /** @inheritdoc */
43
    public function __invoke(): LoadsTables
44
    {
45
        $wiring = [];
46
        $objects = [];
47
        $identity = [];
48
        foreach ($this as $mapping) {
49
            $identity[$mapping->label()] = $mapping->identityColumns();
50
        }
51
        foreach ($this as $mapping) {
52
            foreach ($identity as $label => $columns) {
53
                $mapping = $mapping->identifying($label, ...$columns);
54
            }
55
            $objects[] = $mapping->objects();
56
            $wiring[] = $mapping->wiring();
57
        }
58
        return JoinedTable::converter(
59
            Extract::these(...$objects),
60
            Wired::together(...$wiring)
61
        );
62
    }
63
}
64