Skip to content

Commit

Permalink
Add a method to test if a Collection(Trait) has an object via php's i…
Browse files Browse the repository at this point in the history
…n_array function, unit test it and update the docs.
  • Loading branch information
tdashton committed Jul 11, 2024
1 parent 064ce6e commit da36eef
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/collection-trait.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ A fluent version of `append`. To do stuff like:
$collection->add($item1)->add($item2);
```

## has

```php
public function has($value, $strict): bool;
```

This method will tell you if your collection contains the given value. Please note that this method encapsulates the php [in_array()](https://www.php.net/manual/en/function.in-array.php) method and as such can produce unexpected results when using loose checking.

## unique

```php
Expand Down
5 changes: 5 additions & 0 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function add($value): self|static
return $this;
}

public function has(mixed $value, bool $strict = true): bool
{
return in_array($value, $this->toArray(), $strict);
}

public function unique(): self|static
{
return static::fromIterable(array_unique($this->toArray(), SORT_REGULAR));
Expand Down
103 changes: 103 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ArrayIterator;
use Exception;
use Generator;
use Kununu\Collection\Tests\Stub\AbstractItemStub;
use Kununu\Collection\Tests\Stub\AutoSortedCollectionStub;
use Kununu\Collection\Tests\Stub\CollectionStub;
use Kununu\Collection\Tests\Stub\ToArrayStub;
Expand Down Expand Up @@ -96,6 +97,108 @@ public static function fromIterableDataProvider(): array
];
}

#[DataProvider('hasDataProvider')]
public function testHas(iterable $collectionContents, mixed $hasValue, bool $hasStrict, bool $expectedHas): void
{
$collection = CollectionStub::fromIterable($collectionContents);

$this->assertEquals($expectedHas, $collection->has($hasValue, $hasStrict));
}

public static function hasDataProvider(): array
{
return [
'has_with_integers_strict' => [
[1, 2, 3],
1,
true,
true,
],
'has_with_integers_loose' => [
[1, 2, 3],
'1',
false,
true,
],
'missing_with_integers_strict' => [
[1, 2, 3],
4,
true,
false,
],
'missing_with_integers_strict_string_to_int' => [
[1, 2, 3],
'1',
true,
false,
],
'has_with_strings_strict' => [
['one', 'two', 'three'],
'one',
true,
true,
],
'missing_with_strings_strict' => [
['one', 'two', 'three'],
'missing',
true,
false,
],
'has_with_strings_loose' => [
['one', 'two', 'three'],
'one',
false,
true,
],
'missing_with_strings_loose' => [
['one', 'two', 'three'],
'missing',
false,
false,
],
'has_with_object_strict' => [
[
$one = new AbstractItemStub(['name' => 'one']),
new AbstractItemStub(['name' => 'two']),
new AbstractItemStub(['name' => 'three']),
],
$one,
true,
true,
],
'missing_with_object_strict' => [
[
new AbstractItemStub(['name' => 'one']),
new AbstractItemStub(['name' => 'two']),
new AbstractItemStub(['name' => 'three']),
],
new AbstractItemStub(['name' => 'one']),
true,
false,
],
'has_with_object_loose' => [
[
new AbstractItemStub(['name' => 'one']),
new AbstractItemStub(['name' => 'two']),
new AbstractItemStub(['name' => 'three']),
],
new AbstractItemStub(['name' => 'one']),
false,
true,
],
'missing_with_object_loose' => [
[
new AbstractItemStub(['name' => 'one']),
new AbstractItemStub(['name' => 'two']),
new AbstractItemStub(['name' => 'three']),
],
new AbstractItemStub(['name' => 'vier']),
false,
false,
],
];
}

public function testEmpty(): void
{
$collection = new CollectionStub();
Expand Down

0 comments on commit da36eef

Please sign in to comment.