| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Models\Meta; |
| 7 |
use FluentCart\Framework\Database\Orm\Builder; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class FluentMetaResource extends BaseResourceApi |
| 11 |
{ |
| 12 |
|
| 13 |
public static function getQuery(): Builder |
| 14 |
{ |
| 15 |
return Meta::query(); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Get meta based on the provided parameters. |
| 20 |
* |
| 21 |
* @param array $params Array containing the necessary parameters. |
| 22 |
* [ |
| 23 |
* 'meta_key' => (string) Required. The meta key to retrieve data. |
| 24 |
* 'default' => (mixed) Optional. Default value to return if data is not found. |
| 25 |
* ] |
| 26 |
* |
| 27 |
*/ |
| 28 |
public static function get(array $params = []) |
| 29 |
{ |
| 30 |
$metaKey = Arr::get($params, 'meta_key'); |
| 31 |
$default = Arr::get($params, 'default'); |
| 32 |
|
| 33 |
$metaData = static::getQuery()->where('meta_key', $metaKey)->first(); |
| 34 |
|
| 35 |
if (!empty($metaData)) { |
| 36 |
$metaData = $metaData->toArray(); |
| 37 |
} |
| 38 |
|
| 39 |
if (isset($metaData['meta_value'])) { |
| 40 |
return Arr::get($metaData, 'meta_value'); |
| 41 |
} |
| 42 |
|
| 43 |
return $default; |
| 44 |
} |
| 45 |
|
| 46 |
public static function find($id, $params = []) |
| 47 |
{ |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public static function create($data, $params = []) |
| 52 |
{ |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Update metadata with the given data |
| 58 |
* |
| 59 |
* @param array $data Required. Array containing the necessary parameters. |
| 60 |
* [ |
| 61 |
* 'meta_key' => (string) Required. The key of the metadata. |
| 62 |
* 'meta_value' => (mixed) Required. The value of the metadata. |
| 63 |
* 'type' => (string) Optional. The type of the object. |
| 64 |
* ] |
| 65 |
* @param int $id Required. The ID of the product for which metadata is updated. |
| 66 |
* @param array $params Optional. Additional parameters for updating metadata. |
| 67 |
* [ |
| 68 |
* // Include optional parameters, if any. |
| 69 |
* ] |
| 70 |
* |
| 71 |
*/ |
| 72 |
public static function update($data, $id, $params = []) |
| 73 |
{ |
| 74 |
|
| 75 |
$metaKey = Arr::get($data, 'meta_key'); |
| 76 |
$metaValue = Arr::get($data, 'meta_value'); |
| 77 |
$type = Arr::get($data, 'type', ''); |
| 78 |
|
| 79 |
$existingMeta = static::getQuery()->where('meta_key', $metaKey)->first(); |
| 80 |
|
| 81 |
if ($existingMeta) { |
| 82 |
$existingMeta->meta_value = $metaValue; |
| 83 |
return $existingMeta->update(); |
| 84 |
} |
| 85 |
|
| 86 |
return static::getQuery()->create([ |
| 87 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 88 |
'meta_key' => $metaKey, |
| 89 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 90 |
'meta_value' => $metaValue, |
| 91 |
'object_id' => intval($id), |
| 92 |
'object_type' => $type, |
| 93 |
]); |
| 94 |
} |
| 95 |
|
| 96 |
public static function delete($id, $params = []) |
| 97 |
{ |
| 98 |
|
| 99 |
} |
| 100 |
} |