Skip to content

Commit

Permalink
do not report
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 11, 2024
1 parent 3dae695 commit 3e633c2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/ReferrerSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ public function add(ReferrerSource $source): static
return $this;
}

/**
* @param class-string<ReferrerSource<mixed>> $source
* @param ReferrerSource<mixed>[] $sources
* @return $this
*/
public function set(string $source, array $sources): static
{
$this->items[$source] = $sources;

return $this;
}

/**
* @param ReferrerSource<mixed> $source
*/
Expand All @@ -207,12 +219,22 @@ public function put(
}

/**
* @param class-string<ReferrerSource<mixed>> $source
* @param class-string<ReferrerSource<mixed>>|ReferrerSource<mixed> $source
* @return $this
*/
public function forget(string $source): static
public function forget(string|ReferrerSource $source): static
{
unset($this->items[$source]);
if ($source instanceof ReferrerSource) {
$this->set(
$source::class,
array_filter(
$this->get($source::class),
fn ($value) => ! $source->is($value)
)
);
} else {
unset($this->items[$source]);
}

return $this;
}
Expand Down Expand Up @@ -278,7 +300,10 @@ public static function fromArray(array $sources): static
foreach ($sources as $source => $values) {

foreach ($values as $value) {
rescue(fn () => $items->add($source::fromArray($value)));
rescue(
fn () => $items->add($source::fromArray($value)),
report: false
);
}

}
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/ReferrerSourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,40 @@

expect($sources->count())->toBe(1);
});

it('forget a specific source from the items', function () {
$sources = new ReferrerSources;

$item = new UtmReferrerSource(
utm_source: 'google'
);

$sources
->put(
source: new RequestHeaderSource(
referer: 'foo'
),
strategy: Strategy::All
)
->put(
source: $item,
strategy: Strategy::All
)
->put(
source: new UtmReferrerSource(
utm_source: 'fake'
),
strategy: Strategy::All
);

expect($sources->count())->toBe(2);
expect($sources->get($item::class))->toHaveLength(2);
expect($sources->has($item))->toBe(true);

$sources->forget($item);

expect($sources->count())->toBe(2);
expect($sources->get($item::class))->toHaveLength(1);
expect($sources->has($item))->toBe(false);

});

0 comments on commit 3e633c2

Please sign in to comment.