Cleanup method.

This commit is contained in:
James Cole
2020-03-15 18:05:24 +01:00
parent 524f85b9c1
commit 6a6bf102b6

View File

@@ -191,7 +191,12 @@ class Request extends FormRequest
if (!$this->has($field)) { if (!$this->has($field)) {
return null; return null;
} }
return app('steam')->cleanString((string)($this->get($field) ?? '')); $res = trim(app('steam')->cleanString((string)($this->get($field) ?? '')));
if ('' === $res) {
return null;
}
return $res;
} }
/** /**
@@ -344,11 +349,10 @@ class Request extends FormRequest
$data['latitude'] = null; $data['latitude'] = null;
$data['zoom_level'] = null; $data['zoom_level'] = null;
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
$longitudeKey = null === $prefix ? 'longitude' : sprintf('%s_longitude', $prefix); $latitudeKey = $this->getLocationKey($prefix, 'latitude');
$latitudeKey = null === $prefix ? 'latitude' : sprintf('%s_latitude', $prefix); $zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
$zoomLevelKey = null === $prefix ? 'zoom_level' : sprintf('%s_zoom_level', $prefix); $hasLocationKey = $this->getLocationKey($prefix, 'has_location');
$hasLocationKey = null === $prefix ? 'has_location' : sprintf('%s_has_location', $prefix);
// for a POST (store, all fields must be present and accounted for: // for a POST (store, all fields must be present and accounted for:
if ( if (
@@ -356,11 +360,10 @@ class Request extends FormRequest
&& ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey)) && ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey))
) { ) {
Log::debug('Method is POST and all fields present.'); Log::debug('Method is POST and all fields present.');
$data['store_location'] = $this->boolean($hasLocationKey); $data['store_location'] = $this->boolean($hasLocationKey);
$data['longitude'] = '' === $this->string($longitudeKey) ? null : $this->string($longitudeKey); $data['longitude'] = $this->nullableString($longitudeKey);
$data['latitude'] = '' === $this->string($latitudeKey) ? null : $this->string($latitudeKey); $data['latitude'] = $this->nullableString($latitudeKey);
$data['zoom_level'] = '' === $this->string($zoomLevelKey) ? null : $this->integer($zoomLevelKey); $data['zoom_level'] = $this->nullableString($zoomLevelKey);
} }
if ( if (
($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey)) ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey))
@@ -371,9 +374,9 @@ class Request extends FormRequest
) { ) {
Log::debug('Method is PUT and all fields present.'); Log::debug('Method is PUT and all fields present.');
$data['update_location'] = true; $data['update_location'] = true;
$data['longitude'] = '' === $this->string($longitudeKey) ? null : $this->string($longitudeKey); $data['longitude'] = $this->nullableString($longitudeKey);
$data['latitude'] = '' === $this->string($latitudeKey) ? null : $this->string($latitudeKey); $data['latitude'] = $this->nullableString($latitudeKey);
$data['zoom_level'] = '' === $this->string($zoomLevelKey) ? null : $this->integer($zoomLevelKey); $data['zoom_level'] = $this->nullableString($zoomLevelKey);
} }
if (null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) { if (null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) {
Log::debug('One of the fields is NULL, wont save.'); Log::debug('One of the fields is NULL, wont save.');
@@ -419,5 +422,20 @@ class Request extends FormRequest
} }
} }
/**
* @param string|null $prefix
* @param string $key
*
* @return string
*/
private function getLocationKey(?string $prefix, string $key): string
{
if (null === $prefix) {
return $key;
}
return sprintf('%s_%s', $prefix, $key);
}
} }