KeyMatches::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Proxy\When;
4
5
use Stratadox\Specification\Contract\Satisfiable;
6
7
/**
8
 * When\KeyMatches. Constraint to check if the known data contains a particular
9
 * key/value pair.
10
 *
11
 * @author Stratadox
12
 */
13
final class KeyMatches implements Satisfiable
14
{
15
    private $key;
16
    private $value;
17
18
    private function __construct(string $key, $value)
19
    {
20
        $this->key = $key;
21
        $this->value = $value;
22
    }
23
24
    /**
25
     * Produces a constraint that is satisfied with data that contains this
26
     * particular key/value pair.
27
     *
28
     * @param string $key   The key to look for.
29
     * @param mixed  $value The value that is expected.
30
     * @return Satisfiable  The constraint.
31
     */
32
    public static function with(string $key, $value): Satisfiable
33
    {
34
        return new self($key, $value);
35
    }
36
37
    /** @inheritdoc */
38
    public function isSatisfiedBy($knownData): bool
39
    {
40
        return ($knownData[$this->key] ?? null) === $this->value;
41
    }
42
}
43