Skip to content
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,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
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
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