Completed
Push — master ( 29cde4...77c4c6 )
by Ori
03:02
created

StringField   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 52
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A inferProperties() 0 9 3
A validateCastValue() 0 9 3
A type() 0 4 1
A getInferIdentifier() 0 9 3
A checkMinimumConstraint() 0 4 1
A checkMaximumConstraint() 0 4 1
1
<?php
2
namespace frictionlessdata\tableschema\Fields;
3
4
use frictionlessdata\tableschema\Exceptions\FieldValidationException;
5
6
class StringField extends BaseField
7
{
8
    public function inferProperties($val, $lenient=false)
9
    {
10
        parent::inferProperties($val, $lenient);
11
        if (!$lenient) {
12
            if (strpos($val, "@") !== false) {
13
                $this->descriptor->format = "email";
14
            }
15
        }
16
    }
17
18
    /**
19
     * @param mixed $val
20
     * @return string
21
     * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
22
     */
23
    public function validateCastValue($val)
24
    {
25
        $val = parent::validateCastValue($val);
26
        if ($this->format() == "email" && strpos($val, "@") === false) {
27
            throw $this->getValidationException("value is not a valid email", $val);
28
        } else {
29
            return $val;
30
        }
31
    }
32
33
    public static function type()
34
    {
35
        return "string";
36
    }
37
38
    public function getInferIdentifier($lenient=false)
39
    {
40
        $inferId = parent::getInferIdentifier();
41
        $format = $this->format();
42
        if (!$lenient && !empty($format)) {
43
            $inferId .= ":".$this->format();
44
        };
45
        return $inferId;
46
    }
47
48
    protected function checkMinimumConstraint($val, $minConstraint)
49
    {
50
        throw $this->getValidationException("minimum constraint is not supported for string fields", $val);
51
    }
52
53
    protected function checkMaximumConstraint($val, $maxConstraint)
54
    {
55
        throw $this->getValidationException("maximum constraint is not supported for string fields", $val);
56
    }
57
}