ForDataSets::that()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
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