9) { throw new InvalidArgumentException('Level must be' . " between 0 and 9, $level given."); } $this->level = $level; $this->base = new Native(); } /** * Return the level of compression between 0 and 9. * * @internal * * @return int */ public function level() : int { return $this->level; } /** * Serialize a persistable object and return the data. * * @param Persistable $persistable * @return Encoding */ public function serialize(Persistable $persistable) : Encoding { $encoding = $this->base->serialize($persistable); $data = gzencode($encoding, $this->level); if ($data === false) { throw new RuntimeException('Failed to compress data.'); } return new Encoding($data); } /** * Deserialize a persistable object and return it. * * @param Encoding $encoding * @throws RuntimeException * @return Persistable */ public function deserialize(Encoding $encoding) : Persistable { $data = gzdecode($encoding); if ($data === false) { throw new RuntimeException('Failed to decompress data.'); } return $this->base->deserialize(new Encoding($data)); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return "Gzip (level: {$this->level})"; } }