您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JournalEntryCast.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Casts;
  3. use App\Utilities\Currency\CurrencyAccessor;
  4. use App\Utilities\Currency\CurrencyConverter;
  5. use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
  6. use Illuminate\Database\Eloquent\Model;
  7. use UnexpectedValueException;
  8. class JournalEntryCast implements CastsAttributes
  9. {
  10. public function get(Model $model, string $key, mixed $value, array $attributes): string
  11. {
  12. $currency_code = CurrencyAccessor::getDefaultCurrency();
  13. if ($value !== null) {
  14. return CurrencyConverter::prepareForMutator($value, $currency_code);
  15. }
  16. return '';
  17. }
  18. /**
  19. * @throws UnexpectedValueException
  20. */
  21. public function set(Model $model, string $key, mixed $value, array $attributes): int
  22. {
  23. $currency_code = CurrencyAccessor::getDefaultCurrency();
  24. if (is_numeric($value)) {
  25. $value = (string) $value;
  26. } elseif (! is_string($value)) {
  27. throw new UnexpectedValueException('Expected string or numeric value for money cast');
  28. }
  29. return CurrencyConverter::prepareForAccessor($value, $currency_code);
  30. }
  31. }