Completed
Push — master ( e0b963...10913a )
by Jesse
02:36
created

Check::value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapping\Property;
5
6
use Stratadox\HydrationMapping\MapsProperty;
7
use Stratadox\Specification\Contract\Satisfiable;
8
9
/**
10
 * Checks whether the input value is accepted by the constraints.
11
 *
12
 * @author Stratadox
13
 */
14
final class Check implements MapsProperty
15
{
16
    private $constraint;
17
    private $mapping;
18
19
    private function __construct(Satisfiable $constraint, MapsProperty $mapping)
20
    {
21
        $this->constraint = $constraint;
22
        $this->mapping = $mapping;
23
    }
24
25
    /**
26
     * Creates a check for on a property mapping.
27
     *
28
     * @param Satisfiable  $constraint The constraint for the property.
29
     * @param MapsProperty $mapping    The mapping for the property.
30
     * @return MapsProperty            The checked property mapping.
31
     */
32
    public static function that(
33
        Satisfiable $constraint,
34
        MapsProperty $mapping
35
    ): MapsProperty {
36
        return new Check($constraint, $mapping);
37
    }
38
39
    /** @inheritdoc */
40
    public function name(): string
41
    {
42
        return $this->mapping->name();
43
    }
44
45
    /** @inheritdoc */
46
    public function value(array $data, $owner = null)
47
    {
48
        $value = $this->mapping->value($data, $owner);
49
        if ($this->constraint->isSatisfiedBy($value)) {
50
            return $value;
51
        }
52
        throw UnsatisfiedConstraint::itIsNotConsideredValid($this, $value);
53
    }
54
}
55