diff --git a/src/SslCertificate.php b/src/SslCertificate.php index dcdf92c..d4a2aaf 100644 --- a/src/SslCertificate.php +++ b/src/SslCertificate.php @@ -333,4 +333,15 @@ public function isPreCertificate(): bool return true; } + + public function __serialize(): array { + $data = $this->toArray(); + $data['publicKeyDetail'] = base64_encode(serialize($data['publicKeyDetail'])); + return $data; + } + + public function __unserialize($data): void { + $data['publicKeyDetail'] = unserialize(base64_decode($data['publicKeyDetail'])); + $this->__construct(...$data); + } } diff --git a/tests/SslCertificateSerializationTest.php b/tests/SslCertificateSerializationTest.php new file mode 100644 index 0000000..f9fe492 --- /dev/null +++ b/tests/SslCertificateSerializationTest.php @@ -0,0 +1,28 @@ +toArray() + ); + + expect(json_last_error_msg())->toEqual('Malformed UTF-8 characters, possibly incorrectly encoded'); +}); + +it('can json encode serialized certificate', function() { + $json = json_encode( + serialize(SslCertificate::createFromFile(__DIR__ . '/stubs/spatieCertificate.pem')) + ); + + expect(json_last_error_msg())->toEqual('No error'); +}); + +it('can unserialize serialized certificate', function() { + $serialized = serialize(SslCertificate::createFromFile(__DIR__ . '/stubs/spatieCertificate.pem')); + + $unserialized = unserialize($serialized); + + expect($unserialized->getDomain())->toEqual("analytics.spatie.be"); +}); \ No newline at end of file