Skip to content

Commit 70e5d95

Browse files
committed
Added docblocks to ByteFormatter and Base.
Moved testAutomaticUnitSwitching() from ByteFormatterTest to SymbolDecoratorTest. Minor copy edit in README.
1 parent a600884 commit 70e5d95

File tree

5 files changed

+91
-22
lines changed

5 files changed

+91
-22
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ByteFormatter
55
[![Build status][Build image]][Build]
66

77
ByteFormatter is a [PSR-2](http://www.php-fig.org/psr/psr-2/) compliant PHP library that formats byte values as
8-
human-readable strings. An appropriate scale is calculated automatically such that the value never exceeds the base.
8+
human-readable strings. An appropriate exponent is calculated automatically such that the value never exceeds the base.
99
For example, in base 1024, `format(1023)` gives *1023 B* but `format(1024)` gives *1 KiB* instead of *1024 B*.
1010

1111
Requirements
@@ -162,4 +162,3 @@ in this document can be found in `DocumentationTest`.
162162
[Version image]: http://img.shields.io/github/tag/ScriptFUSION/ByteFormatter.svg "Latest version"
163163
[Build]: http://travis-ci.org/ScriptFUSION/ByteFormatter
164164
[Build image]: http://img.shields.io/travis/ScriptFUSION/ByteFormatter.svg "Build status"
165-

src/Byte/Base.php

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

4+
/**
5+
* Specifies an exponentiation base.
6+
*/
47
final class Base
58
{
69
const

src/Byte/ByteFormatter.php

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ class ByteFormatter
1717
/** @var string */
1818
private $format;
1919

20+
/** @var string */
21+
private $sprintfFormat;
22+
2023
/** @var int */
2124
private $precision = 0;
2225

23-
/** @var string */
24-
private $normalizedFormat;
25-
2626
/** @var UnitDecorator */
2727
private $unitDecorator;
2828

29+
/**
30+
* Initializes this instance, optionally with a specific unit decorator.
31+
* If no unit decorator is specified, SymbolDecorator will be used.
32+
*
33+
* @param UnitDecorator|null $unitDecorator Optional. Unit decorator.
34+
*/
2935
public function __construct(UnitDecorator $unitDecorator = null)
3036
{
3137
$this
@@ -34,51 +40,105 @@ public function __construct(UnitDecorator $unitDecorator = null)
3440
;
3541
}
3642

43+
/**
44+
* Formats the specified number of bytes as a human-readable string.
45+
*
46+
* @param int $bytes Number of bytes.
47+
* @param int|null $precision Optional. Number of fractional digits.
48+
*
49+
* @return string Formatted bytes.
50+
*/
3751
public function format($bytes, $precision = null)
3852
{
39-
$precision = $precision === null ? $this->precision : $precision;
53+
// Use default precision when not specified.
54+
$precision === null && $precision = $this->precision;
55+
4056
$log = log($bytes, $this->base);
4157
$exponent = max(0, $log|0);
4258
$value = round(pow($this->base, $log - $exponent), $precision);
4359
$units = $this->getUnitDecorator()->decorate($exponent, $this->base, $value);
4460

45-
return trim(sprintf($this->normalizedFormat, $value, $units));
61+
return trim(sprintf($this->sprintfFormat, $value, $units));
4662
}
4763

48-
private function normalizeFormat($format)
64+
/**
65+
* Coverts a format specifier into a sprintf() compatible format.
66+
*
67+
* @param string $format Format specifier.
68+
*
69+
* @return string sprintf() format.
70+
*/
71+
private function convertFormat($format)
4972
{
5073
return str_replace(['%v', '%u'], ['%1$s', '%2$s'], $format);
5174
}
5275

76+
/**
77+
* Gets the exponentiation base.
78+
*
79+
* @return int Exponentiation base.
80+
*/
5381
public function getBase()
5482
{
5583
return $this->base;
5684
}
5785

86+
/**
87+
* Sets the exponentiation base which should usually be a Base constant.
88+
*
89+
* @param int $base Exponentiation base.
90+
*
91+
* @return $this
92+
*/
5893
public function setBase($base)
5994
{
6095
$this->base = $base|0;
6196

6297
return $this;
6398
}
6499

100+
/**
101+
* Gets the format specifier.
102+
*
103+
* @return string Format specifier.
104+
*/
65105
public function getFormat()
66106
{
67107
return $this->format;
68108
}
69109

110+
/**
111+
* Sets the format specifier. Occurrences of %v and %u will be replaced
112+
* with formatted byte values and units, respectively.
113+
*
114+
* @param string $format Format specifier.
115+
*
116+
* @return $this
117+
*/
70118
public function setFormat($format)
71119
{
72-
$this->normalizedFormat = $this->normalizeFormat($this->format = "$format");
120+
$this->sprintfFormat = $this->convertFormat($this->format = "$format");
73121

74122
return $this;
75123
}
76124

125+
/**
126+
* Gets the maximum number of fractional digits.
127+
*
128+
* @return int Fractional digits.
129+
*/
77130
public function getPrecision()
78131
{
79132
return $this->precision;
80133
}
81134

135+
/**
136+
* Sets the maximum number of fractional digits.
137+
*
138+
* @param $precision
139+
*
140+
* @return $this
141+
*/
82142
public function setPrecision($precision)
83143
{
84144
$this->precision = $precision|0;
@@ -87,16 +147,25 @@ public function setPrecision($precision)
87147
}
88148

89149
/**
150+
* Gets the unit decorator.
151+
*
90152
* @return UnitDecorator
91153
*/
92154
public function getUnitDecorator()
93155
{
94156
return $this->unitDecorator;
95157
}
96158

97-
public function setUnitDecorator(UnitDecorator $collection)
159+
/**
160+
* Sets the unit decorator.
161+
*
162+
* @param UnitDecorator $decorator Unit decorator.
163+
*
164+
* @return $this
165+
*/
166+
public function setUnitDecorator(UnitDecorator $decorator)
98167
{
99-
$this->unitDecorator = $collection;
168+
$this->unitDecorator = $decorator;
100169

101170
return $this;
102171
}

test/Integration/Byte/ByteFormatterTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,6 @@ public function provideFormats()
119119
];
120120
}
121121

122-
public function testAutomaticUnitSwitching()
123-
{
124-
$formatter = $this->formatter->setUnitDecorator(new SymbolDecorator);
125-
126-
$formatter->setBase(Base::BINARY);
127-
$this->assertSame('1KiB', $formatter->format($formatter->getBase()));
128-
129-
$formatter->setBase(Base::DECIMAL);
130-
$this->assertSame('1KB', $formatter->format($formatter->getBase()));
131-
}
132-
133122
public function testCustomUnitSequence()
134123
{
135124
$formatter = (new ByteFormatter)->setUnitDecorator(

test/Unit/Byte/Unit/SymbolDecoratorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace ScriptFUSIONTest\Unit\Byte\Unit;
33

4+
use ScriptFUSION\Byte\Base;
45
use ScriptFUSION\Byte\Unit\SymbolDecorator;
56

67
final class SymbolDecoratorTest extends \PHPUnit_Framework_TestCase
@@ -40,4 +41,12 @@ public function testIecSuffix()
4041
$this->assertSame($symbol, $decorator->decorate($exponent, 0, 0));
4142
}
4243
}
44+
45+
public function testAutomaticUnitSwitching()
46+
{
47+
$decorator = new SymbolDecorator;
48+
49+
$this->assertSame('KiB', $decorator->decorate(1, Base::BINARY, 0));
50+
$this->assertSame('KB', $decorator->decorate(1, Base::DECIMAL, 0));
51+
}
4352
}

0 commit comments

Comments
 (0)