| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Utilities; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class ProductMetaKey |
| 10 |
{ |
| 11 |
public const PRODUCT_ID = 'sync_basalam_product_id'; |
| 12 |
public const PRODUCT_SYNC_STATUS = 'sync_basalam_product_sync_status'; |
| 13 |
public const PRODUCT_STATUS = 'sync_basalam_product_status'; |
| 14 |
|
| 15 |
public static function basalamProductId($vendorId = null): string |
| 16 |
{ |
| 17 |
return self::build(self::PRODUCT_ID, $vendorId); |
| 18 |
} |
| 19 |
|
| 20 |
public static function basalamProductSyncStatus($vendorId = null): string |
| 21 |
{ |
| 22 |
return self::build(self::PRODUCT_SYNC_STATUS, $vendorId); |
| 23 |
} |
| 24 |
|
| 25 |
public static function basalamProductStatus($vendorId = null): string |
| 26 |
{ |
| 27 |
return self::build(self::PRODUCT_STATUS, $vendorId); |
| 28 |
} |
| 29 |
|
| 30 |
public static function basalamProductMetaKeys($vendorId = null): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
self::basalamProductId($vendorId), |
| 34 |
self::basalamProductSyncStatus($vendorId), |
| 35 |
self::basalamProductStatus($vendorId), |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
private static function build(string $baseKey, $vendorId = null): string |
| 40 |
{ |
| 41 |
$resolvedVendorId = self::normalizeVendorId($vendorId ?? self::getVendorIdFromSettings()); |
| 42 |
|
| 43 |
if ($resolvedVendorId === '') { |
| 44 |
return $baseKey; |
| 45 |
} |
| 46 |
|
| 47 |
return "{$baseKey}_{$resolvedVendorId}"; |
| 48 |
} |
| 49 |
|
| 50 |
private static function getVendorIdFromSettings() |
| 51 |
{ |
| 52 |
return syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 53 |
} |
| 54 |
|
| 55 |
private static function normalizeVendorId($vendorId): string |
| 56 |
{ |
| 57 |
if ($vendorId === null || $vendorId === '') { |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
return preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $vendorId); |
| 62 |
} |
| 63 |
} |
| 64 |
|