Created
August 10, 2019 21:40
-
-
Save Gummibeer/6bda34a908da3ffc555352515aacc504 to your computer and use it in GitHub Desktop.
float & date formatting of model attributes (- are folder separators)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libs; | |
use DateTime; | |
class Formatter | |
{ | |
public static function float(float $float, int $decimals = 2): string | |
{ | |
return number_format($float, $decimals, trans('formatter.numeric.decimal'), trans('formatter.numeric.thousand')); | |
} | |
public static function dateNormalized(DateTime $date): string | |
{ | |
return $date->format('Y-m-d'); | |
} | |
public static function dateLocalized(DateTime $date): string | |
{ | |
return $date->format(trans('formatter.date.ymd')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model as IlluminateModel; | |
/** | |
* @property-read string $price_formatted | |
* @property-read string $created_at_normalized | |
* @property-read string $created_at_localized | |
* @property-read string $updated_at_normalized | |
* @property-read string $updated_at_localized | |
*/ | |
class Model extends IlluminateModel | |
{ | |
public $casts = [ | |
'price' => 'float', | |
]; | |
public function getAttribute($key) | |
{ | |
if (!$key) { | |
return; | |
} | |
if (Str::startsWith($key, $this->getDates())) { | |
if (Str::endsWith($key, '_localized')) { | |
return Formatter::dateLocalized($this->getAttributeValue(str_replace('_localized', '', $key))); | |
} | |
if (Str::endsWith($key, '_normalized')) { | |
return Formatter::dateNormalized($this->getAttributeValue(str_replace('_normalized', '', $key))); | |
} | |
} | |
if (Str::endsWith($key, '_formatted')) { | |
$parentKey = str_replace('_formatted', '', $key); | |
if ($this->hasCast($parentKey, 'float')) { | |
return Formatter::float($this->getAttributeValue($parentKey)); | |
} | |
} | |
return parent::getAttribute($key); | |
} | |
public function setAttribute($key, $value) | |
{ | |
if (!$key) { | |
return; | |
} | |
if (Str::startsWith($key, $this->getDates())) { | |
if (Str::endsWith($key, '_localized')) { | |
$this->attributes[str_replace('_localized', '', $key)] = Carbon::createFromFormat(trans('formatter.date.ymd'), $value); | |
} | |
if (Str::endsWith($key, '_normalized')) { | |
$this->attributes[str_replace('_normalized', '', $key)] = Carbon::createFromFormat('Y-m-d', $value); | |
} | |
} | |
if (Str::endsWith($key, '_formatted')) { | |
$parentKey = str_replace('_formatted', '', $key); | |
if ($this->hasCast($parentKey, 'float')) { | |
$this->attributes[$parentKey] = str_replace(trans('formatter.numeric.decimal'), '.', str_replace(trans('formatter.numeric.thousand'), '', $value)) * 1; | |
} | |
} | |
return parent::setAttribute($key, $value); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'date' => [ | |
'ymdhi' => 'd.m.Y H:i', | |
'ymd' => 'd.m.Y', | |
], | |
'numeric' => [ | |
'decimal' => ',', | |
'thousand' => '.', | |
], | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'date' => [ | |
'ymdhi' => 'Y-m-d H:i', | |
'ymd' => 'Y-m-d', | |
], | |
'numeric' => [ | |
'decimal' => '.', | |
'thousand' => ',', | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment