$levels */ public function __construct( public AccessScope $scope, public ?string $uuid, public array $levels, ) {} /** * @param list $levels */ public static function team(array $levels): self { return new self(AccessScope::Team, null, $levels); } /** * @param array{scope?: string, uuid?: string|null, levels?: list} $data */ public static function fromArray(array $data): ?self { $scope = AccessScope::tryFrom((string) ($data['scope'] ?? '')); if ($scope === null) { return null; } $uuid = $scope === AccessScope::Team ? null : ($data['uuid'] ?? null); if ($scope !== AccessScope::Team && (! is_string($uuid) || $uuid === '')) { return null; } return new self($scope, $uuid, AccessLevel::parseMany($data['levels'] ?? [])); } /** * @return list */ public static function listFrom(mixed $stored): array { if (is_string($stored)) { $stored = json_decode($stored, true); } if (! is_array($stored)) { return []; } $grants = []; foreach ($stored as $item) { $grant = is_array($item) ? self::fromArray($item) : null; if ($grant !== null) { $grants[] = $grant; } } return $grants; } /** * @param list $grants * @return list}> */ public static function listToArray(array $grants): array { return array_map(fn (self $grant) => $grant->toArray(), $grants); } /** * @return array{scope: string, uuid: string|null, levels: list} */ public function toArray(): array { return [ 'scope' => $this->scope->value, 'uuid' => $this->uuid, 'levels' => array_map(fn (AccessLevel $level) => $level->value, $this->levels), ]; } }