Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created August 10, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gummibeer/6bda34a908da3ffc555352515aacc504 to your computer and use it in GitHub Desktop.
Save Gummibeer/6bda34a908da3ffc555352515aacc504 to your computer and use it in GitHub Desktop.
float & date formatting of model attributes (- are folder separators)
<?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'));
}
}
<?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);
}
}
<?php
return [
'date' => [
'ymdhi' => 'd.m.Y H:i',
'ymd' => 'd.m.Y',
],
'numeric' => [
'decimal' => ',',
'thousand' => '.',
],
];
<?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