|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BigCommerce\ApiV3\Catalog\Products\ProductVariant; |
| 4 | + |
| 5 | +use BigCommerce\ApiV3\Api\ResourceApi; |
| 6 | +use BigCommerce\ApiV3\ResponseModels\Product\ProductVariantMetafieldResponse; |
| 7 | +use BigCommerce\ApiV3\ResponseModels\Product\ProductVariantMetafieldsResponse; |
| 8 | + |
| 9 | +class ProductVariantMetafieldsApi extends ResourceApi |
| 10 | +{ |
| 11 | + private int $productId; |
| 12 | + |
| 13 | + private const RESOURCE_NAME = 'metafields'; |
| 14 | + private const METAFIELD_ENDPOINT = 'catalog/product/%d/variants/%d/metafields/%d'; |
| 15 | + private const METAFIELDS_ENDPOINT = 'catalog/product/%d/variants/%d/metafields'; |
| 16 | + |
| 17 | + public function getProductId(): int |
| 18 | + { |
| 19 | + return $this->productId; |
| 20 | + } |
| 21 | + |
| 22 | + public function setProductId(int $productId): void |
| 23 | + { |
| 24 | + $this->productId = $productId; |
| 25 | + } |
| 26 | + |
| 27 | + protected function singleResourceEndpoint(): string |
| 28 | + { |
| 29 | + return self::METAFIELD_ENDPOINT; |
| 30 | + } |
| 31 | + |
| 32 | + protected function singleResourceUrl(): string |
| 33 | + { |
| 34 | + return sprintf( |
| 35 | + $this->singleResourceEndpoint(), |
| 36 | + $this->getProductId(), |
| 37 | + $this->getParentResourceId() ?? $this->getResourceId(), |
| 38 | + $this->getResourceId() |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + protected function multipleResourcesEndpoint(): string |
| 43 | + { |
| 44 | + return self::METAFIELDS_ENDPOINT; |
| 45 | + } |
| 46 | + |
| 47 | + protected function multipleResourceUrl(): string |
| 48 | + { |
| 49 | + return sprintf( |
| 50 | + $this->multipleResourcesEndpoint(), |
| 51 | + $this->getProductId(), |
| 52 | + $this->getParentResourceId() |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + protected function resourceName(): string |
| 57 | + { |
| 58 | + return self::RESOURCE_NAME; |
| 59 | + } |
| 60 | + |
| 61 | + public function get(): ProductVariantMetafieldResponse |
| 62 | + { |
| 63 | + return new ProductVariantMetafieldResponse($this->getResource()); |
| 64 | + } |
| 65 | + |
| 66 | + public function getAll(array $filters = [], int $page = 1, int $limit = 250): ProductVariantMetafieldsResponse |
| 67 | + { |
| 68 | + return new ProductVariantMetafieldsResponse($this->getAllResources($filters, $page, $limit)); |
| 69 | + } |
| 70 | +} |
0 commit comments