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