AttributeOption.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Webkul\BagistoApi\Models;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\GetCollection;
  7. use ApiPlatform\OpenApi\Model;
  8. use Illuminate\Database\Eloquent\Model as EloquentModel;
  9. use Symfony\Component\TypeInfo\Type\BuiltinType;
  10. use Symfony\Component\TypeInfo\TypeIdentifier;
  11. #[ApiResource(
  12. shortName: 'AttributeOption',
  13. description: 'Attribute option resource',
  14. operations: [
  15. new GetCollection(
  16. uriTemplate: '/attribute_options',
  17. routePrefix: '/api/admin',
  18. security: "is_granted('ROLE_ADMIN')",
  19. ),
  20. new Get(
  21. uriTemplate: '/attribute_options/{id}',
  22. routePrefix: '/api/admin',
  23. security: "is_granted('ROLE_ADMIN')",
  24. ),
  25. new GetCollection(
  26. uriTemplate: '/attribute-options',
  27. routePrefix: '/api/shop',
  28. openapi: new Model\Operation(
  29. tags: ['Attribute'],
  30. summary: 'List all attribute options',
  31. description: 'Returns all attribute options.',
  32. ),
  33. ),
  34. new Get(
  35. uriTemplate: '/attribute-options/{id}',
  36. routePrefix: '/api/shop',
  37. openapi: new Model\Operation(
  38. tags: ['Attribute'],
  39. summary: 'Get a single attribute option by ID',
  40. ),
  41. ),
  42. ],
  43. )]
  44. #[ApiProperty(property: 'product_count', writable: false, readable: true, nativeType: new BuiltinType(TypeIdentifier::INT))]
  45. class AttributeOption extends \Webkul\Attribute\Models\AttributeOption
  46. {
  47. /**
  48. * Static cache of product counts resolved by FilterableAttributesProvider.
  49. *
  50. * Keyed by attribute id, then option id. The provider computes the counts
  51. * once per request and stores them here so the GraphQL "options" relation
  52. * (which re-queries options through a separate cursor-connection provider
  53. * and therefore produces fresh model instances) can still surface the
  54. * correct product count.
  55. *
  56. * @var array<int, array<int, int>>
  57. */
  58. private static array $resolvedProductCounts = [];
  59. /**
  60. * Store the resolved product counts for the given attribute's options.
  61. *
  62. * @param array<int, int> $counts option id => count
  63. */
  64. public static function setResolvedProductCounts(int $attributeId, array $counts): void
  65. {
  66. static::$resolvedProductCounts[$attributeId] = $counts;
  67. }
  68. /**
  69. * Number of visible products associated with this option within the
  70. * current category context.
  71. *
  72. * Prefer the value computed and cached by FilterableAttributesProvider;
  73. * this keeps the count correct even when the option is fetched through
  74. * the GraphQL cursor-connection relation instead of the eager-loaded
  75. * collection.
  76. */
  77. public function getProductCountAttribute(): int
  78. {
  79. $attributeId = (int) ($this->attribute_id ?? 0);
  80. if (isset(static::$resolvedProductCounts[$attributeId][(int) $this->id])) {
  81. return (int) static::$resolvedProductCounts[$attributeId][(int) $this->id];
  82. }
  83. return 0;
  84. }
  85. #[ApiProperty(identifier: true, writable: false)]
  86. public function getId(): ?int
  87. {
  88. return $this->id;
  89. }
  90. #[ApiProperty(readableLink: true)]
  91. public function getTranslations()
  92. {
  93. return $this->translations;
  94. }
  95. #[ApiProperty(readableLink: true, description: 'Current locale translation')]
  96. public function getTranslation(?string $locale = null, ?bool $withFallback = null): ?EloquentModel
  97. {
  98. return $this->translation;
  99. }
  100. }