Passed
Pull Request — main (#105)
by Andrey
12:50
created

Boolean::convertToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 2
    public function isTrue($value): bool
15
    {
16 2
        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 2
    public function isFalse($value): bool
27
    {
28 2
        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 6
    public function to($value): bool
39
    {
40 6
        return (bool) $this->parse($value);
41
    }
42
43
    /**
44
     * Getting a filtered value in a boolean view.
45
     *
46
     * @param  mixed  $value
47
     *
48
     * @return bool|null
49
     */
50 10
    public function parse($value): ?bool
51
    {
52 10
        if (is_null($value)) {
53 6
            return null;
54
        }
55
56 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
57
    }
58
59
    /**
60
     * Converts a boolean value to a string.
61
     *
62
     * @param  bool  $value
63
     *
64
     * @return string
65
     */
66
    public function convertToString(bool $value): string
67
    {
68
        return $value ? 'true' : 'false';
69
    }
70
}
71