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

Check   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A name() 0 3 1
A value() 0 7 2
A that() 0 5 1
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