Completed
Push — master ( fbe049...cd378b )
by Ori
07:11
created

DurationField::checkAllowedValues()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 11
nc 3
nop 2
dl 0
loc 17
rs 7.7777
c 0
b 0
f 0
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
Duplication introduced by
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