| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace Webkul\BagistoApi\Models;
- use ApiPlatform\Metadata\ApiProperty;
- use ApiPlatform\Metadata\ApiResource;
- use ApiPlatform\Metadata\Get;
- use ApiPlatform\Metadata\GetCollection;
- use ApiPlatform\OpenApi\Model;
- use Illuminate\Database\Eloquent\Model as EloquentModel;
- use Symfony\Component\TypeInfo\Type\BuiltinType;
- use Symfony\Component\TypeInfo\TypeIdentifier;
- #[ApiResource(
- shortName: 'AttributeOption',
- description: 'Attribute option resource',
- operations: [
- new GetCollection(
- uriTemplate: '/attribute_options',
- routePrefix: '/api/admin',
- security: "is_granted('ROLE_ADMIN')",
- ),
- new Get(
- uriTemplate: '/attribute_options/{id}',
- routePrefix: '/api/admin',
- security: "is_granted('ROLE_ADMIN')",
- ),
- new GetCollection(
- uriTemplate: '/attribute-options',
- routePrefix: '/api/shop',
- openapi: new Model\Operation(
- tags: ['Attribute'],
- summary: 'List all attribute options',
- description: 'Returns all attribute options.',
- ),
- ),
- new Get(
- uriTemplate: '/attribute-options/{id}',
- routePrefix: '/api/shop',
- openapi: new Model\Operation(
- tags: ['Attribute'],
- summary: 'Get a single attribute option by ID',
- ),
- ),
- ],
- )]
- #[ApiProperty(property: 'product_count', writable: false, readable: true, nativeType: new BuiltinType(TypeIdentifier::INT))]
- class AttributeOption extends \Webkul\Attribute\Models\AttributeOption
- {
- /**
- * Static cache of product counts resolved by FilterableAttributesProvider.
- *
- * Keyed by attribute id, then option id. The provider computes the counts
- * once per request and stores them here so the GraphQL "options" relation
- * (which re-queries options through a separate cursor-connection provider
- * and therefore produces fresh model instances) can still surface the
- * correct product count.
- *
- * @var array<int, array<int, int>>
- */
- private static array $resolvedProductCounts = [];
- /**
- * Store the resolved product counts for the given attribute's options.
- *
- * @param array<int, int> $counts option id => count
- */
- public static function setResolvedProductCounts(int $attributeId, array $counts): void
- {
- static::$resolvedProductCounts[$attributeId] = $counts;
- }
- /**
- * Number of visible products associated with this option within the
- * current category context.
- *
- * Prefer the value computed and cached by FilterableAttributesProvider;
- * this keeps the count correct even when the option is fetched through
- * the GraphQL cursor-connection relation instead of the eager-loaded
- * collection.
- */
- public function getProductCountAttribute(): int
- {
- $attributeId = (int) ($this->attribute_id ?? 0);
- if (isset(static::$resolvedProductCounts[$attributeId][(int) $this->id])) {
- return (int) static::$resolvedProductCounts[$attributeId][(int) $this->id];
- }
- return 0;
- }
- #[ApiProperty(identifier: true, writable: false)]
- public function getId(): ?int
- {
- return $this->id;
- }
- #[ApiProperty(readableLink: true)]
- public function getTranslations()
- {
- return $this->translations;
- }
- #[ApiProperty(readableLink: true, description: 'Current locale translation')]
- public function getTranslation(?string $locale = null, ?bool $withFallback = null): ?EloquentModel
- {
- return $this->translation;
- }
- }
|