Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffroy-aubry committed Apr 1, 2013
1 parent 3d8eb2e commit 377d1a9
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.settings
/.buildpath
/.project
/build/
*~
/vendor/
composer.phar
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "geoffroy-aubry/tools",
"description": "bla bla",
"keywords": ["bla"],
"type": "library",
"license": "LGPL-3.0+",
"authors": [
{
"name": "Geoffroy Aubry",
"email": "gaubry@hi-media.com"
}
],
"require": {
"php": ">=5.3.3"
},
"autoload": {
"psr-0": {"GAubry\\Tools": "src/"}
}
}
47 changes: 47 additions & 0 deletions phpunit-dist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
syntaxCheck="true"
processIsolation="false"
colors="true"
strict="true"
verbose="true"
stopOnFailure="false"
timeoutForSmallTests="20"
timeoutForMediumTests="40"
timeoutForLargeTests="60"
>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix="Interface.php">src</directory>
</exclude>
</whitelist>
</filter>

<testsuites>
<testsuite name="GAubry/Tools">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<logging>
<log type="coverage-html"
target="build/coverage"
title="GAubry/Tools"
charset="UTF-8"
yui="true"
highlight="true"
lowUpperBound="35"
highLowerBound="70"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>
196 changes: 196 additions & 0 deletions src/GAubry/Tools/Tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php

namespace GAubry\Tools;

/**
* Outils divers et variés...
*/
class Tools
{
private function __construct()
{
}

/**
* Transforme un tableau multidimensionnel en un tableau à une seule dimension,
* en ramenant toutes les feuilles au premier niveau.
*
* @param array $array
* @return array tableau à une seule dimension.
* @see http://stackoverflow.com/a/1320156/1813519
*/
public static function flatten (array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}

/**
* Retourne la chaîne spécifiée, en l'encodant en UTF8 seulement si celle-ci ne l'était pas déjà.
*
* @param string $s
* @return string la chaîne spécifiée, en l'encodant en UTF8 seulement si celle-ci ne l'était pas déjà.
*/
public static function utf8_encode ($s)
{
return (utf8_encode(utf8_decode($s)) == $s ? $s : utf8_encode($s));
}

/**
* Exécute la commande shell spécifiée et retourne la sortie découpée par ligne dans un tableau.
* En cas d'erreur shell (code d'erreur <> 0), lance une exception incluant le message d'erreur.
*
* @param string $sCmd
* @return array tableau indexé du flux de sortie shell découpé par ligne
* @throws RuntimeException en cas d'erreur shell
*/
public static function exec ($sCmd, $sOutputPath='')
{
if (empty($sOutputPath)) {
$sFullCmd = '( ' . $sCmd . ' ) 2>&1';
} else {
$sFullCmd = "( $sCmd ) 1>$sOutputPath 2>&1 & echo $!";
}
exec($sFullCmd, $aResult, $iReturnCode);
if ($iReturnCode !== 0) {
throw new RuntimeException(
"Exit code not null: $iReturnCode. Result: '" . implode("\n", $aResult) . "'",
$iReturnCode
);
}
return $aResult;
}

public static function stripBashColors ($sMsg)
{
return preg_replace('/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/', '', $sMsg);
}

// Allow negative precision.
public static function round ($fValue, $iPrecision=0)
{
$sPrintfPrecision = max(0, $iPrecision);
return sprintf("%01.{$sPrintfPrecision}f", round($fValue, $iPrecision));
}

public static function ucwordWithDelimiters ($str, array $aDelimiters=array("'", '-')){
$sReturn = ucwords(strtolower($str));
foreach ($aDelimiters as $sDelimiter) {
if (strpos($sReturn, $sDelimiter) !== false) {
$sReturn = implode($sDelimiter, array_map('ucfirst', explode($sDelimiter, $sReturn)));
}
}
return $sReturn;
}

public static function intToSI ($iValue)
{
$prefixes = array(12 => 'T', 9 => 'G', 6 => 'M', 3 => 'k', 0 => '');

$m = 0;
foreach ($prefixes as $multiple => $s) {
if ($iValue >= pow(10, $multiple)) {
$m = $multiple;
break;
}
}

//$decimals = ($m > 0 && $val/pow(10, $m) < 100 ? 1 : 0);
$decimals = ($m === 0 ? 0 : 1);
return array(round($iValue / pow(10, $m), $decimals), $prefixes[$m]);
}

/**
* Format a number with grouped thousands.
* It is an extended version of number_format() that allow do not specify $decimals.
*
* @param float $fNumber The number being formatted.
* @param string $sDecPoint Sets the separator for the decimal point.
* @param string $sThousandsSep Sets the thousands separator. Only the first character of $thousands_sep is used.
* @param int $iDecimals Sets the number of decimal points.
* @return string A formatted version of $number.
*/
public static function numberFormat ($fNumber, $sDecPoint='.', $sThousandsSep=',', $iDecimals=NULL)
{
if ($iDecimals !== NULL) {
return number_format($fNumber, $iDecimals, $sDecPoint, $sThousandsSep);
} else {
$tmp = explode('.', $fNumber);
$out = number_format($tmp[0], 0, $sDecPoint, $sThousandsSep);
if (isset($tmp[1])) {
$out .= $sDecPoint.$tmp[1];
}
return $out;
}
}

/**
* Retourne un couple comprenant d'une part le nombre d'octets contenus dans la plus grande unité informatique
* inférieure à la taille spécifiée, et d'autre part le nom de cette unité.
*
* Par exemple, si $iFileSize vaut 2000, alors le résultat sera : array(1024, 'Kio').
*
* @param int $iFileSize taille en octets à changer d'unité
* @return array tableau (int, string) comprenant d'une part le nombre d'octets contenus dans la plus grande
* unité inférieure à la taille spécifiée, et d'autre part le nom de cette unité.
*/
public static function getFileSizeUnit ($iFileSize)
{
if ($iFileSize < 1024) {
$iUnit = 1;
$sUnit = 'o';
} else if ($iFileSize < 1024*1024) {
$iUnit = 1024;
$sUnit = 'Kio';
} else {
$iUnit = 1024*1024;
$sUnit = 'Mio';
}
return array($iUnit, $sUnit);
}

/**
* Retourne un couple comprenant d'une part la taille spécifiée arrondie,
* et d'autre part l'unité dans laquelle la taille a été arrondie.
*
* Le second paramètre, si <> de 0, permet de spécifier une taille de référence pour le calcul de l'unité.
*
* Par exemple :
* (100, 0) => ('100', 'o')
* (100, 2000000) => ('<1', 'Mio')
* (200, 0) => ('2', 'Kio')
*
* @param int $iSize taille à convertir
* @param int $iRefSize référentiel de conversion, si différent de 0
* @return array un couple comprenant d'une part la taille spécifiée arrondie,
* et d'autre part l'unité dans laquelle la taille a été arrondie.
*/
public static function convertFileSize2String ($iSize, $iRefSize=0)
{
if ($iRefSize === 0) {
$iRefSize = $iSize;
}
list($iUnit, $sUnit) = self::getFileSizeUnit($iRefSize);

$sFileSize = round($iSize/$iUnit);
if ($sFileSize == 0 && $iSize > 0) {
$sFileSize = '<1';
}
return array($sFileSize, $sUnit);
}

public static function strPutCSV ($input, $delimiter = ',', $enclosure = '"') {
// Open a memory "file" for read/write...
$fp = fopen('php://temp', 'r+');
// ... write the $input array to the "file" using fputcsv()...
fputcsv($fp, $input, $delimiter, $enclosure);
// ... rewind the "file" so we can read what we just wrote...
rewind($fp);
// ... read the entire line into a variable...
$data = fgets($fp);
// ... close the "file"...
fclose($fp);
// ... and return the $data to the caller, with the trailing newline from fgets() removed.
return rtrim( $data, "\n" );
}
}
68 changes: 68 additions & 0 deletions tests/GAubry/Tools/ToolsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace GAubry\Tools\Tests;

use \GAubry\Tools\Tools;

/**
* @category TwengaDeploy
* @package Tests
* @author Geoffroy AUBRY <geoffroy.aubry@twenga.com>
*/
class ToolsTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers \GAubry\Tools\Tools::getFileSizeUnit
* @dataProvider dataProvider_testGetFileSizeUnit
* @param int $iFileSize taille en octets à changer d'unité
* @param array $aExpected tableau (int, string) comprenant d'une part le nombre d'octets contenus dans la plus grande
* unité inférieure à la taille spécifiée, et d'autre part le nom de cette unité.
*/
public function testGetFileSizeUnit ($iFileSize, $aExpected)
{
$aResult = Tools::getFileSizeUnit($iFileSize);
$this->assertEquals($aExpected, $aResult);
}

/**
* Data provider pour testGetFileSizeUnit()
*/
public static function dataProvider_testGetFileSizeUnit ()
{
return array(
array(0, array(1, 'o')),
array(100, array(1, 'o')),
array(2000, array(1024, 'Kio')),
array(2000000, array(1024*1024, 'Mio')),
);
}

/**
* @covers \GAubry\Tools\Tools::convertFileSize2String
* @dataProvider dataProvider_testConvertFileSize2String
* @param int $iSize taille à convertir
* @param int $iRefSize référentiel de conversion, si différent de 0
* @param array $aExpected un couple comprenant d'une part la taille spécifiée arrondie,
* et d'autre part l'unité dans laquelle la taille a été arrondie.
*/
public function testConvertFileSize2String ($iSize, $iRefSize, $aExpected)
{
$aResult = Tools::convertFileSize2String($iSize, $iRefSize);
$this->assertEquals($aExpected, $aResult);
}

/**
* Data provider pour testConvertFileSize2String()
*/
public static function dataProvider_testConvertFileSize2String ()
{
return array(
array(0, 0, array('0', 'o')),
array(100, 0, array('100', 'o')),
array(100, 2000000, array('<1', 'Mio')),
array(2000, 0, array('2', 'Kio')),
array(2000000, 0, array('2', 'Mio')),
);
}
}
16 changes: 16 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

if ( ! file_exists(__DIR__ . '/../vendor/autoload.php')) {
echo "\033[1m\033[4;33m/!\\\033[0;37m "
. "You must set up the project dependencies, run the following commands:" . PHP_EOL
. " \033[0;33mcomposer install\033[0;37m or \033[0;33mphp composer.phar install\033[0;37m." . PHP_EOL
. PHP_EOL
. "If needed, to install \033[1;37mcomposer\033[0;37m locally: "
. "\033[0;37m\033[0;33mcurl -sS https://getcomposer.org/installer | php\033[0;37m" . PHP_EOL
. "Or check http://getcomposer.org/doc/00-intro.md#installation-nix for more information." . PHP_EOL
. PHP_EOL;
exit(1);
}

$oLoader = require __DIR__ . '/../vendor/autoload.php';
$oLoader->add('GAubry\Tools\Tests', __DIR__);

0 comments on commit 377d1a9

Please sign in to comment.