1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace frictionlessdata\tableschema\Fields; |
4
|
|
|
|
5
|
|
|
class StringField extends BaseField |
6
|
|
|
{ |
7
|
|
|
public function inferProperties($val, $lenient = false) |
8
|
|
|
{ |
9
|
|
|
parent::inferProperties($val, $lenient); |
10
|
|
|
if (!$lenient) { |
11
|
|
|
if (is_string($val) && strpos($val, '@') !== false) { |
12
|
|
|
$this->descriptor->format = 'email'; |
13
|
|
|
} |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $val |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
* |
22
|
|
|
* @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException; |
23
|
|
|
*/ |
24
|
|
|
protected function validateCastValue($val) |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
$val = (string) $val; |
28
|
|
|
} catch (\Exception $e) { |
|
|
|
|
29
|
|
|
$val = json_encode($val); |
30
|
|
|
} |
31
|
|
|
switch ($this->format()) { |
32
|
|
|
case 'email': |
33
|
|
|
if (strpos($val, '@') === false) { |
34
|
|
|
throw $this->getValidationException('value is not a valid email', $val); |
35
|
|
|
} |
36
|
|
|
break; |
37
|
|
|
case 'uri': |
38
|
|
|
if (filter_var($val, FILTER_VALIDATE_URL) === false) { |
39
|
|
|
throw $this->getValidationException(null, $val); |
40
|
|
|
} |
41
|
|
|
break; |
42
|
|
|
case 'binary': |
43
|
|
|
$decoded = base64_decode($val, true); |
44
|
|
|
if ($decoded === false) { |
45
|
|
|
throw $this->getValidationException(null, $val); |
46
|
|
|
} |
47
|
|
|
break; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $val; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function type() |
54
|
|
|
{ |
55
|
|
|
return 'string'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getInferIdentifier($lenient = false) |
59
|
|
|
{ |
60
|
|
|
$inferId = parent::getInferIdentifier(); |
61
|
|
|
$format = $this->format(); |
62
|
|
|
if (!$lenient && !empty($format)) { |
63
|
|
|
$inferId .= ':'.$this->format(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $inferId; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function checkMinimumConstraint($val, $minConstraint) |
70
|
|
|
{ |
71
|
|
|
throw $this->getValidationException('minimum constraint is not supported for string fields', $val); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function checkMaximumConstraint($val, $maxConstraint) |
75
|
|
|
{ |
76
|
|
|
throw $this->getValidationException('maximum constraint is not supported for string fields', $val); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.