Passed
Pull Request — main (#80)
by Andrey
13:02
created

Boolean::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
final class Boolean
6
{
7
    /**
8
     * Determines if the value is `true`, otherwise it will return `false`.
9
     *
10
     * @param  mixed  $value
11
     *
12
     * @return bool
13
     */
14 6
    public function isTrue($value): bool
15
    {
16 6
        return $this->to($value) === true;
17
    }
18
19
    /**
20
     * Determines if the value is `false`, otherwise it will return `true`.
21
     *
22
     * @param  mixed  $value
23
     *
24
     * @return bool
25
     */
26 6
    public function isFalse($value): bool
27
    {
28 6
        return $this->to($value) === false;
29
    }
30
31
    /**
32
     * Converts a value to a boolean type.
33
     *
34
     * @param  mixed  $value
35
     *
36
     * @return bool
37
     */
38 2
    public function to($value): bool
39
    {
40 2
        return (bool) $this->parse($value);
41 2
    }
42
43
    /**
44 2
     * Getting a filtered value in a boolean view.
45 2
     *
46
     * @param  mixed  $value
47
     *
48 2
     * @return bool|null
49
     */
50
    public function parse($value): ?bool
51
    {
52
        if (is_null($value)) {
53
            return null;
54
        }
55
56
        return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
57
    }
58
}
59