chore: code cleanup.

This commit is contained in:
James Cole
2023-05-29 13:56:55 +02:00
parent 7f7644c92f
commit 1b52147a05
295 changed files with 12418 additions and 12324 deletions

View File

@@ -30,6 +30,16 @@ use Illuminate\Support\Facades\Log;
*/
trait AppendsLocationData
{
/**
* Abstract method stolen from "InteractsWithInput".
*
* @param null $key
* @param bool $default
*
* @return mixed
*/
abstract public function boolean($key = null, $default = false);
/**
* Abstract method.
*
@@ -39,6 +49,22 @@ trait AppendsLocationData
*/
abstract public function has($key);
/**
* Abstract method.
*
* @return string
*/
abstract public function method();
/**
* Abstract method.
*
* @param mixed ...$patterns
*
* @return mixed
*/
abstract public function routeIs(...$patterns);
/**
* Read the submitted Request data and add new or updated Location data to the array.
*
@@ -114,6 +140,27 @@ trait AppendsLocationData
return sprintf('%s_%s', $prefix, $key);
}
/**
* @param string|null $prefix
*
* @return bool
*/
private function isValidEmptyPUT(?string $prefix): bool
{
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
$latitudeKey = $this->getLocationKey($prefix, 'latitude');
$zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
return (
null === $this->get($longitudeKey)
&& null === $this->get($latitudeKey)
&& null === $this->get($zoomLevelKey))
&& (
'PUT' === $this->method()
|| ('POST' === $this->method() && $this->routeIs('*.update'))
);
}
/**
* @param string|null $prefix
*
@@ -157,32 +204,6 @@ trait AppendsLocationData
return false;
}
/**
* Abstract method.
*
* @return string
*/
abstract public function method();
/**
* Abstract method.
*
* @param mixed ...$patterns
*
* @return mixed
*/
abstract public function routeIs(...$patterns);
/**
* Abstract method stolen from "InteractsWithInput".
*
* @param null $key
* @param bool $default
*
* @return mixed
*/
abstract public function boolean($key = null, $default = false);
/**
* @param string|null $prefix
*
@@ -227,25 +248,4 @@ trait AppendsLocationData
return false;
}
/**
* @param string|null $prefix
*
* @return bool
*/
private function isValidEmptyPUT(?string $prefix): bool
{
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
$latitudeKey = $this->getLocationKey($prefix, 'latitude');
$zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
return (
null === $this->get($longitudeKey)
&& null === $this->get($latitudeKey)
&& null === $this->get($zoomLevelKey))
&& (
'PUT' === $this->method()
|| ('POST' === $this->method() && $this->routeIs('*.update'))
);
}
}

View File

@@ -33,44 +33,6 @@ use Illuminate\Support\Facades\Log;
*/
trait ConvertsDataTypes
{
/**
* Return integer value.
*
* @param string $field
*
* @return int
*/
public function convertInteger(string $field): int
{
return (int)$this->get($field);
}
/**
* Abstract method that always exists in the Request classes that use this
* trait, OR a stub needs to be added by any other class that uses this train.
*
* @param string $key
* @param mixed|null $default
* @return mixed
*/
abstract public function get(string $key, mixed $default = null): mixed;
/**
* Return string value.
*
* @param string $field
*
* @return string
*/
public function convertString(string $field): string
{
$entry = $this->get($field);
if (!is_scalar($entry)) {
return '';
}
return $this->clearString((string)$entry, false);
}
/**
* @param string|null $string
* @param bool $keepNewlines
@@ -152,6 +114,53 @@ trait ConvertsDataTypes
return trim($string);
}
/**
* Return integer value.
*
* @param string $field
*
* @return int
*/
public function convertInteger(string $field): int
{
return (int)$this->get($field);
}
/**
* Return string value.
*
* @param string $field
*
* @return string
*/
public function convertString(string $field): string
{
$entry = $this->get($field);
if (!is_scalar($entry)) {
return '';
}
return $this->clearString((string)$entry, false);
}
/**
* Abstract method that always exists in the Request classes that use this
* trait, OR a stub needs to be added by any other class that uses this train.
*
* @param string $key
* @param mixed|null $default
* @return mixed
*/
abstract public function get(string $key, mixed $default = null): mixed;
/**
* Abstract method that always exists in the Request classes that use this
* trait, OR a stub needs to be added by any other class that uses this train.
*
* @param mixed $key
* @return mixed
*/
abstract public function has($key);
/**
* Return string value with newlines.
*
@@ -210,6 +219,44 @@ trait ConvertsDataTypes
return false;
}
protected function convertDateTime(?string $string): ?Carbon
{
$value = $this->get($string);
if (null === $value) {
return null;
}
if ('' === $value) {
return null;
}
if (10 === strlen($value)) {
// probably a date format.
try {
$carbon = Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) {
Log::error(sprintf('[1] "%s" is not a valid date: %s', $value, $e->getMessage()));
return null;
} catch (InvalidFormatException $e) {
Log::error(sprintf('[2] "%s" is of an invalid format: %s', $value, $e->getMessage()));
return null;
}
return $carbon;
}
// is an atom string, I hope?
try {
$carbon = Carbon::parse($value);
} catch (InvalidDateException $e) {
Log::error(sprintf('[3] "%s" is not a valid date or time: %s', $value, $e->getMessage()));
return null;
} catch (InvalidFormatException $e) {
Log::error(sprintf('[4] "%s" is of an invalid format: %s', $value, $e->getMessage()));
return null;
}
return $carbon;
}
/**
* Return floating value.
*
@@ -256,44 +303,6 @@ trait ConvertsDataTypes
return $carbon;
}
protected function convertDateTime(?string $string): ?Carbon
{
$value = $this->get($string);
if (null === $value) {
return null;
}
if ('' === $value) {
return null;
}
if (10 === strlen($value)) {
// probably a date format.
try {
$carbon = Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) {
Log::error(sprintf('[1] "%s" is not a valid date: %s', $value, $e->getMessage()));
return null;
} catch (InvalidFormatException $e) {
Log::error(sprintf('[2] "%s" is of an invalid format: %s', $value, $e->getMessage()));
return null;
}
return $carbon;
}
// is an atom string, I hope?
try {
$carbon = Carbon::parse($value);
} catch (InvalidDateException $e) {
Log::error(sprintf('[3] "%s" is not a valid date or time: %s', $value, $e->getMessage()));
return null;
} catch (InvalidFormatException $e) {
Log::error(sprintf('[4] "%s" is of an invalid format: %s', $value, $e->getMessage()));
return null;
}
return $carbon;
}
/**
* Returns all data in the request, or omits the field if not set,
* according to the config from the request. This is the way.
@@ -315,15 +324,6 @@ trait ConvertsDataTypes
return $return;
}
/**
* Abstract method that always exists in the Request classes that use this
* trait, OR a stub needs to be added by any other class that uses this train.
*
* @param mixed $key
* @return mixed
*/
abstract public function has($key);
/**
* Return date or NULL.
*