Skip to content

Abstract algorithm options into an Options class #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2025
Merged

Conversation

thekid
Copy link
Member

@thekid thekid commented Aug 3, 2025

Most compression algorithms only have one parameter: the compression level - GZip, for example, uses one between 0 and 9.

use io\streams\Compression;
use io\streams\compress\Options;

$gzip= Compression::named('gzip');

// Use defaults
$gzip->compress($data);

// Pass in the compression level, backwards compatible
$gzip->compress($data, Compression::STRONGEST);

// Pass in an Options instance
$gzip->compress($data, new Options(level: Compression::STRONGEST));

// Pass in an a map of options, e.g. from a configuration file
$gzip->compress($data, ['level' => Compression::STRONGEST]);

Other algorithms may require other parameters. Snappy, for example, requires the uncompressed length upfront, see #11 (comment)

use io\File;
use io\streams\{Compression, FileInputStream, FileOutputStream};
use io\streams\compress\Options;

$snappy= Compression::named('snappy');

$in= new FileInputStream('README.md');
try {
  $out= $snappy->open(new FileOutputStream('README.sn')), new Options(length: $in->size()));
  while ($in->available()) {
    $out->write($in->read()):
  }
  $out->close();
} finally {
  $in->close();
}

@thekid thekid added the enhancement New feature or request label Aug 3, 2025
@@ -21,7 +21,7 @@ public abstract function extension(): string;
public abstract function level(int $select): int;

/** Compresses data */
public abstract function compress(string $data, int $level= Compression::DEFAULT): string;
public abstract function compress(string $data, $options= null): string;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes create a BC break for classes extending this base class:

use io\streams\compress\Algorithm;

class Impl extends Algorithm {
  public function compress(string $data, int $level= -1): string { }

  /* Shortened for brevity */
}

...raises the following error: Declaration of Impl::compress(string $data, int $level = -1): string must be compatible with io\streams\compress\Algorithm::compress(string $data, $options = null): string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone calling these methods BC is ensured, see example above.

@thekid thekid merged commit 79a282b into main Aug 3, 2025
22 checks passed
@thekid thekid deleted the feature/options branch August 3, 2025 12:14
@thekid
Copy link
Member Author

thekid commented Aug 15, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant