CastsTo.php 923 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
<?php

namespace Source\Sanitize;

use Carbon\Carbon;

trait CastsTo
{
    /**
Jacob Priddy's avatar
Jacob Priddy committed
10
11
     * @param string|null $date
     * @param string      $format
12
13
     * @return \Carbon\Carbon|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
14
    protected function castToCarbon(?string $date, $format = 'Y-m-d H:i:s'): ?Carbon
15
16
17
18
19
20
21
    {
        if (empty($date)) {
            return null;
        }

        return Carbon::createFromFormat($format, $date);
    }
Jacob Priddy's avatar
Jacob Priddy committed
22

Jacob Priddy's avatar
Jacob Priddy committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    /**
     * @param string|null $date
     * @return \Carbon\Carbon|null
     * @throws \Exception
     */
    protected function liberalCastToCarbon(?string $date): ?Carbon
    {
        if (empty($date)) {
            return null;
        }

        return new Carbon($date);
    }

Jacob Priddy's avatar
Jacob Priddy committed
37
38
39
40
41
42
    /**
     * @param string|null $int
     * @return int|null
     */
    protected function castToInt(?string $int): ?int
    {
43
        if ($int === null) {
Jacob Priddy's avatar
Jacob Priddy committed
44
45
46
47
48
            return null;
        }

        return (int)$int;
    }
49
}