| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\BaseResourceApi; |
| 6 |
use FluentCart\App\Models\OrderAddress; |
| 7 |
use FluentCart\Framework\Database\Orm\Builder; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class OrderAddressResource extends BaseResourceApi |
| 11 |
{ |
| 12 |
public static function getQuery(): Builder |
| 13 |
{ |
| 14 |
return OrderAddress::query(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get order addresses with specified parameters. |
| 19 |
* |
| 20 |
* @param array $params Array containing the necessary parameters. |
| 21 |
* |
| 22 |
*/ |
| 23 |
public static function get(array $params = []) |
| 24 |
{ |
| 25 |
// |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Find order address based on the given ID and parameters. |
| 30 |
* |
| 31 |
* @param int $id Required. The ID of the order. |
| 32 |
* @param array $params Optional. Array containing the necessary parameters. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public static function find($id, $params = []) |
| 36 |
{ |
| 37 |
$type = Arr::get($params, 'type', ''); |
| 38 |
|
| 39 |
return static::getQuery() |
| 40 |
->where('order_id', $id) |
| 41 |
->where('type', $type) |
| 42 |
->first(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Create order address with the given data. |
| 47 |
* |
| 48 |
* @param array $data Required. Array containing the necessary parameters |
| 49 |
* [ |
| 50 |
* 'order_id' => (int) Required. The id of the order. |
| 51 |
* 'name' => (string) Required. The name of the address. |
| 52 |
* 'address_1' => (string) Required. The primary address line. |
| 53 |
* 'address_2' => (string) Optional. The secondary address line. |
| 54 |
* 'city' => (string) Required. The city of the address. |
| 55 |
* 'country' => (string) Required. The country of the address. |
| 56 |
* 'postcode' => (string) Required. The postal code of the address. |
| 57 |
* 'state' => (string) Required. The state of the address. |
| 58 |
* 'type' => (string) Required. (e.g., 'billing', 'shipping'). |
| 59 |
* ] |
| 60 |
* @param array $params Required. Additional parameters for creating an address. |
| 61 |
* |
| 62 |
*/ |
| 63 |
public static function create($data, $params = []) |
| 64 |
{ |
| 65 |
$isCreated = static::getQuery()->create($data); |
| 66 |
|
| 67 |
if ($isCreated) { |
| 68 |
return static::makeSuccessResponse( |
| 69 |
$isCreated, |
| 70 |
__('Order address created successfully!', 'fluent-cart') |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
return static::makeErrorResponse([ |
| 75 |
[ 'code' => 400, 'message' => __('Order address creation failed.', 'fluent-cart') ] |
| 76 |
]); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Update order address with the given data. |
| 81 |
* |
| 82 |
* @param array $data Required. Array containing the necessary parameters |
| 83 |
* @param int $id Required. The ID of the order. |
| 84 |
* |
| 85 |
*/ |
| 86 |
public static function update($data, $id, $params = []) |
| 87 |
{ |
| 88 |
if (!$id) { |
| 89 |
return static::makeErrorResponse([ |
| 90 |
['code' => 403, 'message' => __('Please edit a valid address!', 'fluent-cart')] |
| 91 |
]); |
| 92 |
} |
| 93 |
|
| 94 |
$address = static::find($id, ['type' => Arr::get($data, 'type', null)]); |
| 95 |
|
| 96 |
if (!$address) { |
| 97 |
return static::makeErrorResponse([ |
| 98 |
['code' => 404, 'message' => __('Address not found, please reload the page and try again!', 'fluent-cart')] |
| 99 |
]); |
| 100 |
} |
| 101 |
|
| 102 |
$address->update($data); |
| 103 |
|
| 104 |
if ($address) { |
| 105 |
return static::makeSuccessResponse($address, __('Order address updated successfully!', 'fluent-cart')); |
| 106 |
} |
| 107 |
|
| 108 |
return static::makeErrorResponse([ |
| 109 |
['code' => 400, 'message' => __('Order address update failed.', 'fluent-cart')] |
| 110 |
]); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Delete an address based on the given ID and parameters. |
| 115 |
* |
| 116 |
* @param int $id Required. The ID of the order address. |
| 117 |
* @param array $params Optional. Additional parameters for address deletion. |
| 118 |
* |
| 119 |
*/ |
| 120 |
public static function delete($id, $params = []) |
| 121 |
{ |
| 122 |
// |
| 123 |
} |
| 124 |
} |