@@ -70,8 +70,8 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO
7070
7171 $ revCode = '' ;
7272
73- $ latVal = round (($ latitude +self ::LATITUDE_MAX )*self ::LAT_INTEGER_MULTIPLIER *1e6 )/1e6 ;
74- $ lngVal = round (($ longitude +self ::LONGITUDE_MAX )*self ::LNG_INTEGER_MULTIPLIER *1e6 )/1e6 ;
73+ $ latVal = ( int ) round (($ latitude +self ::LATITUDE_MAX )*self ::LAT_INTEGER_MULTIPLIER *1e6 )/1e6 ;
74+ $ lngVal = ( int ) round (($ longitude +self ::LONGITUDE_MAX )*self ::LNG_INTEGER_MULTIPLIER *1e6 )/1e6 ;
7575
7676 if ($ codeLength >self ::PAIR_CODE_LENGTH ){
7777 for ($ i =0 ;$ i <self ::GRID_CODE_LENGTH ;$ i ++){
@@ -83,8 +83,8 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO
8383 $ lngVal /= self ::GRID_COLUMNS ;
8484 }
8585 }else {
86- $ latVal = $ latVal /pow (self ::GRID_ROWS ,self ::GRID_CODE_LENGTH );
87- $ lngVal = $ lngVal /pow (self ::GRID_COLUMNS ,self ::GRID_CODE_LENGTH );
86+ $ latVal = ( int ) ( $ latVal /pow (self ::GRID_ROWS ,self ::GRID_CODE_LENGTH ) );
87+ $ lngVal = ( int ) ( $ lngVal /pow (self ::GRID_COLUMNS ,self ::GRID_CODE_LENGTH ) );
8888 }
8989
9090 for ($ i =0 ;$ i <self ::PAIR_CODE_LENGTH /2 ;$ i ++){
@@ -146,13 +146,13 @@ public function decode(){
146146 for ($ i =0 ;$ i <min (strlen ($ clean ),self ::PAIR_CODE_LENGTH );$ i +=2 ){
147147 $ latPlaceVal /= self ::ENCODING_BASE ;
148148 $ lngPlaceVal /= self ::ENCODING_BASE ;
149- $ latVal += strpos (self ::CODE_ALPHABET ,substr ($ clean ,$ i ,1 )) * $ latPlaceVal ;
150- $ latVal += strpos (self ::CODE_ALPHABET ,substr ($ clean ,$ i +1 ,1 )) * $ lngPlaceVal ;
149+ $ latVal += self :: indexOf (self ::CODE_ALPHABET ,substr ($ clean ,$ i ,1 )) * $ latPlaceVal ;
150+ $ lngVal += self :: indexOf (self ::CODE_ALPHABET ,substr ($ clean ,$ i +1 ,1 )) * $ lngPlaceVal ;
151151 }
152152 for ($ i =self ::PAIR_CODE_LENGTH ;$ i <min (strlen ($ clean ),self ::MAX_DIGIT_COUNT );$ i ++){
153153 $ latPlaceVal /= self ::GRID_ROWS ;
154154 $ lngPlaceVal /= self ::GRID_COLUMNS ;
155- $ digit = strpos (self ::CODE_ALPHABET ,substr ($ clean ,$ i ,1 ));
155+ $ digit = self :: indexOf (self ::CODE_ALPHABET ,substr ($ clean ,$ i ,1 ));
156156 $ row = $ digit /self ::GRID_COLUMNS ;
157157 $ col = $ digit %self ::GRID_COLUMNS ;
158158 $ latVal += $ row *$ latPlaceVal ;
@@ -179,7 +179,7 @@ public static function decodeByCode($code){
179179 * @return bool
180180 */
181181 public function isFull (){
182- return strpos ($ this ->code ,self ::SEPARATOR )= ==self ::SEPARATOR_POSITION ;
182+ return self :: indexOf ($ this ->code ,self ::SEPARATOR )==self ::SEPARATOR_POSITION ;
183183 }
184184
185185 /**
@@ -195,7 +195,7 @@ public static function isFullByCode($code){
195195 * @return bool
196196 */
197197 public function isShort (){
198- return strpos ($ this ->code ,self ::SEPARATOR )>=0 && strpos ($ this ->code ,self ::SEPARATOR )<self ::SEPARATOR_POSITION ;
198+ return self :: indexOf ($ this ->code ,self ::SEPARATOR )>=0 && self :: indexOf ($ this ->code ,self ::SEPARATOR )<self ::SEPARATOR_POSITION ;
199199 }
200200
201201 /**
@@ -211,7 +211,7 @@ public static function isShortByCode($code){
211211 * @return bool
212212 */
213213 public function isPadded (){
214- return strpos ($ this ->code ,self ::PADDING_CHARACTER )>=0 ;
214+ return self :: indexOf ($ this ->code ,self ::PADDING_CHARACTER )>=0 ;
215215 }
216216
217217 /**
@@ -228,7 +228,7 @@ public function shorten($referenceLatitude,$referenceLongitude){
228228 throw new InvalidArgumentException ('shorten() method could only be called on a full code. ' );
229229 }
230230 if ($ this ->isPadded ()){
231- throw new InvalidArgumentException ('shorten() method can not be called on a padded code. ' );
231+ throw new InvalidArgumentException ('shorten() method can not be called on a padded code. ' );
232232 }
233233
234234 $ codeArea = $ this ->decode ();
@@ -254,7 +254,7 @@ public function recover($referenceLatitude,$referenceLongitude){
254254 $ referenceLatitude = self ::clipLatitude ($ referenceLatitude );
255255 $ referenceLongitude = self ::normalizeLongitude ($ referenceLongitude );
256256
257- $ digitsToRecover = self ::SEPARATOR_POSITION - strpos ($ this ->code ,self ::SEPARATOR );
257+ $ digitsToRecover = self ::SEPARATOR_POSITION - self :: indexOf ($ this ->code ,self ::SEPARATOR );
258258 $ prefixPrecision = pow (self ::ENCODING_BASE ,2 -($ digitsToRecover /2 ));
259259
260260 $ recoveredPrefix = substr ((new self ($ referenceLatitude ,$ referenceLongitude ))->getCode (),0 ,$ digitsToRecover );
@@ -305,30 +305,30 @@ public static function isValidCode($code){
305305 }
306306 $ code = strtoupper ($ code );
307307
308- $ separatorPosition = strpos ($ code ,self ::SEPARATOR );
309- if ($ separatorPosition === false ){
308+ $ separatorPosition = self :: indexOf ($ code ,self ::SEPARATOR );
309+ if ($ separatorPosition ==- 1 ){
310310 return false ;
311311 }
312- if ($ separatorPosition !=strpos ($ code ,self ::SEPARATOR )){
312+ if ($ separatorPosition !== self :: indexOf ($ code ,self ::SEPARATOR )){
313313 return false ;
314314 }
315315 if ($ separatorPosition %2 !==0 || $ separatorPosition >self ::SEPARATOR_POSITION ){
316316 return false ;
317317 }
318318
319319 if ($ separatorPosition ==self ::SEPARATOR_POSITION ){
320- if (strpos (self ::CODE_ALPHABET ,substr ($ code ,0 ,1 ))>8 ){
320+ if (self :: indexOf (self ::CODE_ALPHABET ,substr ($ code ,0 ,1 ))>8 ){
321321 return false ;
322322 }
323323
324- if (strpos (self ::CODE_ALPHABET ,substr ($ code ,1 ,1 ))>17 ){
324+ if (self :: indexOf (self ::CODE_ALPHABET ,substr ($ code ,1 ,1 ))>17 ){
325325 return false ;
326326 }
327327 }
328328
329329 $ paddingStarted = false ;
330330 for ($ i =0 ;$ i <$ separatorPosition ;$ i ++){
331- if (strpos (self ::CODE_ALPHABET ,substr ($ code ,$ i ,1 ))=== false && substr ($ code ,$ i ,1 )!==self ::PADDING_CHARACTER ){
331+ if (self :: indexOf (self ::CODE_ALPHABET ,substr ($ code ,$ i ,1 ))==- 1 && substr ($ code ,$ i ,1 )!==self ::PADDING_CHARACTER ){
332332 return false ;
333333 }
334334 if ($ paddingStarted ){
@@ -355,7 +355,7 @@ public static function isValidCode($code){
355355 return false ;
356356 }
357357 for ($ i =$ separatorPosition +1 ;$ i <strlen ($ code );$ i ++){
358- if (strpos (self ::CODE_ALPHABET ,substr ($ code ,$ i ,1 ))=== false ){
358+ if (self :: indexOf (self ::CODE_ALPHABET ,substr ($ code ,$ i ,1 ))==- 1 ){
359359 return false ;
360360 }
361361 }
@@ -414,9 +414,17 @@ private static function normalizeLongitude($longitude){
414414 */
415415 private static function computeLatitudePrecision ($ codeLength ){
416416 if ($ codeLength <=self ::CODE_PRECISION_NORMAL ){
417- return pow ( self ::ENCODING_BASE , ($ codeLength / -2 + 2 ) );
417+ return self ::ENCODING_BASE ** ($ codeLength / -2 + 2 );
418418 }
419- return pow (self ::ENCODING_BASE ,-3 ) / pow (self ::GRID_ROWS ,$ codeLength - self ::PAIR_CODE_LENGTH );
419+ return (self ::ENCODING_BASE ** -3 ) / (self ::GRID_ROWS ** ($ codeLength - self ::PAIR_CODE_LENGTH ));
420+ }
421+
422+ private static function indexOf ($ haystack ,$ needle ,$ offset =0 ){
423+ $ pos = strpos ($ haystack ,$ needle ,$ offset );
424+ if ($ pos ===false ){
425+ return -1 ;
426+ }
427+ return $ pos ;
420428 }
421429
422430}
0 commit comments