Make sure location data in request is processed in a unified way.

This commit is contained in:
James Cole
2020-01-01 14:27:43 +01:00
parent 414e8d7153
commit ca260387a2
4 changed files with 101 additions and 20 deletions

View File

@@ -344,4 +344,45 @@ class Request extends FormRequest
return $result;
}
/**
* Read the submitted Request data and add new or updated Location data to the array.
*
* @param array $data
*
* @return array
*/
protected function appendLocationData(array $data): array
{
Log::debug('Now in appendLocationData()');
$data['store_location'] = false;
$data['update_location'] = false;
$data['longitude'] = null;
$data['latitude'] = null;
$data['zoom_level'] = null;
// for a POST (store, all fields must be present and accounted for:
if ('POST' === $this->method() && $this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) {
Log::debug('Method is POST and all fields present.');
$data['store_location'] = true;
$data['longitude'] = '' === $this->string('longitude') ? null : $this->string('longitude');
$data['latitude'] = '' === $this->string('latitude') ? null : $this->string('latitude');
$data['zoom_level'] = '' === $this->string('zoom_level') ? null : $this->integer('zoom_level');
}
if ('PUT' === $this->method() && $this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) {
Log::debug('Method is PUT and all fields present.');
$data['update_location'] = true;
$data['longitude'] = '' === $this->string('longitude') ? null : $this->string('longitude');
$data['latitude'] = '' === $this->string('latitude') ? null : $this->string('latitude');
$data['zoom_level'] = '' === $this->string('zoom_level') ? null : $this->integer('zoom_level');
}
if (null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) {
Log::debug('One of the fields is NULL, wont save.');
$data['store_location'] = false;
$data['update_location'] = false;
}
Log::debug(sprintf('Returning longitude: "%s", latitude: "%s", zoom level: "%s"', $data['longitude'], $data['latitude'], $data['zoom_level']));
return $data;
}
}