diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ebc88f7..8d5039d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Chg #159: Replace `withNextPageToken()` and `withPreviousPageToken()` of `PaginatorInterface` with `withToken()`, `getNextPageToken()` with `getNextToken()`, `getPreviousPageToken()` with `getPreviousToken()`, and add `getToken()`. These methods use new `PageToken` class (@vjik) +- New #160: Add `Sort::hasFieldInConfig()` method that checks for the presence of a field in the sort config (@vjik) ## 1.0.1 January 25, 2023 diff --git a/src/Reader/Sort.php b/src/Reader/Sort.php index d71d79bf..ca5ed950 100644 --- a/src/Reader/Sort.php +++ b/src/Reader/Sort.php @@ -330,4 +330,14 @@ public function getCriteria(): array return $criteria; } + + /** + * @param string $name The field name. + * + * @return bool Whether the field is present in the config. + */ + public function hasFieldInConfig(string $name): bool + { + return isset($this->config[$name]); + } } diff --git a/tests/Reader/SortTest.php b/tests/Reader/SortTest.php index d680f6b7..8c9dcd2b 100644 --- a/tests/Reader/SortTest.php +++ b/tests/Reader/SortTest.php @@ -333,4 +333,13 @@ public function testPrepareConfig(array $expected, array $config): void $sort = Sort::only($config); $this->assertSame($expected, $sort->getCriteria()); } + + public function testHasFieldInConfig(): void + { + $sort = Sort::only(['a', 'b']); + + $this->assertTrue($sort->hasFieldInConfig('a')); + $this->assertTrue($sort->hasFieldInConfig('b')); + $this->assertFalse($sort->hasFieldInConfig('c')); + } }