Skip to content

Commit 9dca899

Browse files
committed
Added option to customize SymbolDecorator prefixes.
1 parent dd94d9e commit 9dca899

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

.travis.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
sudo: false
1+
notifications:
2+
email: false
23

34
language: php
45

56
php:
67
- 5.6
78
- 7.0
89
- 7.1
10+
- 7.2
11+
- 7.3
12+
- 7.4
913

1014
env:
1115
matrix:
@@ -17,19 +21,17 @@ matrix:
1721

1822
cache:
1923
directories:
20-
- .composer/cache
24+
- vendor
2125

2226
install:
2327
- alias composer=composer\ -n && composer selfupdate
2428
- composer validate
25-
- composer update $DEPENDENCIES
29+
- composer update --no-progress --no-suggest $DEPENDENCIES
2630

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

3034
after_success:
31-
- composer require satooshi/php-coveralls
32-
- vendor/bin/coveralls -v
35+
- composer require php-coveralls/php-coveralls:^2
36+
- vendor/bin/php-coveralls -v
3337

34-
notifications:
35-
email: false

src/Byte/Unit/SymbolDecorator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SymbolDecorator implements UnitDecorator
1313
const SUFFIX_METRIC = 'B';
1414
const SUFFIX_IEC = 'iB';
1515

16+
private $prefixes = self::PREFIXES;
1617
private $suffix;
1718
private $alwaysShowUnit;
1819

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

42-
return substr(static::PREFIXES, min($exponent, strlen(static::PREFIXES)) - 1, 1) . $suffix;
43+
return $this->prefixes[min($exponent, strlen($this->prefixes)) - 1] . $suffix;
44+
}
45+
46+
public function getPrefixes()
47+
{
48+
return $this->prefixes;
49+
}
50+
51+
public function setPrefixes($prefixes)
52+
{
53+
$this->prefixes = (string)$prefixes;
54+
55+
return $this;
4356
}
4457

4558
public function getSuffix()

test/Unit/Byte/Unit/SymbolDecoratorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ public function testSuffix()
5454
{
5555
self::assertSame($suffix = 'foo', (new SymbolDecorator)->setSuffix($suffix)->getSuffix());
5656
}
57+
58+
public function testCustomPrefixes()
59+
{
60+
$decorator = (new SymbolDecorator())->setPrefixes('XYZ');
61+
62+
foreach (['B', 'X', 'Y', 'Z', 'Z', 'Z'] as $exponent => $symbol) {
63+
self::assertSame($symbol, $decorator->decorate($exponent, 0, 0));
64+
}
65+
}
5766
}

0 commit comments

Comments
 (0)