Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dhassanali committed Aug 13, 2019
1 parent fe18263 commit bc51988
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
14 changes: 14 additions & 0 deletions .travis.yml
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)
34 changes: 33 additions & 1 deletion README.md
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.
47 changes: 47 additions & 0 deletions composer.json
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
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
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>
78 changes: 78 additions & 0 deletions src/OneLoop.php
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);
}
}
12 changes: 12 additions & 0 deletions src/helpers.php
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);
}
}
51 changes: 51 additions & 0 deletions tests/OneLoopTest.php
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);
}
}

0 comments on commit bc51988

Please sign in to comment.