Passed
Push — master ( 646380...d8c5da )
by Jesse
01:28
created

ObjectExtractor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\EntityState\Internal;
4
5
use function array_merge as these;
6
use function assert;
7
use function is_object;
8
use Stratadox\EntityState\PropertyState;
9
10
final class ObjectExtractor implements Extractor
11
{
12
    private $next;
13
14
    private function __construct(Extractor $next)
15
    {
16
        $this->next = $next;
17
    }
18
19
    public static function withAlternative(Extractor $next): Extractor
20
    {
21
        return new self($next);
22
    }
23
24
    public function extract(
25
        ExtractionRequest $request,
26
        Extractor $base = null
27
    ): array {
28
        if (!is_object($request->value())) {
29
            return $this->next->extract($request, $base);
30
        }
31
        assert($base !== null);
32
        $properties = [];
33
        foreach (ReflectionProperties::ofThe($request->value()) as $property) {
34
            $properties[] = $base->extract(
35
                $request->forProperty($property),
36
                $base
37
            );
38
        }
39
        if (empty($properties)) {
40
            return $request->isTheOwner() ?
41
                [] :
42
                [PropertyState::with((string) $request->objectName(), null)];
43
        }
44
        return these(...$properties);
45
    }
46
}
47