Boolean::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the "andrey-helldar/support" project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Andrey Helldar <[email protected]>
9
 *
10
 * @copyright 2021 Andrey Helldar
11
 *
12
 * @license MIT
13
 *
14
 * @see https://github.com/andrey-helldar/support
15
 */
16
17
namespace Helldar\Support\Helpers;
18
19
class Boolean
20
{
21
    /**
22
     * Determines if the value is `true`, otherwise it will return `false`.
23
     *
24
     * @param  mixed  $value
25
     *
26
     * @return bool
27
     */
28 2
    public function isTrue($value): bool
29
    {
30 2
        return $this->to($value) === true;
31
    }
32
33
    /**
34
     * Determines if the value is `false`, otherwise it will return `true`.
35
     *
36
     * @param  mixed  $value
37
     *
38
     * @return bool
39
     */
40 2
    public function isFalse($value): bool
41
    {
42 2
        return $this->to($value) === false;
43
    }
44
45
    /**
46
     * Converts a value to a boolean type.
47
     *
48
     * @param  mixed  $value
49
     *
50
     * @return bool
51
     */
52 6
    public function to($value): bool
53
    {
54 6
        return (bool) $this->parse($value);
55
    }
56
57
    /**
58
     * Getting a filtered value in a boolean view.
59
     *
60
     * @param  mixed  $value
61
     *
62
     * @return bool|null
63
     */
64 10
    public function parse($value): ?bool
65
    {
66 10
        if (is_null($value)) {
67 6
            return null;
68
        }
69
70 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
71
    }
72
73
    /**
74
     * Converts a boolean value to a string.
75
     *
76
     * @param  bool  $value
77
     *
78
     * @return string
79
     */
80 2
    public function convertToString(bool $value): string
81
    {
82 2
        return $value ? 'true' : 'false';
83
    }
84
}
85