ForDataSets   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 46
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 3 1
A isSatisfiedBy() 0 3 1
A __construct() 0 4 1
A that() 0 5 1
A typeFor() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Deserializer;
5
6
use Stratadox\Specification\Contract\Satisfiable;
7
8
/**
9
 * Represents a deserialization option, consisting of a condition and a
10
 * deserializer.
11
 *
12
 * The embedded deserializer is used for data sets that pass the condition.
13
 *
14
 * @author Stratadox
15
 */
16
final class ForDataSets implements DeserializationOption
17
{
18
    /** @var Satisfiable */
19
    private $condition;
20
    /** @var Deserializer */
21
    private $deserialize;
22
23
    private function __construct(Satisfiable $condition, Deserializer $deserialize)
24
    {
25
        $this->condition = $condition;
26
        $this->deserialize = $deserialize;
27
    }
28
29
    /**
30
     * Produce an option for data sets that pass the condition.
31
     *
32
     * @param Satisfiable  $condition   The condition that must be satisfied by
33
     *                                  the data set.
34
     * @param Deserializer $deserialize The deserializer that will deserialize
35
     *                                  satisfying data sets.
36
     * @return DeserializationOption    The option object to supply to the
37
     *                                  @see OneOfThese::deserializers() method.
38
     */
39
    public static function that(
40
        Satisfiable $condition,
41
        Deserializer $deserialize
42
    ): DeserializationOption {
43
        return new ForDataSets($condition, $deserialize);
44
    }
45
46
    /** @inheritdoc */
47
    public function isSatisfiedBy($input): bool
48
    {
49
        return $this->condition->isSatisfiedBy($input);
50
    }
51
52
    /** @inheritdoc */
53
    public function from(array $input)
54
    {
55
        return $this->deserialize->from($input);
56
    }
57
58
    /** @inheritdoc */
59
    public function typeFor(array $input): string
60
    {
61
        return $this->deserialize->typeFor($input);
62
    }
63
}
64