Strict   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 28

12 Methods

Rating   Name   Duplication   Size   Complexity  
A integer() 0 3 1
B typeof() 0 19 8
A type() 0 3 1
A boolean() 0 3 1
A arrayable() 0 3 1
A string() 0 3 1
A float() 0 3 1
A __construct() 0 3 1
B validate() 0 19 8
A __callStatic() 0 3 1
A resource() 0 3 1
A object() 0 7 3
1
<?php
2
3
namespace kartavik\Support;
4
5
use kartavik\Support\Exception\UnprocessedType;
6
7
/**
8
 * Class Strict
9
 * @package kartavik\Support
10
 */
11
class Strict implements StrictInterface
12
{
13
    /** @var string */
14
    private $type;
15
16
    protected function __construct(string $type)
17
    {
18
        $this->type = $type;
19
    }
20
21
    final public function type(): string
22
    {
23
        return $this->type;
24
    }
25
26
    /**
27
     * @param mixed $var
28
     *
29
     * @return bool
30
     */
31
    public function validate($var): bool
32
    {
33
        switch ($this->type) {
34
            case static::STRING:
35
                return is_string($var);
36
            case static::INTEGER:
37
                return is_int($var);
38
            case static::FLOAT:
39
                return is_float($var);
40
            case static::ARRAYABLE:
41
                return is_array($var);
42
            case static::BOOLEAN:
43
                return is_bool($var);
44
            case static::RESOURCE:
45
                return is_resource($var);
46
            case static::OBJECT:
47
                return is_object($var);
48
            default:
49
                return $var instanceof $this->type;
50
        }
51
    }
52
53
    public static function string(): StrictInterface
54
    {
55
        return new static(static::STRING);
56
    }
57
58
    public static function integer(): StrictInterface
59
    {
60
        return new static(static::INTEGER);
61
    }
62
63
    public static function float(): StrictInterface
64
    {
65
        return new static(static::FLOAT);
66
    }
67
68
    public static function arrayable(): StrictInterface
69
    {
70
        return new static(static::ARRAYABLE);
71
    }
72
73
    public static function boolean(): StrictInterface
74
    {
75
        return new static(static::BOOLEAN);
76
    }
77
78
    public static function resource(): StrictInterface
79
    {
80
        return new static(static::RESOURCE);
81
    }
82
83
    public static function object(string $className = self::OBJECT): StrictInterface
84
    {
85
        if ($className === static::OBJECT || class_exists($className)) {
86
            return new static($className);
87
        }
88
89
        throw new Exception\UnprocessedType($className);
90
    }
91
92
    public static function typeof($var): StrictInterface
93
    {
94
        switch (true) {
95
            case is_string($var):
96
                return Strict::string();
97
            case is_int($var):
98
                return Strict::integer();
99
            case is_float($var):
100
                return Strict::float();
101
            case is_array($var):
102
                return Strict::arrayable();
103
            case is_resource($var):
104
                return Strict::resource();
105
            case is_bool($var):
106
                return Strict::boolean();
107
            case is_object($var):
108
                return Strict::object(get_class($var));
109
            default:
110
                throw new UnprocessedType(gettype($var));
111
        }
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param array $arguments
117
     *
118
     * @return mixed
119
     */
120
    public static function __callStatic($name, $arguments)
121
    {
122
        return static::object($name);
123
    }
124
}
125