Skip to content

Commit

Permalink
Add array unique
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasiliy-Makogon committed Oct 17, 2023
1 parent 5b9b1e2 commit 0d5ef91
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/CoverArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ final public function map(callable $callback): static
return new static(array_map($callback, $this->data));
}

/**
* Убирает повторяющиеся значения из массива
*
* @param int $flags
* @return $this
* @see array_unique
*/
final public function unique(int $flags = SORT_STRING): static
{
return new static(array_unique($this->data, $flags));
}

/**
* Проверяет, присутствует ли в массиве значение
*
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/CoverArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public function testSetDataMethod(): void
['PHP', 'MySql', 'C++', 'C#', 'Python', 'Ruby'],
$this->data->get('langs.backend')->getDataAsArray()
);

$this->assertEmpty((new NewTypeArray())->setData(null));
$this->assertEmpty((new NewTypeArray())->setData([]));
}

/**
* @see CoverArray::setData()
*/
public function testConstructorOnEmptyValue(): void
{
$this->assertEmpty(new NewTypeArray(null));
$this->assertEmpty(new NewTypeArray());
}

/**
Expand Down Expand Up @@ -349,6 +361,26 @@ public function testMapMethod(): void
);
}

/**
* @see CoverArray::unique()
*/
public function testUniqueMethod(): void
{
$this->data->get('langs.backend')->append('PHP');
$this->data->get('langs.backend')->append('PHP');
$this->data->get('langs.backend')->append('PHP');

$this->assertSame(
['PHP', 'MySql', 'PHP', 'PHP', 'PHP'],
$this->data->get('langs.backend')->getDataAsArray()
);

$this->assertSame(
['PHP', 'MySql'],
$this->data->get('langs.backend')->unique()->getDataAsArray()
);
}

/**
* @see CoverArray::in()
*/
Expand Down

0 comments on commit 0d5ef91

Please sign in to comment.