AreOfType::string()   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 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Deserializer\Condition;
5
6
use function gettype;
7
use Stratadox\Specification\Contract\Specifies;
8
use Stratadox\Specification\Specifying;
9
10
/**
11
 * Condition that accepts a specific input type.
12
 *
13
 * Can be used to have the deserializer act differently upon receiving a
14
 * particular type of data. Generally combined with @see ConsistOfItems in order
15
 * to check for the data types of all items in a list.
16
 *
17
 * @author Stratadox
18
 */
19
final class AreOfType implements Specifies
20
{
21
    use Specifying;
22
23
    /** @var string */
24
    private $expectation;
25
26
    private function __construct(string $expectation)
27
    {
28
        $this->expectation = $expectation;
29
    }
30
31
    /**
32
     * Produces a condition that accepts boolean type input.
33
     *
34
     * @return Specifies The type enforcing condition.
35
     */
36
    public static function boolean(): Specifies
37
    {
38
        return new self('boolean');
39
    }
40
41
    /**
42
     * Produces a condition that accepts integer type input.
43
     *
44
     * @return Specifies The type enforcing condition.
45
     */
46
    public static function integer(): Specifies
47
    {
48
        return new self('integer');
49
    }
50
51
    /**
52
     * Produces a condition that accepts string type input.
53
     *
54
     * @return Specifies The type enforcing condition.
55
     */
56
    public static function string(): Specifies
57
    {
58
        return new self('string');
59
    }
60
61
    /** @inheritdoc */
62
    public function isSatisfiedBy($input): bool
63
    {
64
        return gettype($input) === $this->expectation;
65
    }
66
}
67