Skip to content

Commit

Permalink
Added option to customize SymbolDecorator prefixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Oct 17, 2020
1 parent dd94d9e commit 9dca899
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
16 changes: 9 additions & 7 deletions .travis.yml
@@ -1,11 +1,15 @@
sudo: false
notifications:
email: false

language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

env:
matrix:
Expand All @@ -17,19 +21,17 @@ matrix:

cache:
directories:
- .composer/cache
- vendor

install:
- alias composer=composer\ -n && composer selfupdate
- composer validate
- composer update $DEPENDENCIES
- composer update --no-progress --no-suggest $DEPENDENCIES

script:
- composer test -- --coverage-clover=build/logs/clover.xml

after_success:
- composer require satooshi/php-coveralls
- vendor/bin/coveralls -v
- composer require php-coveralls/php-coveralls:^2
- vendor/bin/php-coveralls -v

notifications:
email: false
15 changes: 14 additions & 1 deletion src/Byte/Unit/SymbolDecorator.php
Expand Up @@ -13,6 +13,7 @@ class SymbolDecorator implements UnitDecorator
const SUFFIX_METRIC = 'B';
const SUFFIX_IEC = 'iB';

private $prefixes = self::PREFIXES;
private $suffix;
private $alwaysShowUnit;

Expand All @@ -39,7 +40,19 @@ public function decorate($exponent, $base, $value)
return $suffix !== static::SUFFIX_NONE || $this->alwaysShowUnit ? 'B' : '';
}

return substr(static::PREFIXES, min($exponent, strlen(static::PREFIXES)) - 1, 1) . $suffix;
return $this->prefixes[min($exponent, strlen($this->prefixes)) - 1] . $suffix;
}

public function getPrefixes()
{
return $this->prefixes;
}

public function setPrefixes($prefixes)
{
$this->prefixes = (string)$prefixes;

return $this;
}

public function getSuffix()
Expand Down
9 changes: 9 additions & 0 deletions test/Unit/Byte/Unit/SymbolDecoratorTest.php
Expand Up @@ -54,4 +54,13 @@ public function testSuffix()
{
self::assertSame($suffix = 'foo', (new SymbolDecorator)->setSuffix($suffix)->getSuffix());
}

public function testCustomPrefixes()
{
$decorator = (new SymbolDecorator())->setPrefixes('XYZ');

foreach (['B', 'X', 'Y', 'Z', 'Z', 'Z'] as $exponent => $symbol) {
self::assertSame($symbol, $decorator->decorate($exponent, 0, 0));
}
}
}

0 comments on commit 9dca899

Please sign in to comment.