HaveTheDiscriminatorValue   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isSatisfiedBy() 0 4 2
A of() 0 3 1
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