-
Notifications
You must be signed in to change notification settings - Fork 0
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
fe18263
commit bc51988
Showing
9 changed files
with
260 additions
and
1 deletion.
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,2 @@ | ||
composer.lock | ||
vendor |
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 @@ | ||
preset: laravel |
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,14 @@ | ||
language: php | ||
|
||
php: | ||
- 7.2 | ||
- 7.3 | ||
|
||
before_script: | ||
- travis_retry composer install --no-interaction | ||
|
||
script: | ||
- vendor/bin/phpunit --coverage-clover clover.xml | ||
|
||
after_script: | ||
- bash <(curl -s https://codecov.io/bash) |
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 |
---|---|---|
@@ -1 +1,33 @@ | ||
# one-loop | ||
# One Loop | ||
|
||
[![Latest Version on Packagist](https://img.shields.io/packagist/v/hassan/one-loop.svg?style=flat-square)](https://packagist.org/packages/hassan/one-loop) | ||
[![Build Status](https://badgen.net/travis/dhassanali/one-loop/master)](https://travis-ci.org/dhassanali/one-loop) | ||
[![License](https://badgen.net/packagist/license/hassan/one-loop)](https://packagist.org/packages/hassan/one-loop) | ||
|
||
A Laravel/PHP Package for Minimizing Collection/Array Iterations | ||
|
||
## Installation | ||
|
||
Install the package via composer: | ||
|
||
```bash | ||
composer require hassan/one-loop | ||
``` | ||
|
||
## Usage | ||
|
||
``` php | ||
$users = App\User::all(); | ||
|
||
$ids = one_loop($users)->reject(static function ($user) { | ||
return $user->age < 20; | ||
}) | ||
->map(static function ($user) { | ||
return $user->id; | ||
}) | ||
->apply(); | ||
``` | ||
|
||
### Security | ||
|
||
If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
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,47 @@ | ||
{ | ||
"name": "hassan/one-loop", | ||
"description": "A Laravel/PHP Package for Minimizing Collection/Array Iterations", | ||
"keywords": [ | ||
"hassan", | ||
"one-loop", | ||
"collect", | ||
"collection", | ||
"laravel-collection" | ||
], | ||
"homepage": "https://github.com/dhassanali/one-loop", | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Hassan Ali", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.1", | ||
"illuminate/support": "5.8.*", | ||
"illuminate/contracts": "5.8.*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.0" | ||
}, | ||
"autoload": { | ||
"files": [ | ||
"src/helpers.php" | ||
], | ||
"psr-4": { | ||
"Hassan\\OneLoop\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Hassan\\OneLoop\\Tests\\": "tests" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "vendor/bin/phpunit" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,78 @@ | ||
<?php | ||
|
||
namespace Hassan\OneLoop; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class OneLoop | ||
{ | ||
private $items; | ||
|
||
private $applicableMethods = []; | ||
|
||
protected static $availableMethods = ['map', 'reject']; | ||
|
||
public function __construct($items) | ||
{ | ||
$this->items = $this->getArrayableItems($items); | ||
} | ||
|
||
public function apply() : array | ||
{ | ||
foreach ($this->items as $key => $item) { | ||
$this->applyMethods($item, $key); | ||
} | ||
|
||
return $this->items; | ||
} | ||
|
||
private function applyMethods($item, $index) : void | ||
{ | ||
foreach ($this->applicableMethods as $key => $method) { | ||
|
||
if (!isset($this->items[$index])) { | ||
continue; | ||
} | ||
|
||
switch ($key) { | ||
case 'map': | ||
$this->items[$index] = $method($item, $index); | ||
break; | ||
|
||
case 'reject': | ||
if ((bool) $method($item, $index)) { | ||
unset($this->items[$index]); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
protected function getArrayableItems($items) | ||
{ | ||
if (is_array($items)) { | ||
return $items; | ||
} | ||
|
||
if ($items instanceof Collection) { | ||
return $items->all(); | ||
} | ||
|
||
return (array) $items; | ||
} | ||
|
||
public function __call($name, $arguments) | ||
{ | ||
if ( | ||
isset($arguments[0]) && | ||
in_array($name, static::$availableMethods, true) && | ||
is_callable($arguments[0]) | ||
) { | ||
$this->applicableMethods[$name] = $arguments[0]; | ||
|
||
return $this; | ||
} | ||
|
||
return $this->$name(...$arguments); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
if (!function_exists('one_loop')) { | ||
/** | ||
* @param \Illuminate\Support\Collection|array $items | ||
* @return mixed | ||
*/ | ||
function one_loop($items) | ||
{ | ||
return new \Hassan\OneLoop\OneLoop($items); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace Hassan\OneLoop\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class OneLoopTest extends TestCase | ||
{ | ||
public function test_objects() | ||
{ | ||
$users = collect($this->getUsers()); | ||
|
||
$ids = one_loop($users) | ||
->reject(static function ($user) { | ||
return $user->age < 20; | ||
})->map(static function ($user) { | ||
return $user->id; | ||
})->apply(); | ||
|
||
$this->assertCount(2, $ids); | ||
} | ||
|
||
private function getUsers() | ||
{ | ||
$user1 = new \stdClass(); | ||
$user1->id = 1; | ||
$user1->age = 32; | ||
|
||
$user2 = new \stdClass(); | ||
$user2->id = 2; | ||
$user2->age = 2; | ||
|
||
$user3 = new \stdClass(); | ||
$user3->id = 3; | ||
$user3->age = 42; | ||
|
||
return [$user1, $user2, $user3]; | ||
} | ||
|
||
public function test_numbers() | ||
{ | ||
$numbers = one_loop([1, 2, 3]) | ||
->reject(static function ($item) { | ||
return $item === 2; | ||
})->map(static function ($item) { | ||
return $item * 3; | ||
})->apply(); | ||
|
||
$this->assertCount(2, $numbers); | ||
} | ||
} |