GenericHelper::convertToDateTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 2
cts 4
cp 0.5
crap 2.5
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