Skip to content

Commit

Permalink
Adding UrlSearchParams::uniqueKeyCount method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 28, 2024
1 parent f91515d commit da63908
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All Notable changes to `League\Uri\Components` will be documented in this file

## [Next](https://github.com/thephpleague/uri-components/compare/7.4.1...master) - TBD

### Added

- `UrlSearchParams::uniqueKeyCount`

### Fixed

- None

### Deprecated

- None

### Removed

- None

## [7.4.1](https://github.com/thephpleague/uri-components/compare/7.4.0...7.4.1) - 2024-02-23

### Added
Expand Down
3 changes: 1 addition & 2 deletions Components/HierarchicalPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public static function fromAbsolute(string ...$segments): self
*/
private static function fromSegments(int $pathType, array $segments): self
{
$pathSegments = array_map(fn (Stringable|string $segment): string => (string) $segment, $segments);
$path = implode(self::SEPARATOR, $pathSegments);
$path = implode(self::SEPARATOR, $segments);

return match (true) {
self::IS_RELATIVE === $pathType => new self(ltrim($path, self::SEPARATOR)),
Expand Down
12 changes: 12 additions & 0 deletions Components/URLSearchParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ public function isEmpty(): bool
return 0 === $this->size();
}

/**
* Returns the total number of distinct search parameter keys.
*/
public function uniqueKeyCount(): int
{
return count(
array_count_values(
array_column([...$this->pairs], 0)
)
);
}

/**
* Returns the total number of search parameter entries.
*/
Expand Down
5 changes: 5 additions & 0 deletions Components/URLSearchParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,18 +630,22 @@ public function testSizeWithDeletionMethodAndBehaviour(): void
self::assertCount(3, $params);
self::assertTrue($params->isNotEmpty());
self::assertFalse($params->isEmpty());
self::assertSame(2, $params->uniqueKeyCount());

$params->delete('a');
self::assertCount(1, $params);
self::assertSame(1, $params->uniqueKeyCount());
}

public function testSizeWithAdditionMethodAndBehaviour(): void
{
$params = new URLSearchParams('a=1&b=2&a=3');
self::assertCount(3, $params);
self::assertSame(2, $params->uniqueKeyCount());

$params->append('b', '4');
self::assertCount(4, $params);
self::assertSame(2, $params->uniqueKeyCount());
}

public function testSizeWithEmptyInstance(): void
Expand All @@ -650,6 +654,7 @@ public function testSizeWithEmptyInstance(): void
self::assertCount(0, $params);
self::assertFalse($params->isNotEmpty());
self::assertTrue($params->isEmpty());
self::assertSame(0, $params->uniqueKeyCount());
}

public function testBasicHasMethod(): void
Expand Down

0 comments on commit da63908

Please sign in to comment.