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 Maps; |
||
4 | |||
5 | use DataValues\Geo\Parsers\GeoCoordinateParser; |
||
6 | use DataValues\Geo\Values\LatLongValue; |
||
7 | use Maps\Elements\Line; |
||
8 | use ValueParsers\StringValueParser; |
||
9 | |||
10 | /** |
||
11 | * ValueParser that parses the string representation of a line. |
||
12 | * |
||
13 | * @since 3.0 |
||
14 | * |
||
15 | * @licence GNU GPL v2+ |
||
16 | * @author Kim Eik |
||
17 | * @author Jeroen De Dauw < [email protected] > |
||
18 | */ |
||
19 | class LineParser extends StringValueParser { |
||
20 | |||
21 | protected $supportGeocoding = true; |
||
22 | |||
23 | // TODO: use options |
||
24 | protected $metaDataSeparator = '~'; |
||
25 | |||
26 | /** |
||
27 | * @see StringValueParser::stringParse |
||
28 | * |
||
29 | * @since 3.0 |
||
30 | * |
||
31 | * @param string $value |
||
32 | * |
||
33 | * @return Line |
||
34 | */ |
||
35 | public function stringParse( $value ) { |
||
36 | $parts = explode( $this->metaDataSeparator , $value ); |
||
37 | |||
38 | $line = $this->constructShapeFromLatLongValues( $this->parseCoordinates( |
||
39 | explode( ':' , array_shift( $parts ) ) |
||
40 | ) ); |
||
41 | |||
42 | $this->handleCommonParams( $parts, $line ); |
||
43 | |||
44 | return $line; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @since 3.0 |
||
49 | * |
||
50 | * @param string[] $coordinateStrings |
||
51 | * |
||
52 | * @return LatLongValue[] |
||
53 | */ |
||
54 | protected function parseCoordinates( array $coordinateStrings ) { |
||
55 | $coordinates = []; |
||
56 | $coordinateParser = new GeoCoordinateParser( new \ValueParsers\ParserOptions() ); |
||
57 | |||
58 | $supportsGeocoding = $this->supportGeocoding && \Maps\Geocoders::canGeocode(); |
||
59 | |||
60 | foreach ( $coordinateStrings as $coordinateString ) { |
||
61 | if ( $supportsGeocoding ) { |
||
62 | $coordinate = \Maps\Geocoders::attemptToGeocode( $coordinateString ); |
||
63 | |||
64 | if ( $coordinate === false ) { |
||
0 ignored issues
–
show
|
|||
65 | // TODO |
||
66 | } |
||
67 | else { |
||
68 | $coordinates[] = $coordinate; |
||
69 | } |
||
70 | } |
||
71 | else { |
||
72 | $coordinates[] = $coordinateParser->parse( $coordinateString ); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | return $coordinates; |
||
77 | } |
||
78 | |||
79 | protected function constructShapeFromLatLongValues( array $locations ) { |
||
80 | return new Line( $locations ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * This method requires that parameters are positionally correct, |
||
85 | * 1. Link (one parameter) or bubble data (two parameters) |
||
86 | * 2. Stroke data (three parameters) |
||
87 | * 3. Fill data (two parameters) |
||
88 | * e.g ...title~text~strokeColor~strokeOpacity~strokeWeight~fillColor~fillOpacity |
||
89 | * |
||
90 | * @since 3.0 |
||
91 | * |
||
92 | * @param array $params |
||
93 | * @param Line $line |
||
94 | */ |
||
95 | protected function handleCommonParams( array &$params, Line &$line ) { |
||
96 | //Handle bubble and link parameters |
||
97 | |||
98 | //create link data |
||
99 | $linkOrTitle = array_shift( $params ); |
||
100 | if ( $link = $this->isLinkParameter( $linkOrTitle ) ) { |
||
101 | $this->setLinkFromParameter( $line , $link ); |
||
102 | } else { |
||
103 | //create bubble data |
||
104 | $this->setBubbleDataFromParameter( $line , $params , $linkOrTitle ); |
||
105 | } |
||
106 | |||
107 | |||
108 | //handle stroke parameters |
||
109 | if ( $color = array_shift( $params ) ) { |
||
110 | $line->setStrokeColor( $color ); |
||
111 | } |
||
112 | |||
113 | if ( $opacity = array_shift( $params ) ) { |
||
114 | $line->setStrokeOpacity( $opacity ); |
||
115 | } |
||
116 | |||
117 | if ( $weight = array_shift( $params ) ) { |
||
118 | $line->setStrokeWeight( $weight ); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | protected function setBubbleDataFromParameter( Line &$line , &$params , $title ) { |
||
123 | if ( $title ) { |
||
124 | $line->setTitle( $title ); |
||
125 | } |
||
126 | if ( $text = array_shift( $params ) ) { |
||
127 | $line->setText( $text ); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | protected function setLinkFromParameter( Line &$line , $link ) { |
||
132 | if ( filter_var( $link , FILTER_VALIDATE_URL , FILTER_FLAG_SCHEME_REQUIRED ) ) { |
||
133 | $line->setLink( $link ); |
||
134 | } else { |
||
135 | $title = \Title::newFromText( $link ); |
||
136 | $line->setLink( $title->getFullURL() ); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Checks if a string is prefixed with link: |
||
142 | * @static |
||
143 | * @param $link |
||
144 | * @return bool|string |
||
145 | * @since 2.0 |
||
146 | */ |
||
147 | private function isLinkParameter( $link ) { |
||
148 | if ( strpos( $link , 'link:' ) === 0 ) { |
||
149 | return substr( $link , 5 ); |
||
150 | } |
||
151 | |||
152 | return false; |
||
153 | } |
||
154 | |||
155 | } |
||
156 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.