-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php namespace io\streams\compress; | ||
|
||
use io\streams\Compression; | ||
use lang\Value; | ||
use util\Comparison; | ||
|
||
/** @test io.streams.compress.unittest.OptionsTest */ | ||
class Options implements Value { | ||
use Comparison; | ||
|
||
public $level, $length; | ||
|
||
/** | ||
* Compression options | ||
* | ||
* @param ?int $level | ||
* @param ?int $length | ||
*/ | ||
public function __construct( | ||
$level= null, | ||
$length= null | ||
) { | ||
$this->level= $level ?? Compression::DEFAULT; | ||
$this->length= $length; | ||
} | ||
|
||
/** @param ?int|[:var]|self $arg */ | ||
public static function from($arg): self { | ||
if (null === $arg) { | ||
return new self(); | ||
} else if ($arg instanceof self) { | ||
return $arg; | ||
} else if (is_array($arg)) { | ||
return new self( | ||
$arg['level'] ?? null, | ||
$arg['length'] ?? null | ||
); | ||
} else { | ||
return new self($arg); | ||
} | ||
} | ||
|
||
/** @return string */ | ||
public function toString() { | ||
switch ($this->level) { | ||
case Compression::FASTEST: $level= 'FASTEST'; break; | ||
case Compression::DEFAULT: $level= 'DEFAULT'; break; | ||
case Compression::STRONGEST: $level= 'STRONGEST'; break; | ||
default: $level= $this->level; | ||
} | ||
|
||
return sprintf( | ||
'%s(level: %s, length: %s)', | ||
nameof($this), | ||
$level, | ||
null === $this->length ? 'null' : $this->length | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/php/io/streams/compress/unittest/OptionsTest.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php namespace io\streams\compress\unittest; | ||
|
||
use io\streams\Compression; | ||
use io\streams\compress\Options; | ||
use test\{Assert, Test, Values}; | ||
|
||
class OptionsTest { | ||
const LENGTH= 6100; | ||
|
||
/** @return iterable */ | ||
private function maps() { | ||
yield [[], Compression::DEFAULT, null]; | ||
yield [['unused' => -1], Compression::DEFAULT, null]; | ||
yield [['level' => Compression::FASTEST], Compression::FASTEST, null]; | ||
yield [['level' => Compression::FASTEST, 'length' => self::LENGTH], Compression::FASTEST, self::LENGTH]; | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new Options(); | ||
} | ||
|
||
#[Test] | ||
public function default_level() { | ||
Assert::equals(Compression::DEFAULT, (new Options())->level); | ||
} | ||
|
||
#[Test] | ||
public function default_length() { | ||
Assert::null((new Options())->length); | ||
} | ||
|
||
#[Test, Values([Compression::FASTEST, Compression::DEFAULT, Compression::STRONGEST])] | ||
public function level($level) { | ||
Assert::equals($level, (new Options($level))->level); | ||
} | ||
|
||
#[Test] | ||
public function length() { | ||
Assert::equals(self::LENGTH, (new Options(null, self::LENGTH))->length); | ||
} | ||
|
||
#[Test] | ||
public function from_level() { | ||
Assert::equals(new Options(Compression::FASTEST, null), Options::from(Compression::FASTEST)); | ||
} | ||
|
||
#[Test] | ||
public function from_null() { | ||
Assert::equals(new Options(Compression::DEFAULT, null), Options::from(null)); | ||
} | ||
|
||
#[Test] | ||
public function from_options() { | ||
$options= new Options(Compression::FASTEST, self::LENGTH); | ||
Assert::equals($options, Options::from($options)); | ||
} | ||
|
||
#[Test, Values(from: 'maps')] | ||
public function from($map, $level, $length) { | ||
Assert::equals(new Options($level, $length), Options::from($map)); | ||
} | ||
|
||
#[Test] | ||
public function string_representation() { | ||
Assert::equals( | ||
'io.streams.compress.Options(level: DEFAULT, length: null)', | ||
(new Options())->toString() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function string_representation_with_length() { | ||
Assert::equals( | ||
'io.streams.compress.Options(level: DEFAULT, length: 6100)', | ||
(new Options(null, self::LENGTH))->toString() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function string_representation_with_level() { | ||
Assert::equals( | ||
'io.streams.compress.Options(level: 22, length: null)', | ||
(new Options(22))->toString() | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
...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
There was a problem hiding this comment.
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.