<?php
const LOCATION_PRECISION = 7;
const LOCATION_LAT = '40.7591523';
const LOCATION_LNG = '-73.9777136';
trait Geocodes
{
protected $recast = [];
protected $precision = LOCATION_PRECISION;
abstract function reCast(array &$payload);
}
class Location //implements ArrayAccess
{
use Geocodes;
//FATAL: can't override here!
//protected $recast = [
// 'lat' => ['index' => 'lat', 'type' => 'double'],
// 'lng' => ['index' => 'lng', 'type' => 'double']
//];
protected $lat = LOCATION_LAT;
protected $lng = LOCATION_LNG;
public function __construct()
{
$this->precision = LOCATION_PRECISION;
$this->recast['lat'] = ['index' => 'lat', 'type' => 'double'];
$this->recast['lng'] = ['index' => 'lng', 'type' => 'double'];
}
public function recast(array &$payload)
{
foreach(array_keys($payload) as $key)
{
var_dump($payload);
$api_key = $this->recast[$key]['index'];
$api_type = $this->recast[$key]['type'];
if(! array_key_exists($api_key, $payload))
$payload[$api_key] = bcadd($payload[$key],0,$this->precision);
}
}
public function getLat() { return $this->lat; }
public function getLng() { return $this->lng; }
}
$loc = new Location();
$payload = ['lat' => $loc->getLat(), 'lng' => $loc->getLng()];
$loc->recast($payload);
echo PHP_EOL.print_r($payload, 1).PHP_EOL;
- Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array(2) {
["lat"]=>
string(10) "40.7591523"
["lng"]=>
string(11) "-73.9777136"
}
array(2) {
["lat"]=>
string(10) "40.7591523"
["lng"]=>
string(11) "-73.9777136"
}
Array
(
[lat] => 40.7591523
[lng] => -73.9777136
)
preferences:
145.89 ms | 408 KiB | 5 Q