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 | class GeopointField extends BaseField |
|||||||||||
6 | { |
|||||||||||
7 | protected function validateCastValue($val) |
|||||||||||
8 | { |
|||||||||||
9 | if (in_array($this->format(), ['array', 'object']) && is_string($val)) { |
|||||||||||
10 | try { |
|||||||||||
11 | $val = json_decode($val); |
|||||||||||
12 | } catch (\Exception $e) { |
|||||||||||
13 | throw $this->getValidationException($e->getMessage(), $val); |
|||||||||||
14 | } |
|||||||||||
15 | } |
|||||||||||
16 | switch ($this->format()) { |
|||||||||||
17 | case 'default': |
|||||||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
||||||||||||
18 | if (!is_string($val)) { |
|||||||||||
19 | throw $this->getValidationException('value must be a string', $val); |
|||||||||||
20 | } else { |
|||||||||||
21 | return $this->getNativeGeopoint(explode(',', $val)); |
|||||||||||
22 | } |
|||||||||||
23 | case 'array': |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
24 | if (!is_array($val)) { |
|||||||||||
25 | throw $this->getValidationException('value must be an array', $val); |
|||||||||||
26 | } else { |
|||||||||||
27 | return $this->getNativeGeopoint($val); |
|||||||||||
28 | } |
|||||||||||
29 | case 'object': |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
30 | if (!is_object($val)) { |
|||||||||||
31 | throw $this->getValidationException('value must be an object', $val); |
|||||||||||
32 | } elseif (!isset($val->lat) || !isset($val->lon)) { |
|||||||||||
33 | throw $this->getValidationException('object must contain lon and lat attributes', $val); |
|||||||||||
34 | } else { |
|||||||||||
35 | return $this->getNativeGeopoint([$val->lon, $val->lat]); |
|||||||||||
36 | } |
|||||||||||
37 | default: |
|||||||||||
38 | throw $this->getValidationException('invalid format', $val); |
|||||||||||
39 | } |
|||||||||||
40 | } |
|||||||||||
41 | ||||||||||||
42 | public static function type() |
|||||||||||
43 | { |
|||||||||||
44 | return 'geopoint'; |
|||||||||||
45 | } |
|||||||||||
46 | ||||||||||||
47 | protected function getNativeGeopoint($arr) |
|||||||||||
48 | { |
|||||||||||
49 | if (count($arr) != 2) { |
|||||||||||
50 | throw $this->getValidationException('lon,lat array must contain only lon,lat', json_encode($arr)); |
|||||||||||
51 | } else { |
|||||||||||
52 | list($lon, $lat) = $arr; |
|||||||||||
53 | $lon = (int) $lon; |
|||||||||||
54 | $lat = (int) $lat; |
|||||||||||
55 | if ( |
|||||||||||
56 | $lon > 180 || $lon < -180 |
|||||||||||
57 | || $lat > 90 or $lat < -90 |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
58 | ) { |
|||||||||||
59 | throw $this->getValidationException('invalid lon,lat values', json_encode($arr)); |
|||||||||||
60 | } else { |
|||||||||||
61 | return [$lon, $lat]; |
|||||||||||
62 | } |
|||||||||||
63 | } |
|||||||||||
64 | } |
|||||||||||
65 | } |
|||||||||||
66 |