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 fb4ec62 commit 9e9aac7
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$header = <<<EOF
League.Uri (https://uri.thephpleague.com)
Expand All @@ -17,6 +19,7 @@
$config = new PhpCsFixer\Config();

return $config
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
Expand Down
18 changes: 18 additions & 0 deletions components/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/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/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/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
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
"ext-fileinfo": "*",
"ext-gmp": "*",
"ext-intl": "*",
"friendsofphp/php-cs-fixer": "^3.53.0",
"friendsofphp/php-cs-fixer": "^3.57.2",
"guzzlehttp/psr7": "^2.6.2",
"laminas/laminas-diactoros": "^3.3.1",
"nyholm/psr7": "^1.8.1",
"phpbench/phpbench": "^1.2.15",
"phpstan/phpstan": "^1.10.66",
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-phpunit": "^1.3.16",
"phpstan/phpstan-strict-rules": "^1.5.3",
"phpunit/phpunit": "^10.5.17 || ^11.1.1",
"psr/http-factory": "^1.0.2",
"phpstan/phpstan": "^1.11.2",
"phpstan/phpstan-deprecation-rules": "^1.2.0",
"phpstan/phpstan-phpunit": "^1.4.0",
"phpstan/phpstan-strict-rules": "^1.6.0",
"phpunit/phpunit": "^10.5.17 || ^11.1.3",
"psr/http-factory": "^1.1.0",
"psr/http-message": "^1.1.0 || ^2.0",
"symfony/var-dumper": "^6.4.6",
"symfony/var-dumper": "^6.4.7",
"uri-templates/uritemplate-test": "dev-master"
},
"repositories": [
Expand Down
2 changes: 2 additions & 0 deletions docs/components/7.0/urlsearchparams.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ While the class implements all the methods define in the RFC, the following meth

- `URLSearchParams::isEmpty`
- `URLSearchParams::isNotEmpty`
- `URLSearchParams::uniqueKeyCount` new in version `7.5.0`

~~~php
use League\Uri\Components\URLSearchParams;
Expand All @@ -104,6 +105,7 @@ $params->has('foo', 'bar'); //returns true
$params->has('foo', 'toto'); //returns false (the second parameter is the value of the pair)
count($params); //returns 3
$params->size(); //returns 3
$params->uniqueKeyCount(); //returns 2
$params->delete('foo');
count($params); //returns 1 (because all the pairs which contains foo have been removed)
$params->set('aha', true);
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ parameters:
path: interfaces/IPv4/NativeCalculator.php
- message: '#function gmp_(.*)? expects (GMP|int)#'
path: interfaces/IPv4/GMPCalculator.php
- identifier: missingType.iterableValue
reportUnmatchedIgnoredErrors: true
treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false

0 comments on commit 9e9aac7

Please sign in to comment.