Extract::from()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
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\MapsObjectsByIdentity as Map;
7
use Stratadox\ImmutableCollection\ImmutableCollection;
8
9
/**
10
 * Makes partially hydrated objects from an input array.
11
 *
12
 * This class is a composition of zero or more implementations of its own
13
 * interface. It Makes Objects by delegating the work to each of its
14
 * subcontractors, merging their work into one final result.
15
 *
16
 * @author Stratadox
17
 */
18
final class Extract extends ImmutableCollection implements MakesObjects
19
{
20
    private function __construct(MakesObjects ...$objects)
21
    {
22
        parent::__construct(...$objects);
23
    }
24
25
    /**
26
     * Makes a new object extractor that composes more object extractors.
27
     *
28
     * @param MakesObjects ...$objects The other object extractors.
29
     *
30
     * @return MakesObjects            The composed extractor.
31
     */
32
    public static function these(MakesObjects ...$objects): MakesObjects
33
    {
34
        return new self(...$objects);
35
    }
36
37
    /** @inheritdoc */
38
    public function current(): MakesObjects
39
    {
40
        return parent::current();
41
    }
42
43
    /** @inheritdoc */
44
    public function from(array $input, Map $map): ContainsResultingObjects
45
    {
46
        $result = Result::fromArray([], $map);
47
        foreach ($this as $objects) {
48
            $result = $result->mergeWith($objects->from(
49
                $input,
50
                $result->identityMap()
51
            ));
52
        }
53
        return $result;
54
    }
55
}
56