Skip to content

Commit

Permalink
Binary suffix converter
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Apr 24, 2014
1 parent 65564e1 commit d7fd45f
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 2 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Michal Dabrowski, Norbert Orzechowicz

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#PHP Humanizer

[![Build Status](https://travis-ci.org/norzechowicz/php-humanizer.svg?branch=master)](https://travis-ci.org/norzechowicz/php-humanizer)

Humanize values to make them readable for regular people ;)

#Installation

Add to your composer.json

```
require: {
"norzechowicz/php-humanizer": "dev-master"
}
```

#Usage

## Text

**Humanize**

```php
use PHPHumanizer\String;

echo String::humanize('field_name'); // "Field Name"
echo String::humanize('user_id'); // "User"
echo String::humanize('field_name', true); // "field name"
```

## Number

**Ordinalize**

```php
use PHPHumanizer\Number;

echo Number::ordinalize(0); // "0th"
echo Number::ordinalize(1); // "1st"
echo Number::ordinalize(2); // "2nd"
echo Number::ordinalize(23); // "23rd"
echo Number::ordinalize(1002); // "1002nd"
echo Number::ordinalize(-111); // "-111th"

```

**Ordinal**

```php
use PHPHumanizer\Number;

echo Number::ordinal(0); // "th"
echo Number::ordinal(1); // "st"
echo Number::ordinal(2); // "nd"
echo Number::ordinal(23); // "rd"
echo Number::ordinal(1002); // "nd"
echo Number::ordinal(-111); // "th"
```

**Binary Suffix**

```php
use PHPHumanizer\Number;

echo Number::binarySuffix(0); // "0 bytes"
echo Number::binarySuffix(1); // "1 bytes"
echo Number::binarySuffix(1024); // "1 kB"
echo Number::binarySuffix(1025); // "1 kB"
echo Number::binarySuffix(1536); // "1.5 kB"
echo Number::binarySuffix(1048576 * 5); // "5 MB"
echo Number::binarySuffix(1073741824 * 2); // "2 GB"
echo Number::binarySuffix(1099511627776 * 3); // "3 TB"
echo Number::binarySuffix(1325899906842624); // "1.18 PB"
```

Number can be also formatted for specific locale

```php
use PHPHumanizer\Number;

echo Number::binarySuffix(1536, 'pl'); "1,5 kB"
```

# Credits

This lib was inspired by [Java Humanize Lib](https://github.com/mfornos/humanize) && [Rails Active Support](https://github.com/rails/rails/tree/master/activesupport/lib/active_support)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"symfony/intl": "~2.3"
},
"require-dev": {
"phpspec/phpspec": "2.0.*"
Expand Down
25 changes: 25 additions & 0 deletions spec/PHPHumanizer/NumberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,29 @@ function it_returns_oridinal_suffix()
$this->oridinal(-111)->shouldReturn("th");
}

function it_convert_number_to_string_with_binary_suffix()
{
$this->binarySuffix(-1)->shouldReturn(-1);
$this->binarySuffix(0)->shouldReturn("0 bytes");
$this->binarySuffix(1)->shouldReturn("1 bytes");
$this->binarySuffix(1024)->shouldReturn("1 kB");
$this->binarySuffix(1025)->shouldReturn("1 kB");
$this->binarySuffix(1536)->shouldReturn("1.5 kB");
$this->binarySuffix(1048576 * 5)->shouldReturn("5 MB");
$this->binarySuffix(1073741824 * 2)->shouldReturn("2 GB");
$this->binarySuffix(1099511627776 * 3)->shouldReturn("3 TB");
$this->binarySuffix(1325899906842624)->shouldReturn("1.18 PB");
}

function it_convert_number_to_string_with_binary_suffix_for_specific_locale()
{
$this->binarySuffix(1536, 'pl')->shouldReturn("1,5 kB");
$this->binarySuffix(1325899906842624, 'pl')->shouldReturn("1,18 PB");
}

function it_throw_exception_when_converting_to_string_with_binary_suffix_non_numeric_values()
{
$this->shouldThrow(new \RuntimeException("binarySuffix converter accept only numeric values."))
->during('binarySuffix', array('as12'));
}
}
2 changes: 1 addition & 1 deletion spec/PHPHumanizer/StringSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class StringSpec extends ObjectBehavior
{
function it_humanize_strings()
function it_humanize_string()
{
$this->humanize('news_count')->shouldReturn('News count');
}
Expand Down
7 changes: 7 additions & 0 deletions src/PHPHumanizer/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPHumanizer;

use PHPHumanizer\Number\Ordinal;
use PHPHumanizer\String\BinarySuffix;

class Number
{
Expand All @@ -15,4 +16,10 @@ public static function oridinal($number)
{
return (string) new Ordinal($number);
}

public static function binarySuffix($number, $locale = 'en')
{
$binarySuffix = new BinarySuffix($number, $locale);
return $binarySuffix->convert();
}
}
64 changes: 64 additions & 0 deletions src/PHPHumanizer/String/BinarySuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace PHPHumanizer\String;

class BinarySuffix
{
const CONVERT_THRESHOLD = 1024;

/**
* @var int
*/
private $number;

/**
* @var string
*/
private $locale;

/**
* @var array
*/
private $binaryPrefixes = array(
1125899906842624 => '#.## PB',
1099511627776 => '#.## TB',
1073741824 => '#.## GB',
1048576 => '#.## MB',
1024 => '#.# kB',
0 => '# bytes'
);

/**
* @param $number
* @param string $locale
* @throws \RuntimeException
*/
public function __construct($number, $locale = 'en')
{
if (!is_numeric($number)) {
throw new \RuntimeException("binarySuffix converter accept only numeric values.");
}

$this->number = (int) $number;
$this->locale = $locale;
}

public function convert()
{
$formatter = new \NumberFormatter($this->locale, \NumberFormatter::PATTERN_DECIMAL);
if ($this->number < 0) {
return $this->number;
}

foreach ($this->binaryPrefixes as $size => $unitPattern) {
if ($size <= $this->number) {
$value = ($this->number >= self::CONVERT_THRESHOLD) ? $this->number / (double) $size : $this->number;
$formatter->setPattern($unitPattern);

return $formatter->format($value);
}
}

return $formatter->format($this->number);
}
}

0 comments on commit d7fd45f

Please sign in to comment.