needed($version); if ($needed === []) { return false; } $now = now(); $byAcquisition = Grant::query() ->where('status', GrantStatus::Aktiv) ->whereHas('acquisition', fn ($query) => $query->where('party_id', $holder->id)) ->get() ->filter(fn (Grant $grant): bool => ($grant->valid_from === null || ! $grant->valid_from->greaterThan($now)) && ($grant->valid_until === null || $grant->valid_until->greaterThan($now))) ->groupBy('acquisition_id'); foreach ($byAcquisition as $grants) { $have = []; foreach ($grants as $grant) { $have[$grant->license_article_id.'|'.$this->fingerprint($grant->rights_snapshot ?? [])] = true; } if (array_diff_key($needed, $have) === []) { return true; } } return false; } /** * @return array */ private function needed(OfferVersion $version): array { $needed = []; $version->loadMissing('resolvedScopes.rights'); foreach ($version->resolvedScopes as $scope) { $needed[$scope->license_article_id.'|'.$this->fingerprint([ 'purpose' => $scope->rights->purpose, 'capabilities' => $scope->rights->capabilities, 'options' => $scope->rights->options ?? [], ])] = true; } return $needed; } /** * Reihenfolge von Schlüsseln und Aktionen spielt keine Rolle. * * @param array $rights */ private function fingerprint(array $rights): string { $capabilities = []; foreach ((array) ($rights['capabilities'] ?? []) as $class => $actions) { $actions = array_values(array_unique((array) $actions)); sort($actions); $capabilities[(string) $class] = $actions; } ksort($capabilities); $options = (array) ($rights['options'] ?? []); ksort($options); return md5(json_encode([ 'purpose' => (string) ($rights['purpose'] ?? ''), 'capabilities' => $capabilities, 'options' => $options, ], JSON_THROW_ON_ERROR)); } }