From 06c0bc3383415e6e27e5daa0098ce2cdf2a75758 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Tue, 28 May 2024 11:19:34 +0200 Subject: [PATCH] Adding UrlSearchParams::uniqueKeyCount method --- CHANGELOG.md | 18 ++++++++++++++++++ Components/HierarchicalPath.php | 3 +-- Components/URLSearchParams.php | 12 ++++++++++++ Components/URLSearchParamsTest.php | 5 +++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee703edeb..79ebe60d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Components/HierarchicalPath.php b/Components/HierarchicalPath.php index 820dfc504..b915e176e 100644 --- a/Components/HierarchicalPath.php +++ b/Components/HierarchicalPath.php @@ -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)), diff --git a/Components/URLSearchParams.php b/Components/URLSearchParams.php index 5e062295d..706ad2796 100644 --- a/Components/URLSearchParams.php +++ b/Components/URLSearchParams.php @@ -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. */ diff --git a/Components/URLSearchParamsTest.php b/Components/URLSearchParamsTest.php index f20e83999..e9994e728 100644 --- a/Components/URLSearchParamsTest.php +++ b/Components/URLSearchParamsTest.php @@ -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 @@ -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