Skip to content
Merged

1.x #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Minimum Requirement
- PHP 7.2 +
- Composer

- For using `collect()` method, requires `illuminate\support` >= 5.5
> Additionally on Laravel App, if `arrayed.php`'s config file doesn't get added automatically then run `php artisan vendor:publish --tag=arrayed` after installation.

## Quick Usage

```php
Expand Down Expand Up @@ -261,6 +264,9 @@ Arrayed::diff(array $array2, array ...$_): ArrayedInterface;

Arrayed::reverse(bool $preserve_keys = false): ArrayedInterface;

Arrayed::diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface;

Arrayed::diffKey(array $array2, array ...$_): ArrayedInterface;

```

Expand Down
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@
"psr-4": {
"Transprime\\Arrayed\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"Transprime\\Arrayed\\Providers\\ArrayedServiceProvider"
]
}
}
}
15 changes: 10 additions & 5 deletions src/Arrayed.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ class Arrayed implements ArrayedInterface
private $result;

public function __construct(...$values)
{
$this->raw = $this->argumentsToArray(...$values);

$this->setResult(new Undefined());
}

private function argumentsToArray(...$values)
{
if (func_num_args() === 1 && is_array($values[0])) {
$this->raw = $values[0];
} else {
$this->raw = $values;
return $values[0];
}

$this->setResult(new Undefined());
return $values;
}

public static function on(...$values): ArrayedInterface
Expand Down Expand Up @@ -237,7 +242,7 @@ public function collect(...$with)
$collectionClass = $this->getConfig('collection_class');

if ($collectionClass && class_exists($collectionClass)) {
return new $collectionClass($this->copy()->merge($with)->result());
return new $collectionClass($this->copy()->merge($this->argumentsToArray(...$with))->result());
}

throw new ArrayedException('Collection class is not set or does not exist');
Expand Down
4 changes: 3 additions & 1 deletion src/Interfaces/ArrayedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ArrayAccess;
use JsonSerializable;
use IteratorAggregate;
use Transprime\Arrayed\Exceptions\ArrayedException;

interface ArrayedInterface extends ArrayAccess, Countable, IteratorAggregate, JsonSerializable
{
Expand Down Expand Up @@ -88,6 +87,9 @@ public function diff(array $array2, array ...$_): ArrayedInterface;

public function reverse(bool $preserve_keys = false): ArrayedInterface;

public function diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface;

public function diffKey(array $array2, array ...$_): ArrayedInterface;

/**
* Like php array_key_exists, this instead search if (one or more) keys exists in the array
Expand Down
29 changes: 29 additions & 0 deletions src/Providers/ArrayedServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Transprime\Arrayed\Providers;

use Illuminate\Support\ServiceProvider;
use Transprime\Arrayed\Arrayed;
use Transprime\Arrayed\Interfaces\ArrayedInterface;

class ArrayedServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../../config/arrayed.php' => config_path('arrayed.php'),
], 'arrayed');

$this->app->bind(ArrayedInterface::class, Arrayed::class);
}

public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../../config/arrayed.php', 'arrayed');
}
}
18 changes: 18 additions & 0 deletions src/Traits/ArrayPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
use Transprime\Arrayed\Exceptions\ArrayedException;
use Transprime\Arrayed\Interfaces\ArrayedInterface;

/**
* Trait ArrayPrefix
* @package Transprime\Arrayed\Traits
*
* @method self combine(array $values)
* @method mixed shift()
* @method self slice(int $offset, int $length = null, bool $preserve_keys = false)
*/
trait ArrayPrefix
{
public function changeKeyCase(int $case = null): ArrayedInterface
Expand Down Expand Up @@ -42,6 +50,16 @@ public function reverse(bool $preserve_keys = false): ArrayedInterface
return $this->setResult(array_reverse($this->getWorkableItem(), $preserve_keys));
}

public function diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface
{
return $this->setResult(array_diff_uassoc($this->getWorkableItem(), $array2, ...$_, ...[$key_compare_func]));
}

public function diffKey(array $array2, array ...$_): ArrayedInterface
{
return $this->setResult(array_diff_key($this->getWorkableItem(), $array2, ...$_));
}

/**
* Like php array_key_exists, this instead search if (one or more) keys exists in the array
*
Expand Down
40 changes: 39 additions & 1 deletion tests/ArrayPrefixTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testChangeKeyCase()
public function testChunk()
{
$this->assertEquals(
[[1,2], [3,4]],
[[1, 2], [3, 4]],
arrayed(1, 2, 3, 4)->chunk(2)->result()
);
}
Expand Down Expand Up @@ -77,6 +77,44 @@ public function testReverse()
);
}

public function testDiffUassoc()
{
$first = ['a' => 'b', 'c' => 'd'];
$second = ['2' => 'b', 'c' => 'd'];
$third = ['2' => 'b', 'k' => 'd'];
$callback = function ($first, $second) {
return $first === $second;
};

$this->assertEquals(
array_diff_uassoc($first, $second, $callback),
arrayed($first)->diffUassoc($callback, $second)->result()
);

$this->assertEquals(
array_diff_uassoc($first, $second, $third, $callback),
arrayed($first)->diffUassoc($callback, $second, $third)->result()
);
}

public function testDiffKey()
{
$first = ['a' => 'b', 'c' => 'd'];
$second = ['2' => 'b', 'c' => 'd'];
$third = ['2' => 'b', 'k' => 'd', 'm' => 'a'];
$fourth = ['m' => 'b', 'h' => 'd', 's' => 'a'];

$this->assertEquals(
array_diff_key($first, $second),
arrayed($first)->diffKey($second)->result()
);

$this->assertEquals(
array_diff_key($first, $second, $third, $fourth),
arrayed($first)->diffKey($second, $third, $fourth)->result()
);
}

public function testUnImplementedArrayPrefixFunction()
{
// array_combine
Expand Down