39 public const DEFAULT_LEVEL = 7;
40 public const DEFAULT_THRESHOLD = 256;
41 public const DEFAULT_MAX_DECOMPRESSION_SIZE = 8 * 1024 * 1024;
46 private static function make() :
self{
47 return new self(self::DEFAULT_LEVEL, self::DEFAULT_THRESHOLD, self::DEFAULT_MAX_DECOMPRESSION_SIZE);
50 public function __construct(
52 private ?
int $minCompressionSize,
53 private int $maxDecompressionSize
57 return $this->minCompressionSize;
64 $result = @zlib_decode($payload, $this->maxDecompressionSize);
65 if($result ===
false){
71 public function compress(
string $payload) : string{
72 $compressible = $this->minCompressionSize !== null && strlen($payload) >= $this->minCompressionSize;
73 $level = $compressible ? $this->level : 0;
75 return function_exists(
'libdeflate_deflate_compress') ?
76 libdeflate_deflate_compress($payload, $level) :
77 Utils::assumeNotFalse(zlib_encode($payload, ZLIB_ENCODING_RAW, $level),
"ZLIB compression failed");