Passed
Push — master ( 13d8cc...7bc2a7 )
by Jesse
03:29
created

HasOne   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A in() 0 3 1
A load() 0 14 2
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\TableLoader\Loader;
5
6
/**
7
 * Defines a has-one relationship.
8
 *
9
 * @author Stratadox
10
 */
11
final class HasOne implements MakesConnections
12
{
13
    private $property;
14
15
    private function __construct(string $property)
16
    {
17
        $this->property = $property;
18
    }
19
20
    /**
21
     * Makes a connector for a has-one type relationship.
22
     *
23
     * @param string $property  The property to map.
24
     * @return MakesConnections The relationship connector.
25
     */
26
    public static function in(string $property): MakesConnections
27
    {
28
        return new self($property);
29
    }
30
31
    /** @inheritdoc */
32
    public function load(
33
        KnowsWhereToLook $from,
34
        array $data,
35
        KnowsWhereToLookTo $to,
36
        ContainsResultingObjects $objects
37
    ): array {
38
        // @todo add caching?
39
        $relations = [];
40
        foreach ($data as $relation) {
41
            $relations[
42
                $from->this($relation)
43
            ] = $objects[$to->label()][$to->this($relation)];
44
        }
45
        return [$this->property => $relations];
46
    }
47
}
48