HaveTheDiscriminatorValue::of()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Deserializer\Condition;
5
6
use Stratadox\Specification\Contract\Specifies;
7
use Stratadox\Specification\Specifying;
8
9
/**
10
 * Condition that accepts data with a specific discriminator key and value.
11
 *
12
 * Particularly useful when deserializing relational data into polymorphic
13
 * object structures.
14
 *
15
 * @author Stratadox
16
 */
17
final class HaveTheDiscriminatorValue implements Specifies
18
{
19
    use Specifying;
20
21
    /** @var string */
22
    private $key;
23
    /** @var string */
24
    private $value;
25
26
    private function __construct(string $key, string $value)
27
    {
28
        $this->key = $key;
29
        $this->value = $value;
30
    }
31
32
    /**
33
     * Produces a condition that checks a discriminator key/value combination.
34
     *
35
     * @param string $key   The discriminator key, for instance a column name.
36
     * @param string $value The discriminator value to be triggered by.
37
     * @return Specifies    The discriminating condition.
38
     */
39
    public static function of(string $key, string $value): Specifies
40
    {
41
        return new self($key, $value);
42
    }
43
44
    /** @inheritdoc */
45
    public function isSatisfiedBy($input): bool
46
    {
47
        return isset($input[$this->key])
48
            && (string) $input[$this->key] === $this->value;
49
    }
50
}
51