Skip to content

Instantly share code, notes, and snippets.

@iamroi
Last active December 3, 2021 11:11
Show Gist options
  • Save iamroi/f87e328994dc821667b7ee87244f9cf3 to your computer and use it in GitHub Desktop.
Save iamroi/f87e328994dc821667b7ee87244f9cf3 to your computer and use it in GitHub Desktop.
MoneyIntegerCast - For package https://github.com/cknow/laravel-money
<?php
namespace App\Casts;
use Cknow\Money\Money;
use Cknow\Money\MoneyCast;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use InvalidArgumentException;
class MoneyIntegerCast extends MoneyCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return \Cknow\Money\Money|null
*/
public function get($model, string $key, $value, array $attributes)
{
if ($value === null) {
return $value;
}
$currency = $this->getCurrency($attributes);
// adding your extra attributes needed
$attributes = ['subunit' => Money::getCurrencies()->subunitFor($currency)];
$money = new Money($value, $currency);
$money->attributes($attributes);
return $money;
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @throws \InvalidArgumentException
*
* @return array
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value === null) {
return [$key => $value];
}
try {
$currency = $this->getCurrency($attributes);
$money = $value instanceof Money ? $value : new Money($value, $currency);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(
sprintf('Invalid data provided for %s::$%s', get_class($model), $key)
);
}
$amount = $money->getAmount();
if (array_key_exists($this->currency, $attributes)) {
return [$key => $amount, $this->currency => $money->getCurrency()->getCode()];
}
return [$key => $amount];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment