|
4 | 4 |
|
5 | 5 | namespace Paddle\SDK\Resources\Adjustments\Operations; |
6 | 6 |
|
| 7 | +use Paddle\SDK\Entities\Adjustment\AdjustmentType; |
7 | 8 | use Paddle\SDK\Entities\Shared\Action; |
| 9 | +use Paddle\SDK\Exceptions\SdkExceptions\InvalidArgumentException; |
| 10 | +use Paddle\SDK\FiltersUndefined; |
8 | 11 | use Paddle\SDK\Resources\Adjustments\Operations\Create\AdjustmentItem; |
| 12 | +use Paddle\SDK\Undefined; |
9 | 13 |
|
10 | 14 | class CreateAdjustment implements \JsonSerializable |
11 | 15 | { |
| 16 | + use FiltersUndefined; |
| 17 | + |
12 | 18 | /** |
13 | 19 | * @param array<AdjustmentItem> $items |
| 20 | + * |
| 21 | + * @throws InvalidArgumentException |
14 | 22 | */ |
15 | 23 | public function __construct( |
16 | 24 | public readonly Action $action, |
17 | | - public readonly array $items, |
| 25 | + public readonly array|Undefined $items, |
18 | 26 | public readonly string $reason, |
19 | 27 | public readonly string $transactionId, |
| 28 | + public readonly AdjustmentType|Undefined $type = new Undefined(), |
20 | 29 | ) { |
| 30 | + if ($this->type === AdjustmentType::Partial() && ($this->items instanceof Undefined || empty($this->items))) { |
| 31 | + throw InvalidArgumentException::arrayIsEmpty('items'); |
| 32 | + } |
21 | 33 | } |
22 | 34 |
|
23 | 35 | public function jsonSerialize(): array |
24 | 36 | { |
25 | 37 | $items = []; |
26 | 38 |
|
27 | | - foreach ($this->items as $item) { |
28 | | - $items[] = [ |
29 | | - 'item_id' => $item->itemId, |
30 | | - 'type' => $item->type->getValue(), |
31 | | - 'amount' => $item->amount, |
32 | | - ]; |
| 39 | + if (! $this->items instanceof Undefined) { |
| 40 | + foreach ($this->items as $item) { |
| 41 | + $items[] = [ |
| 42 | + 'item_id' => $item->itemId, |
| 43 | + 'type' => $item->type->getValue(), |
| 44 | + 'amount' => $item->amount, |
| 45 | + ]; |
| 46 | + } |
33 | 47 | } |
34 | 48 |
|
35 | | - return [ |
| 49 | + return $this->filterUndefined([ |
36 | 50 | 'action' => $this->action, |
37 | 51 | 'items' => $items, |
38 | 52 | 'reason' => $this->reason, |
39 | 53 | 'transaction_id' => $this->transactionId, |
40 | | - ]; |
| 54 | + 'type' => $this->type, |
| 55 | + ]); |
41 | 56 | } |
42 | 57 | } |
0 commit comments