Joined   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 4
A table() 0 4 1
A current() 0 3 1
A __construct() 0 3 1
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