ObjectExtractor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A withAlternative() 0 3 1
A extract() 0 21 5
A __construct() 0 3 1
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