11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Finller \Invoice \Casts ;
46
57use Finller \Invoice \InvoiceDiscount ;
@@ -14,16 +16,29 @@ class Discounts implements CastsAttributes
1416{
1517 /**
1618 * @param array<string, mixed> $attributes
17- * @return null| InvoiceDiscount[]
19+ * @return InvoiceDiscount[]
1820 */
1921 public function get (Model $ model , string $ key , mixed $ value , array $ attributes ): mixed
2022 {
21- $ data = Json::decode (data_get ($ attributes , $ key , '' ));
23+ /**
24+ * @var null|array<array-key, null|array{
25+ * name: ?string,
26+ * code: ?string,
27+ * currency: ?string,
28+ * amount_off: ?int,
29+ * percent_off: ?float,
30+ * }> $data
31+ */
32+ $ data = Json::decode ($ attributes [$ key ] ?? '' );
33+
34+ /** @var class-string<InvoiceDiscount> $class */
35+ $ class = config ()->string ('invoices.discount_class ' );
2236
23- /** @var string $class */
24- $ class = config ('invoices.discount_class ' );
37+ if (! is_array ($ data )) {
38+ return [];
39+ }
2540
26- return is_array ( $ data ) ? array_map (fn (? array $ item ) => $ class ::fromArray ($ item ), $ data ) : null ;
41+ return array_map (fn ($ item ) => $ class ::fromArray ($ item ), $ data );
2742 }
2843
2944 /**
@@ -35,6 +50,8 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
3550 */
3651 public function set (Model $ model , string $ key , mixed $ value , array $ attributes ): mixed
3752 {
38- return [$ key => Json::encode ($ value )];
53+ return [
54+ $ key => blank ($ value ) ? null : Json::encode ($ value ),
55+ ];
3956 }
4057}
0 commit comments