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

Stringifier::withCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\EntityState\Internal;
4
5
use Stratadox\EntityState\PropertyState;
6
use Stratadox\Specification\Contract\Satisfiable;
7
8
final class Stringifier implements Extractor
9
{
10
    private $next;
11
    private $condition;
12
13
    private function __construct(Extractor $next, Satisfiable $condition)
14
    {
15
        $this->next = $next;
16
        $this->condition = $condition;
17
    }
18
19
    public static function withCondition(
20
        Satisfiable $constraint,
21
        Extractor $next
22
    ): Extractor {
23
        return new self($next, $constraint);
24
    }
25
26
    public function extract(
27
        ExtractionRequest $request,
28
        Extractor $baseExtractor = null
29
    ): array {
30
        if ($this->condition->isSatisfiedBy($request->value())) {
31
            return [PropertyState::with(
32
                (string) $request->objectName(),
33
                (string) $request->value()
34
            )];
35
        }
36
        return $this->next->extract($request, $baseExtractor);
37
    }
38
}
39