wip: continue implementation
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Carbon\Exceptions\InvalidFormatException;
|
||||
|
||||
trait TypeConversionTrait
|
||||
{
|
||||
protected function stringToArray(string $string, string $separator = ','): array
|
||||
{
|
||||
return explode($separator, $string);
|
||||
}
|
||||
|
||||
protected function stringToFloat(string $string): float
|
||||
{
|
||||
return (float) str_replace(['.', ','], ['', '.'], $string);
|
||||
}
|
||||
|
||||
protected function floatToString(float $float): string
|
||||
{
|
||||
return number_format($float, 2, ',', '');
|
||||
}
|
||||
|
||||
protected function stringToBool(string $string): bool
|
||||
{
|
||||
return 'True' === $string;
|
||||
}
|
||||
|
||||
protected function boolToString(bool $bool): string
|
||||
{
|
||||
return $bool ? 'True' : 'False';
|
||||
}
|
||||
|
||||
protected function stringToDate(string $string): ?\DateTimeImmutable
|
||||
{
|
||||
try {
|
||||
$date = Carbon::createFromFormat('d.m.Y', $string)->startOfDay();
|
||||
return $date->toDateTimeImmutable();
|
||||
} catch (InvalidFormatException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function stringToDateTime(string $string): ?\DateTimeImmutable
|
||||
{
|
||||
try {
|
||||
$dateTime = Carbon::createFromFormat('d.m.Y H:i', substr($string, 0, 16))->startOfDay();
|
||||
return $dateTime->toDateTimeImmutable();
|
||||
} catch (InvalidFormatException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user