Skip to content

Commit 8880e9f

Browse files
committed
Changed byte unit sequences into collections that inherit from ByteUnitCollection.
Removed Sequence namespace and classes.
1 parent d37af26 commit 8880e9f

16 files changed

+226
-194
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ The format can be changed by calling the `setFormat($format)` function which tak
5858
5959
Customizing units
6060
-----------------
61-
Units are generated by sequence generators. The current unit sequence generator instance can be retrieved from `ByteFormatter::getUnitSequence()`.
61+
Units are provided by collections that extend `ByteUnitCollection`. The current unit collection instance can be retrieved from `ByteFormatter::getUnitCollection()`.
6262

63-
Sequence generators are notified of base changes in the formatter so that different units can be generated for different bases. For example, the default generator outputs `KiB` in base 1024 for *2<sup>10</sup> < bytes < 2<sup>20</sup>* but outputs `KB` in base 1000 for *1000 < bytes < 1000000*. This behaviour can be suppressed by calling `disableAutomaticUnitSwitching(true)` to prevent units changing when the base is changed.
63+
Unit collections are notified of base changes in the formatter so that different units can be returned for different bases. For example, the default collection outputs `KiB` in base 1024 for *2<sup>10</sup> < bytes < 2<sup>20</sup>* but outputs `KB` in base 1000 for *1000 < bytes < 1000000*. This behaviour can be suppressed by calling `disableAutomaticUnitSwitching(true)` to prevent units changing when the base is changed.
6464

65-
### Byte unit symbol sequence
65+
### Symbol collection
6666

67-
`ByteUnitSymbolSequence` is the default unit sequence and generates units like *B*, *KB*, *MB*, etc. The symbol's suffix can be changed according to one of the constants from the following table.
67+
`ByteUnitSymbolCollection` is the default unit collection and returns units like *B*, *KB*, *MB*, etc. The symbol's suffix can be changed using one of the class constants from the following table.
6868

6969
| Constant | B | K | M | G | T | P | E | Z | Y |
7070
|---------------|:-:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
@@ -77,7 +77,7 @@ The following example uses base 1024 but displays the metric suffix, like Window
7777
```php
7878
(new ByteFormatter)
7979
->setBase(Base::BINARY)
80-
->setUnitSymbolSuffix(ByteUnitSymbolSequence::SUFFIX_METRIC)
80+
->setUnitSymbolSuffix(ByteUnitSymbolCollection::SUFFIX_METRIC)
8181
->format(0x80000);
8282
```
8383
> 512 KB
@@ -86,32 +86,32 @@ If you prefer terse notation the suffix may be removed with `SUFFIX_NONE`.
8686

8787
```php
8888
(new ByteFormatter)
89-
->setUnitSymbolSuffix(ByteUnitSymbolSequence::SUFFIX_NONE)
89+
->setUnitSymbolSuffix(ByteUnitSymbolCollection::SUFFIX_NONE)
9090
->format(0x80000);
9191
```
9292
> 512 K
9393
94-
Note that no unit is displayed for bytes when the suffix is disabled. If this is undesired, byte units can be forced with `ByteUnitSymbolSequence::alwaysShowUnit()`.
94+
Note that no unit is displayed for bytes when the suffix is disabled. If this is undesired, byte units can be forced with `ByteUnitSymbolCollection::alwaysShowUnit()`.
9595

9696
```php
9797
$formatter = (new ByteFormatter)
98-
->setUnitSymbolSuffix(ByteUnitSymbolSequence::SUFFIX_NONE)
98+
->setUnitSymbolSuffix(ByteUnitSymbolCollection::SUFFIX_NONE)
9999
->format(512);
100100
```
101101
> 512
102102
103103
```php
104-
$formatter->getUnitSequence()->alwaysShowUnit();
104+
$formatter->getUnitCollection()->alwaysShowUnit();
105105
$formatter->format(512);
106106
```
107107
> 512 B
108108
109-
### Byte unit name sequence
110-
`ByteUnitNameSequence` can be used to replace the default unit sequence generator and generates units like *byte*, *kilobyte*, *megabyte*, etc.
109+
### Name collection
110+
`ByteUnitNameCollection` can be used to replace the default collection and returns units like *byte*, *kilobyte*, *megabyte*, etc.
111111

112112
```php
113113
(new ByteFormatter)
114-
->setUnitSequence(new ByteUnitNameSequence)
114+
->setUnitCollection(new ByteUnitNameCollection)
115115
->format(0x80000);
116116
```
117117
> 512 kibibytes
@@ -120,7 +120,7 @@ Using decimal base:
120120

121121
```php
122122
(new ByteFormatter)
123-
->setUnitSequence(new ByteUnitNameSequence)
123+
->setUnitCollection(new ByteUnitNameCollection)
124124
->setBase(Base::DECIMAL)
125125
->format(500000);
126126
```

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Byte/ByteFormatter.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace ScriptFUSION\Byte;
33

4-
use ScriptFUSION\Sequence\FiniteSequence;
5-
64
/**
75
* Formats byte values as human-readable strings.
86
*/
@@ -16,7 +14,7 @@ class ByteFormatter implements BaseAware {
1614

1715
private
1816
$normalizedFormat,
19-
$unitSequence
17+
$unitCollection
2018
;
2119

2220
public function __construct($precision = 0, $base = Base::BINARY, $format = '%v %u') {
@@ -27,24 +25,31 @@ public function format($bytes) {
2725
$log = log($bytes, $this->base);
2826
$scale = max(0, $log|0);
2927
$value = round(pow($this->base, $log - $scale), $this->precision);
30-
$units = $this->getUnitSequence()->getSequenceIndex($scale);
28+
$units = $this->getUnits($value, $scale);
3129

3230
return sprintf($this->normalizedFormat, $value, $units);
3331
}
3432

33+
protected function getUnits($value, $scale) {
34+
$collection = $this->getUnitCollection();
35+
$collection instanceof ValueAware && $collection->setValue($value);
36+
37+
return $collection[$scale];
38+
}
39+
3540
private function normalizeFormat($format) {
3641
return str_replace(['%v', '%u'], ['%1$s', '%2$s'], $format);
3742
}
3843

3944
/**
40-
* Notifies the unit sequence of base changes.
45+
* Notifies the unit collection of base changes.
4146
*
4247
* @param int $base Numeric base.
4348
*/
4449
private function notifyBaseChanged($base) {
4550
$this->automaticUnitSwitching
46-
&& ($seq = $this->getUnitSequence()) instanceof BaseAware
47-
&& $seq->setBase($base);
51+
&& ($collection = $this->getUnitCollection()) instanceof BaseAware
52+
&& $collection->setBase($base);
4853
}
4954

5055
public function getBase() {
@@ -74,34 +79,34 @@ public function setPrecision($precision) {
7479
return $this;
7580
}
7681

77-
public function getUnitSequence() {
78-
return $this->unitSequence ?: $this->unitSequence = new ByteUnitSymbolSequence;
82+
public function getUnitCollection() {
83+
return $this->unitCollection ?: $this->unitCollection = new ByteUnitSymbolCollection;
7984
}
80-
public function setUnitSequence(FiniteSequence $sequence) {
81-
$this->unitSequence = $sequence;
85+
public function setUnitCollection(ByteUnitCollection $collection) {
86+
$this->unitCollection = $collection;
8287

83-
//Notify new sequence of current base.
88+
//Notify new collection of current base.
8489
$this->notifyBaseChanged($this->base);
8590

8691
return $this;
8792
}
8893

8994
/**
9095
* Sets the unit symbol suffix to the specified value.
91-
* This is a shortcut method for interfacing with the default unit sequence generator and cannot be used if the
92-
* default generator has been replaced with a non-derived object.
96+
* This is a shortcut method for interfacing with the default unit collection and cannot be used if the default
97+
* collection has been replaced with a non-derived object.
9398
*
9499
* @param string $suffix Unit symbol suffix. Defaults to no suffix.
95100
* @return $this
96-
* @throws \BadFunctionCallException when unit sequence is not an instance of ByteUnitSymbolSequence.
101+
* @throws \BadFunctionCallException when unit collection is not an instance of ByteUnitSymbolCollection.
97102
*/
98-
public function setUnitSymbolSuffix($suffix = ByteUnitSymbolSequence::SUFFIX_NONE) {
99-
if (!($seq = $this->getUnitSequence()) instanceof ByteUnitSymbolSequence)
103+
public function setUnitSymbolSuffix($suffix = ByteUnitSymbolCollection::SUFFIX_NONE) {
104+
if (!($collection = $this->getUnitCollection()) instanceof ByteUnitSymbolCollection)
100105
throw new \BadFunctionCallException(
101-
'Cannot set suffix: unit sequence not instance of ByteUnitSymbolSequence.'
106+
'Cannot set suffix: unit collection not instance of ByteUnitSymbolCollection.'
102107
);
103108

104-
$seq->setSuffix($suffix);
109+
$collection->setSuffix($suffix);
105110

106111
return $this;
107112
}

src/Byte/ByteUnitCollection.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace ScriptFUSION\Byte;
3+
4+
/**
5+
* Provides a collection of byte units.
6+
*/
7+
abstract class ByteUnitCollection implements \Countable, \ArrayAccess, \IteratorAggregate {
8+
public function count() {
9+
return 9;
10+
}
11+
12+
public function offsetExists($offset) {
13+
return $offset > 0 && $offset < count($this);
14+
}
15+
16+
public function offsetSet($offset, $value) {
17+
throw new \BadFunctionCallException('Collection is read-only.');
18+
}
19+
20+
public function offsetUnset($offset) {
21+
throw new \BadFunctionCallException('Collection is read-only.');
22+
}
23+
24+
public function getIterator() {
25+
for ($i = 0; $i < count($this); ++$i)
26+
yield $this[$i];
27+
}
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace ScriptFUSION\Byte;
3+
4+
/**
5+
* Provides a collection of byte unit names.
6+
*/
7+
class ByteUnitNameCollection extends ByteUnitCollection implements BaseAware, ValueAware {
8+
private
9+
$base,
10+
$value
11+
;
12+
13+
protected static $sequences = [
14+
Base::BINARY => [ 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi' ],
15+
Base::DECIMAL => [ 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'yotta' ],
16+
];
17+
18+
public function __construct($base = Base::BINARY) {
19+
$this->setBase($base);
20+
}
21+
22+
public function offsetGet($offset) {
23+
$suffix = $this->value == 1 ? 'byte' : 'bytes';
24+
25+
if (!$offset) return $suffix;
26+
27+
return static::$sequences[$this->base][--$offset] . $suffix;
28+
}
29+
30+
public function setBase($base) {
31+
$this->base = +$base;
32+
33+
return $this;
34+
}
35+
36+
function setValue($value) {
37+
$this->value = +$value;
38+
39+
return $this;
40+
}
41+
}

src/Byte/ByteUnitNameSequence.php

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22
namespace ScriptFUSION\Byte;
33

4-
use ScriptFUSION\Sequence\FiniteSequence;
5-
64
/**
7-
* Provides a sequence of byte unit symbols from least to most significant.
5+
* Provides a collection of byte unit symbols.
86
*/
9-
class ByteUnitSymbolSequence extends FiniteSequence implements BaseAware {
10-
protected static $prefixes = 'KMGTPEZY';
11-
7+
class ByteUnitSymbolCollection extends ByteUnitCollection implements BaseAware {
128
const
9+
PREFIXES = 'KMGTPEZY',
10+
1311
SUFFIX_NONE = '',
1412
SUFFIX_METRIC = 'B',
1513
SUFFIX_IEC = 'iB'
@@ -24,17 +22,10 @@ public function __construct($suffix = self::SUFFIX_NONE, $alwaysShowUnit = false
2422
$this->setSuffix($suffix)->alwaysShowUnit($alwaysShowUnit);
2523
}
2624

27-
public function getSequence() {
28-
yield $this->suffix !== self::SUFFIX_NONE || $this->alwaysShowUnit ? 'B' : '';
29-
30-
for ($i = 0; $i < strlen(static::$prefixes); ++$i)
31-
yield static::$prefixes[$i] . $this->suffix;
32-
}
33-
34-
public function getSequenceIndex($index) {
35-
$sequence = $this->getSequenceArray();
25+
public function offsetGet($offset) {
26+
if (!$offset) return $this->suffix !== static::SUFFIX_NONE || $this->alwaysShowUnit ? 'B' : '';
3627

37-
return $sequence[min($index, count($sequence) - 1)];
28+
return substr(static::PREFIXES, min(--$offset, count($this) - 1), 1) . $this->suffix;
3829
}
3930

4031
public function setBase($base) {

src/Byte/ValueAware.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace ScriptFUSION\Byte;
3+
4+
interface ValueAware {
5+
function setValue($value);
6+
}

src/Sequence/ArraySequence.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Sequence/FiniteSequence.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)