digestSignatures = null; // 2. Serialize to JSON // We use JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES to match typical API expectations $jsonToSign = $clone->toJson(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); // 3. Sign $signature = $signer->sign($jsonToSign); // 4. Attach signature to the original object if ($this->digestSignatures === null) { $this->digestSignatures = new DataCollection(DigestSignatureData::class, []); } // We can't push to DataCollection directly if it's not array-access compatible in all versions, // but Spatie DataCollection is usually iterable. // Safest way is to reconstruct it. $current = array_values($this->digestSignatures->toCollection()->all()); $current[] = new DigestSignatureData($signature, 'cms'); $this->digestSignatures = new DataCollection(DigestSignatureData::class, $current); return $this; } }