Skip to content

Commit

Permalink
Fix: ImmutableArrayCollection now can be constructed and/or created
Browse files Browse the repository at this point in the history
Added: diffKeysAssoc and keys methods
  • Loading branch information
sauls committed Feb 18, 2018
1 parent ee3f922 commit 71020d8
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ $ composer require sauls/collections
Available collections

* ArrayCollection
* ImmutableArrayCollection
17 changes: 17 additions & 0 deletions src/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace Sauls\Component\Collection;

use function Sauls\Component\Helper\array_deep_search;
use function Sauls\Component\Helper\array_diff_key_assoc;
use function Sauls\Component\Helper\array_get_value;
use function Sauls\Component\Helper\array_key_assoc;
use function Sauls\Component\Helper\array_remove_key;
use function Sauls\Component\Helper\array_remove_value;
use function Sauls\Component\Helper\array_merge;
Expand Down Expand Up @@ -276,4 +278,19 @@ public function diffAssoc(array $elements, \Closure $function = null): Collectio

return $this->create($difference);
}

public function keys(): Collection
{
return $this->create(array_key_assoc($this->elements));
}

protected function assign(array $elements): void
{
$this->elements = $elements;
}

public function diffKeysAssoc(array $elements): Collection
{
return $this->create(array_diff_key_assoc($this->elements, $elements));
}
}
2 changes: 2 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function sort(\Closure $function = null): Collection;
public function sortKeys(\Closure $function = null): Collection;
public function diff(array $elements, \Closure $function = null): Collection;
public function diffKeys(array $elements, \Closure $function = null): Collection;
public function diffKeysAssoc(array $elements): Collection;
public function diffAssoc(array $elements, \Closure $function = null): Collection;
public function keys(): Collection;
public function map(\Closure $function);
public function keyOrValueExists($keyOrValue): bool;
public function keyOrValueDoesNotExists($keyOrValue): bool;
Expand Down
5 changes: 5 additions & 0 deletions src/ImmutableArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

class ImmutableArrayCollection extends ArrayCollection
{
public function __construct($elements = null)
{
$this->assign((new ArrayCollection($elements))->all());
}

/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
Expand Down
119 changes: 119 additions & 0 deletions tests/ArrayCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,36 @@ public function should_return_existence_of_key_or_value()
$this->assertFalse($arrayCollection->keyOrValueExists('key2.b'));
}

/**
* @test
*/
public function should_construct_immutable_array_collection(): void
{
$immutableArrayCollection = new ImmutableArrayCollection(['no' => 'change']);

$this->assertSame(
[
'no' => 'change',
],
$immutableArrayCollection->all()
);
}

/**
* @test
*/
public function should_create_immutable_array_collection(): void
{
$immutableArrayCollection = (new ImmutableArrayCollection())->create(['no' => 'change']);

$this->assertSame(
[
'no' => 'change',
],
$immutableArrayCollection->all()
);
}

/**
* @test
*/
Expand Down Expand Up @@ -874,4 +904,93 @@ function($a, $b) {
)->all()
);
}

/**
* @test
*/
public function should_return_flat_array_keys()
{
$arrayCollection = new ArrayCollection([
'a' => 1,
'b' => 2,
]);

$this->assertSame(
[
0 => 'a',
1 => 'b',
],
$arrayCollection->keys()->all()
);
}

/**
* @test
*/
public function should_return_assoc_array_keys()
{
$arrayCollection = new ArrayCollection([
'a' => 1,
'c' => [
'd' => 11
],
'g' => 2,
'x' => [
'y' => [
'z' => [
'yes' => 11
]
]
]
]);

$this->assertSame(
[
0 => 'a',
1 => 'c.d',
2 => 'g',
3 => 'x.y.z.yes',
],
$arrayCollection->keys()->all()
);
}

/**
* @test
*/
public function should_diff_keys_with_associative_arrays(): void
{
$array1 = [
'a' => 1,
'b' => 2,
'c' => [
'd' => 13
],
'x' => [
'y' => [
'z' => 11,
]
]
];
$arrayCollection = new ArrayCollection($array1);
$array2 = [
'b' => 4,
'c' => [
'd' =>11,
]
];


$this->assertSame(
[
'a' => 1,
'x' => [
'y' => [
'z' => 11,
]
],
],
$arrayCollection->diffKeysAssoc($array2)->all()
);
}
}

0 comments on commit 71020d8

Please sign in to comment.