-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65564e1
commit d7fd45f
Showing
7 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |