summaryrefslogtreecommitdiff
path: root/src/Http/Validation/Rules/StringInputLength.php
blob: 7b5c248b295207e12ce7f1115316480681149140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

namespace Engelsystem\Http\Validation\Rules;

use DateTime;
use Illuminate\Support\Str;
use Throwable;

trait StringInputLength
{
    /**
     * Use the input length of a string
     *
     * @param mixed $input
     * @return bool
     */
    public function validate($input): bool
    {
        if (
            is_string($input)
            && !is_numeric($input)
            && !$this->isDateTime($input)
        ) {
            $input = Str::length($input);
        }

        return parent::validate($input);
    }

    /**
     * @param mixed $input
     * @return bool
     */
    protected function isDateTime($input): bool
    {
        try {
            new DateTime($input);
        } catch (Throwable $e) {
            return false;
        }

        return true;
    }
}