Skip to content

Commit

Permalink
feature/compress
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Mauricio Uñate Castro committed Jul 6, 2024
1 parent af1298c commit 671ff6d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"ext-openssl": "*",
"ext-session": "*",
"ext-tokenizer": "*",
"ext-zlib": "*",
"composer-runtime-api": "^2.2",
"brick/math": "^0.9.3|^0.10.2|^0.11|^0.12",
"doctrine/inflector": "^2.0.5",
Expand Down Expand Up @@ -165,6 +166,7 @@
"ext-pdo": "Required to use all database features.",
"ext-posix": "Required to use all features of the queue worker.",
"ext-redis": "Required to use the Redis cache and queue drivers (^4.0|^5.0|^6.0).",
"ext-zlib" : "Required for data compression and decompression operations.",
"ably/ably-php": "Required to use the Ably broadcast driver (^1.0).",
"aws/aws-sdk-php": "Required to use the SQS queue driver, DynamoDb failed job storage, and SES mail driver (^3.235.5).",
"brianium/paratest": "Required to run tests in parallel (^7.0|^8.0).",
Expand Down
32 changes: 32 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,38 @@ public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?st
return mb_convert_case($string, $mode, $encoding);
}

/**
* Compresses a string using gzip compression.
*
* @param string $string The string to compress.
* @param int $mode Compression level (1 to 9, defaults to 5).
* @return string|false The compressed string, or false on failure.
*/
public static function compress(string $string, int $mode = 5)
{
// Ensure compression mode is within valid range
if ($mode < 1) {
$mode = 1;
} elseif ($mode > 9) {
$mode = 9;
}

// Compress the string using gzip
return gzcompress($string, $mode);
}

/**
* Decompresses a gzip compressed string.
*
* @param string $compressedString The compressed string.
* @return string|false The decompressed string, or false on failure.
*/
public static function decompress(string $compressedString)
{
// Decompress the gzip compressed string
return gzuncompress($compressedString);
}

/**
* Determine if a given string ends with a given substring.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,36 @@ public function convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8
return new static(Str::convertCase($this->value, $mode, $encoding));
}

/**
* Compresses a string using gzip compression.
*
* @param int $mode Compression level (1 to 9, defaults to 5).
* @return string|false The compressed string, or false on failure.
*/
public function compress(int $mode = 5)
{
// Ensure compression mode is within valid range
if ($mode < 1) {
$mode = 1;
} elseif ($mode > 9) {
$mode = 9;
}

// Compress the string using gzip
return gzcompress($this->value, $mode);
}

/**
* Decompresses a gzip compressed string.
*
* @return string|false The decompressed string, or false on failure.
*/
public function decompress()
{
// Decompress the gzip compressed string
return new static(gzuncompress($this->value));
}

/**
* Get the parent directory's path.
*
Expand Down

0 comments on commit 671ff6d

Please sign in to comment.