StringEnsurance   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 190
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isEqualTo() 0 6 1
A isNotEqualTo() 0 6 1
A matches() 0 6 1
A hasLengthOf() 0 7 1
A isShorterThan() 0 7 1
A isShorterOrEqualTo() 0 7 1
A isLongerThan() 0 7 1
A isLongerOrEqualTo() 0 7 1
A beginsWith() 0 6 1
A endsWith() 0 7 1
A isCallable() 0 6 1
A isClass() 0 6 1
A isJson() 0 8 1
A isXml() 0 13 1
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use DOMDocument;
6
7
/**
8
 * Class StringEnsurance
9
 * @package Dgame\Ensurance
10
 */
11
final class StringEnsurance implements EnsuranceInterface
12
{
13
    use EnsuranceTrait;
14
15
    /**
16
     * StringEnsurance constructor.
17
     *
18
     * @param EnsuranceInterface $ensurance
19
     */
20
    public function __construct(EnsuranceInterface $ensurance)
21
    {
22
        $this->transferEnsurance($ensurance);
23 12
    }
24
25 12
    /**
26 12
     * @param string $str
27
     *
28
     * @return StringEnsurance
29
     */
30
    public function isEqualTo(string $str): self
31
    {
32
        $this->ensure($this->value === $str)->orThrow('"%s" is not equal to "%s"', $this->value, $str);
33 1
34
        return $this;
35 1
    }
36
37 1
    /**
38
     * @param string $str
39
     *
40
     * @return StringEnsurance
41
     */
42
    public function isNotEqualTo(string $str): self
43
    {
44
        $this->ensure($this->value !== $str)->orThrow('"%s" is equal to "%s"', $this->value, $str);
45 1
46
        return $this;
47 1
    }
48
49 1
    /**
50
     * @param string $pattern
51
     *
52
     * @return StringEnsurance
53
     */
54
    public function matches(string $pattern): self
55
    {
56
        $this->ensure((bool) preg_match($pattern, $this->value))->orThrow('"%s" does not match pattern "%s"', $this->value, $pattern);
57 1
58
        return $this;
59 1
    }
60
61 1
    /**
62
     * @param int $length
63
     *
64
     * @return StringEnsurance
65
     */
66
    public function hasLengthOf(int $length): self
67
    {
68
        $len = strlen($this->value);
69 1
        $this->ensure($len === $length)->orThrow('"%s" (%d) has not the length of %d', $this->value, $len, $length);
70
71 1
        return $this;
72 1
    }
73
74 1
    /**
75
     * @param int $length
76
     *
77
     * @return StringEnsurance
78
     */
79
    public function isShorterThan(int $length): self
80
    {
81
        $len = strlen($this->value);
82 1
        $this->ensure($len < $length)->orThrow('"%s" (%d) is not shorter than %d', $this->value, $len, $length);
83
84 1
        return $this;
85 1
    }
86
87 1
    /**
88
     * @param int $length
89
     *
90
     * @return StringEnsurance
91
     */
92
    public function isShorterOrEqualTo(int $length): self
93
    {
94
        $len = strlen($this->value);
95 1
        $this->ensure($len <= $length)->orThrow('"%s" (%d) is not shorter or equal to %d', $this->value, $len, $length);
96
97 1
        return $this;
98 1
    }
99
100 1
    /**
101
     * @param int $length
102
     *
103
     * @return StringEnsurance
104
     */
105
    public function isLongerThan(int $length): self
106
    {
107
        $len = strlen($this->value);
108 1
        $this->ensure($len > $length)->orThrow('"%s" (%d) is longer than %d', $this->value, $len, $length);
109
110 1
        return $this;
111 1
    }
112
113 1
    /**
114
     * @param int $length
115
     *
116
     * @return StringEnsurance
117
     */
118
    public function isLongerOrEqualTo(int $length): self
119
    {
120
        $len = strlen($this->value);
121 1
        $this->ensure($len >= $length)->orThrow('"%s" (%d) is not longer or equal to %d', $this->value, $len, $length);
122
123 1
        return $this;
124 1
    }
125
126 1
    /**
127
     * @param string $str
128
     *
129
     * @return StringEnsurance
130
     */
131
    public function beginsWith(string $str): self
132
    {
133
        $this->ensure(strpos($this->value, $str) === 0)->orThrow('"%s" did not begin with "%s"', $this->value, $str);
134 1
135
        return $this;
136 1
    }
137 1
138
    /**
139 1
     * @param string $str
140
     *
141
     * @return StringEnsurance
142
     */
143
    public function endsWith(string $str): self
144
    {
145
        $len = strlen($str);
146
        $this->ensure(substr($this->value, $len * -1) === $str)->orThrow('"%s" did not end with "%s"', $this->value, $str);
147 1
148
        return $this;
149 1
    }
150 1
151
    /**
152 1
     * @return StringEnsurance
153
     */
154
    public function isCallable(): self
155
    {
156
        $this->ensure(is_callable($this->value))->orThrow('"%s" is not a callable', $this->value);
157
158 1
        return $this;
159
    }
160 1
161 1
    /**
162
     * @return ReflectionEnsurance
163
     * @throws \ReflectionException
164
     */
165
    public function isClass(): ReflectionEnsurance
166 1
    {
167
        $this->ensure(class_exists($this->value))->orThrow('"%s" is not a callable', $this->value);
168 1
169
        return new ReflectionEnsurance($this);
170 1
    }
171
172
    /**
173
     * @return StringEnsurance
174
     */
175
    public function isJson(): self
176
    {
177
        json_decode($this->value, true);
178
179
        $this->ensure(json_last_error() === JSON_ERROR_NONE);
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return StringEnsurance
186
     */
187
    public function isXml(): self
188
    {
189
        libxml_use_internal_errors(false);
190
191
        try {
192
            $doc = new DOMDocument();
193
            $this->ensure(@$doc->loadXML($this->value));
194
        } finally {
195
            libxml_clear_errors();
196
        }
197
198
        return $this;
199
    }
200
}
201