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

StringField::checkMinimumConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
}