Assert::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 1
nop 2
crap 6
1
<?php namespace Chekote\NounStore;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
6
/**
7
 * Makes assertions regarding store data.
8
 */
9
class Assert
10
{
11
    protected Store $store;
12
13
    protected Key $keyService;
14
15
    /**
16
     * Assert constructor.
17
     *
18
     * @param Store    $store      the store to assert against.
19
     * @param Key|null $keyService the keyService to use for key parsing/building. Will use the default Key service
20
     *                             if not specified.
21
     *
22
     * @codeCoverageIgnore
23
     */
24
    public function __construct(Store $store, Key $keyService = null)
25
    {
26
        $this->store = $store;
27
        $this->keyService = $keyService ?: Key::getInstance();
28
    }
29
30
    /**
31
     * Asserts that a value has been stored for the specified key.
32
     *
33
     * @see    Key::build()
34
     * @see    Key::parseNoun()
35
     * @param  string                   $key The key to check. Supports nth notation.
36
     * @throws OutOfBoundsException     if a value has not been stored for the specified key.
37
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
38
     *                                  that does not match the index.
39
     * @return mixed                    The value.
40
     */
41
    public function keyExists($key): mixed
42
    {
43 4
        if (!$this->store->keyExists($key)) {
44
            throw new OutOfBoundsException("Entry '$key' was not found in the store.");
45 4
        }
46
47 3
        return $this->store->get($key);
48 1
    }
49 1
50
    /**
51
     * Asserts that the key's value contains the specified string.
52
     *
53 2
     * @see    Key::build()
54
     * @see    Key::parseNoun()
55
     * @param  string                   $key    The key to check. Supports nth notation.
56
     * @param  string                   $needle The value expected to be contained within the key's value.
57
     * @throws AssertionFailedException If the key's value does not contain the specified string.
58
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
59
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
60
     *                                  that does not match the index.
61
     * @return mixed                    The key's value.
62
     */
63
    public function keyValueContains($key, $needle): mixed
64
    {
65
        $haystack = $this->keyExists($key);
66
67 5
        if (!$this->store->keyValueContains($key, $needle)) {
68
            throw new AssertionFailedException("Entry '$key' does not contain '$needle'");
69 5
        }
70
71 4
        return $haystack;
72
    }
73 3
74 1
    /**
75 1
     * Asserts that the key's value matches the specified value.
76
     *
77
     * @see    Key::build()
78 2
     * @see    Key::parseNoun()
79
     * @param  string                   $key   The key to check. Supports nth notation.
80
     * @param  mixed                    $value The expected value.
81
     * @throws AssertionFailedException If the key's value does not match the specified value.
82
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
83
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
84
     *                                  that does not match the index.
85
     */
86
    public function keyValueIs($key, $value): void
87
    {
88
        if ($this->keyExists($key) != $value) {
89
            throw new AssertionFailedException("Entry '$key' does not match '" . print_r($value, true) . "'");
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            throw new AssertionFailedException("Entry '$key' does not match '" . /** @scrutinizer ignore-type */ print_r($value, true) . "'");
Loading history...
90
        }
91 5
    }
92
93 5
    /**
94
     * Asserts that the key's value matches the specified class instance.
95 4
     *
96
     * @template T
97 3
     * @see      Key::build()
98 1
     * @see      Key::parseNoun()
99 1
     * @param  string                   $key   The key to check. Supports nth notation.
100
     * @param  class-string<T>          $class The expected class instance.
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
101
     * @throws AssertionFailedException If the key's value is not an instance of the specified class.
102 2
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
103
     * @return T                        The key's value.
104
     */
105
    public function keyIsClass(string $key, string $class): mixed
106
    {
107
        if ($this->keyExists($key) && !$this->store->keyIsClass($key, $class)) {
108
            throw new AssertionFailedException("Entry '$key' does not match instance of '$class'");
109
        }
110
111
        return $this->store->get($key);
112
    }
113
}
114