Completed
Push — master ( be4243...1976df )
by Ori
19:42
created

src/Fields/DurationField.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
use Carbon\CarbonInterval;
6
7
class DurationField extends BaseField
8
{
9
    protected function validateCastValue($val)
10
    {
11 View Code Duplication
        if (!is_string($val)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
            throw $this->getValidationException('must be string', $val);
13
        } else {
14
            $val = trim($val);
15
            try {
16
                // we create a dateInterval first, because it's more restrictive
17
                return CarbonInterval::instance(new \DateInterval($val));
18
            } catch (\Exception $e) {
19
                throw $this->getValidationException($e->getMessage(), $val);
20
            }
21
        }
22
    }
23
24
    public static function type()
25
    {
26
        return 'duration';
27
    }
28
29
    protected function checkAllowedValues($allowedValues, $val)
30
    {
31
        foreach ($allowedValues as $allowedValue) {
32
            if (
33
                $val->years == $allowedValue->years
34
                && $val->months == $allowedValue->months
35
                && $val->days == $allowedValue->days
36
                && $val->hours == $allowedValue->hours
37
                && $val->minutes == $allowedValue->minutes
38
                && $val->seconds == $allowedValue->seconds
39
            ) {
40
                return true;
41
            }
42
        }
43
44
        return false;
45
    }
46
}
47