Call::that()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapper\Instruction;
5
6
use Closure;
7
use Stratadox\Hydration\Mapping\Property\Check;
8
use Stratadox\Hydration\Mapping\Property\Dynamic\ClosureResult;
9
use Stratadox\HydrationMapper\InstructsHowToMap;
10
use Stratadox\HydrationMapping\MapsProperty;
11
use Stratadox\Specification\Contract\Satisfiable;
12
13
/**
14
 * Indicates that a closure should be called to hydrate this property.
15
 *
16
 * @package Stratadox\Hydrate
17
 * @author  Stratadox
18
 */
19
final class Call implements InstructsHowToMap
20
{
21
    private $function;
22
    private $constraint;
23
24
    private function __construct(Closure $function, ?Satisfiable $constraint)
25
    {
26
        $this->function = $function;
27
        $this->constraint = $constraint;
28
    }
29
30
    /**
31
     * Creates a closure-call instruction for the property.
32
     *
33
     * @param Closure $function The anonymous function to call while hydrating
34
     *                          this property.
35
     * @return Call             The instruction object.
36
     */
37
    public static function the(Closure $function): Call
38
    {
39
        return new Call($function, null);
40
    }
41
42
    /** @inheritdoc */
43
    public function that(Satisfiable $constraint): InstructsHowToMap
44
    {
45
        return new Call($this->function, $constraint);
46
    }
47
48
    /** @inheritdoc */
49
    public function followFor(string $property): MapsProperty
50
    {
51
        $mapping = ClosureResult::inProperty($property, $this->function);
52
        if (isset($this->constraint)) {
53
            $mapping = Check::that($this->constraint, $mapping);
54
        }
55
        return $mapping;
56
    }
57
}
58