entitlements->resolveForEvent($event); if (! $entitlement['allowed']) { return false; } $allowedSources = $this->allowedSources($style); if (! in_array((string) $entitlement['granted_by'], $allowedSources, true)) { return false; } $requiredPackageFeatures = $this->requiredPackageFeatures($style); if ($requiredPackageFeatures === []) { return true; } $packageFeatures = $this->resolveEventPackageFeatures($event); foreach ($requiredPackageFeatures as $requiredFeature) { if (! in_array($requiredFeature, $packageFeatures, true)) { return false; } } return true; } /** * @param Collection $styles * @return Collection */ public function filterStylesForEvent(Event $event, Collection $styles): Collection { return $styles ->filter(fn (AiStyle $style): bool => $this->canUseStyle($event, $style)) ->values(); } /** * @return array */ private function allowedSources(AiStyle $style): array { $metadataSources = $this->normalizeStringList(Arr::get($style->metadata ?? [], 'entitlements.allowed_sources', [])); if ($metadataSources !== []) { return $metadataSources; } if (is_bool(Arr::get($style->metadata ?? [], 'entitlements.allow_with_addon'))) { return Arr::get($style->metadata ?? [], 'entitlements.allow_with_addon') ? ['package', 'addon'] : ['package']; } return $style->is_premium ? ['package'] : ['package', 'addon']; } /** * @return array */ private function requiredPackageFeatures(AiStyle $style): array { return $this->normalizeStringList( Arr::get($style->metadata ?? [], 'entitlements.required_package_features', []) ); } /** * @return array */ private function resolveEventPackageFeatures(Event $event): array { $eventPackage = $event->relationLoaded('eventPackage') && $event->eventPackage ? $event->eventPackage : $event->eventPackage()->with('package')->first(); if (! $eventPackage instanceof EventPackage) { return []; } $package = $eventPackage->relationLoaded('package') ? $eventPackage->package : $eventPackage->package()->first(); return $this->normalizeFeatureList($package?->features); } /** * @return array */ private function normalizeFeatureList(mixed $value): array { if (is_string($value)) { $decoded = json_decode($value, true); if (json_last_error() === JSON_ERROR_NONE) { $value = $decoded; } } if (! is_array($value)) { return []; } if (array_is_list($value)) { return $this->normalizeStringList($value); } return $this->normalizeStringList(array_keys(array_filter($value, static fn (mixed $enabled): bool => (bool) $enabled))); } /** * @return array */ private function normalizeStringList(array $values): array { return array_values(array_unique(array_filter(array_map( static fn (mixed $value): string => trim((string) $value), $values )))); } }