Skip to content

Commit 5bb5b81

Browse files
committed
Update CurrencyCast
1 parent 33cfd75 commit 5bb5b81

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/CurrencyCast.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Brick\Money\Exception\UnknownCurrencyException;
66
use Brick\Money\Money;
7+
use Exception;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
910
use Illuminate\Support\Arr;
@@ -38,9 +39,14 @@ public function get($model, $key, $value, $attributes)
3839
* @param Money|null $value
3940
* @param array $attributes
4041
* @return string
42+
* @throws Exception
4143
*/
4244
public function set($model, $key, $value, $attributes)
4345
{
46+
if (!$value instanceof Money) {
47+
throw new Exception(sprintf('Property %s must be an instance of %s', $key, Money::class));
48+
}
49+
4450
return json_encode([
4551
'currency' => $value->getCurrency()->getCurrencyCode(),
4652
'amount' => $value->getMinorAmount()->toInt(),

0 commit comments

Comments
 (0)