diff --git a/README.md b/README.md index 7507212..a040c7e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ Sclable Array Functions ======================= -[![Build Status](https://travis-ci.org/sclable/array-functions.svg?branch=master)](https://travis-ci.org/sclable/array-functions) +[![Build Status](https://img.shields.io/travis/m-bymike/array-functions/master.svg?style=flat-square)](https://travis-ci.org/m-bymike/array-functions) +[![Latest Stable Version](https://img.shields.io/packagist/v/m-bymike/array-functions.svg?style=flat-square)](https://gemnasium.com/m-bymike/array-functions) An array wrapper to normalize php array functions, give them an ObjectOriented and functional programming approach. diff --git a/composer.json b/composer.json index b3fbfb9..6b77a30 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "sclable/array-functions", + "name": "m-bymike/array-functions", "license": "MIT", "description": "An array wrapper to normalize php array functions, give them an ObjectOriented and functional programming approach.", "authors": [ @@ -9,8 +9,8 @@ } ], "require-dev": { - "phpunit/phpunit": "^4", - "sclable/coding-standards": "0.1.0.2" + "phpunit/phpunit": "^4|^5", + "sclable/coding-standards": "^0.1.0.2" }, "autoload": { "psr-4": { diff --git a/examples/assignById.php b/examples/assignById.php new file mode 100644 index 0000000..93e4ff9 --- /dev/null +++ b/examples/assignById.php @@ -0,0 +1,53 @@ +id] = $instance; + } + $end = microtime(true); + $foreach->push($end - $start); + + // map + $start = microtime(true); + $assigned2 = array_combine(array_map(function ($instance) { + return $instance->id; + }, $instances), $instances); + $end = microtime(true); + $map->push($end - $start); + + // ArrayWrap map + $start = microtime(true); + $assigned3 = array_combine(\sclable\arrayFunctions\ArrayWrap::create($instances)->map(function ($instance) { + return $instance->id; + })->getRaw(), $instances); + $end = microtime(true); + $wrapMap->push($end - $start); +} + +echo 'foreach: ' . printResult($foreach) . PHP_EOL; +echo 'map: ' . printResult($map) . PHP_EOL; +echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL; + +function printResult(\sclable\arrayFunctions\ArrayWrap $results) +{ + return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; +} \ No newline at end of file diff --git a/examples/data/listOfInstances.php b/examples/data/listOfInstances.php new file mode 100644 index 0000000..dc5be37 --- /dev/null +++ b/examples/data/listOfInstances.php @@ -0,0 +1,18 @@ +map(function ($id) { + return (object) [ + 'id' => $id, + 'name' => 'name - ' . $id, + ]; +})->getRaw(); \ No newline at end of file diff --git a/examples/extractIds.php b/examples/extractIds.php new file mode 100644 index 0000000..f07d860 --- /dev/null +++ b/examples/extractIds.php @@ -0,0 +1,66 @@ +id; + } + $end = microtime(true); + $foreach->push($end - $start); + + // map + $start = microtime(true); + $assigned2 = array_map(function ($instance) { + return $instance->id; + }, $instances); + $end = microtime(true); + $mapMethod->push($end - $start); + + // method call + $start = microtime(true); + $assigned2 = array_map('extractId', $instances); + $end = microtime(true); + $map->push($end - $start); + + // ArrayWrap map + $start = microtime(true); + $assigned3 = \sclable\arrayFunctions\ArrayWrap::create($instances)->map(function ($instance) { + return $instance->id; + }); + $end = microtime(true); + $wrapMap->push($end - $start); +} + +echo 'foreach: ' . printResult($foreach) . PHP_EOL; +echo 'map: ' . printResult($map) . PHP_EOL; +echo 'map method: ' . printResult($mapMethod) . PHP_EOL; +echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL; + +function printResult(\sclable\arrayFunctions\ArrayWrap $results) +{ + return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; +} + +function extractId($instance) +{ + return $instance->id; +} \ No newline at end of file diff --git a/examples/htmlEntites.php b/examples/htmlEntites.php new file mode 100644 index 0000000..43cb8f8 --- /dev/null +++ b/examples/htmlEntites.php @@ -0,0 +1,53 @@ +push($end - $start); + + // map + $wTextList = $textList; + $start = microtime(true); + $wTextList = array_map('htmlentities', $wTextList); + $end = microtime(true); + $map->push($end - $start); + + // ArrayWrap map + $mTextList = $textList; + $start = microtime(true); + $mTextList = \sclable\arrayFunctions\ArrayWrap::create($mTextList)->apply('htmlentities'); + $end = microtime(true); + $wrapMap->push($end - $start); +} + +echo 'foreach: ' . printResult($foreach) . PHP_EOL; +echo 'map: ' . printResult($map) . PHP_EOL; +echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL; + +function printResult(\sclable\arrayFunctions\ArrayWrap $results) +{ + return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; +} \ No newline at end of file diff --git a/examples/max.php b/examples/max.php new file mode 100644 index 0000000..eb24947 --- /dev/null +++ b/examples/max.php @@ -0,0 +1,36 @@ +shuffle()->getRaw(); + + + +// old +$numbers = array_pad([], 10, 0); +foreach ($numbers as &$number) { + $number = rand(); +} + +$max = null; +foreach ($numbers as $number) { + $max = $max === null ? $number : max($max, $number); +} + +echo $max . PHP_EOL; + +// fp +echo ArrayWrap::create([]) + ->pad(10, 0) + ->map(function () { return rand(); }) + ->reduce('max'); \ No newline at end of file diff --git a/examples/sum.php b/examples/sum.php new file mode 100644 index 0000000..51eccd5 --- /dev/null +++ b/examples/sum.php @@ -0,0 +1,57 @@ +shuffle()->getRaw(); + + +$foreach = ArrayWrap::create([]); +$map = ArrayWrap::create([]); +$wrapSum = ArrayWrap::create([]); + +for ($i = 0; $i < 100; $i++) { + // foreach + $start = microtime(true); + $max = null; + foreach ($numbers as $number) { + if ($max === null) { + $max = $number; + continue; + } + + $max = max($max, $number); + } + $end = microtime(true); + $foreach->push($end - $start); + + // map + $start = microtime(true); + $max = array_reduce($numbers, 'max', null); + $end = microtime(true); + $map->push($end - $start); + + // ArrayWrap map + $start = microtime(true); + $max = ArrayWrap::create($numbers)->max(); + $end = microtime(true); + $wrapSum->push($end - $start); +} + +echo 'foreach: ' . printResult($foreach) . PHP_EOL; +echo 'map: ' . printResult($map) . PHP_EOL; +echo 'wrapMap: ' . printResult($wrapSum) . PHP_EOL; + +function printResult(ArrayWrap $results) +{ + return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; +} \ No newline at end of file