Passed
Pull Request — master (#11)
by Ryosuke
01:19
created

GenericHelper::convertToBoolean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Mpyw\OpenGraph;
4
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use Throwable;
8
9
/**
10
 * Trait GenericHelper
11
 */
12
trait GenericHelper
13
{
14
    /**
15
     * @param  string                 $value
16
     * @return null|DateTimeInterface
17
     */
18 1
    protected function convertToDateTime(string $value): ?DateTimeInterface
19
    {
20
        try {
21 1
            return new DateTimeImmutable($value);
22
        } catch (Throwable $e) {
23
            return null;
24
        }
25
    }
26
27
    /**
28
     * @param  string $value
29
     * @return bool
30
     */
31 1
    protected function convertToBoolean(string $value): bool
32
    {
33 1
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
34
    }
35
36
    /**
37
     * @param  null|string $string
38
     * @return null|string
39
     */
40 17
    protected function emptyStringAsNull(?string $string): ?string
41
    {
42 17
        return (string)$string !== '' ? (string)$string : null;
43
    }
44
}
45