fix(ProductRepo): 修复产品仓库中的数组为空的处理逻辑

- 优化了 one_time、package 和 plan 数据的处理方式
- 使用 array_map 函数替代循环,提高代码可读性
- 增加空值合并运算符 ??,提高代码健壮性
-调整代码格式,提高代码整洁度
This commit is contained in:
李东云
2025-09-17 14:40:56 +08:00
parent 65b060e5d3
commit 686d835a91

View File

@@ -55,7 +55,8 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
pointBonus: $result['one_time']['point']['bonus'],
),
),
packages: array_map(fn(array $item) => new ProductItem(
packages: array_map(
fn(array $item) => new ProductItem(
id: $item['id'],
description: $item['name'],
unitPrice: new Money(
@@ -68,7 +69,9 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
pointBasic: $item['point']['number'],
pointBonus: $item['point']['bonus'],
),
), $result['package']),
),
$result['package'] ?? [],
),
);
}
@@ -99,7 +102,8 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
),
)
: null,
plans: array_map(fn(array $item) => new ProductItem(
plans: array_map(
fn(array $item) => new ProductItem(
id: $item['id'],
description: $item['name'],
unitPrice: new Money(
@@ -107,8 +111,11 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
currency: new Currency($item['currency']),
),
productType: ProductType::plan,
), $result['plan']),
packages: array_map(fn(array $item) => new ProductItem(
),
$result['plan'] ?? [],
),
packages: array_map(
fn(array $item) => new ProductItem(
id: $item['id'],
description: $item['name'],
unitPrice: new Money(
@@ -121,7 +128,9 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
pointBasic: $item['point']['number'],
pointBonus: $item['point']['bonus'],
),
), $result['package']),
),
$result['package'] ?? [],
),
);
}
@@ -171,7 +180,8 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
),
)
: null,
plans: array_map(fn(array $item) => new ProductItem(
plans: array_map(
fn(array $item) => new ProductItem(
id: $item['id'],
description: $item['name'],
unitPrice: new Money(
@@ -184,7 +194,9 @@ final class ProductRepo extends AbstractRepo implements RechargeProductRepoInter
pointBasic: $item['point']['number'],
pointBonus: $item['point']['bonus'],
),
), $result['plan']),
),
$result['plan'] ?? [],
),
);
}