From 45127379502c5fa8f3ef52b72b6f3e21dffbfd42 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 5 Oct 2025 18:14:42 +0530 Subject: [PATCH 1/6] Refactor OpenSRS suggest method to properly handle limit parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Simplified service selection logic by always including all services - Fixed TLD filtering to apply across all services (suggestion, premium, lookup) - Implemented post-processing to enforce limit by prioritizing premium domains - Removed conditional service selection based on price filters - Improved code readability by reducing nested conditionals 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/Domains/Registrar/OpenSRS.php | 82 +++++++++++++++---------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/src/Domains/Registrar/OpenSRS.php b/src/Domains/Registrar/OpenSRS.php index 2c59695..5c40e1d 100644 --- a/src/Domains/Registrar/OpenSRS.php +++ b/src/Domains/Registrar/OpenSRS.php @@ -238,61 +238,35 @@ public function cancelPurchase(): bool public function suggest(array|string $query, array $tlds = [], int|null $limit = null, int|null $priceMax = null, int|null $priceMin = null): array { $query = is_array($query) ? $query : [$query]; - - // Determine which services to use based on parameters - $hasPriceFilter = $priceMax !== null || $priceMin !== null; - - if ($hasPriceFilter) { - $services = ['premium']; - } elseif ($limit) { - $services = ['lookup', 'suggestion']; - } else { - $services = ['suggestion', 'premium', 'lookup']; - } - $message = [ 'object' => 'DOMAIN', 'action' => 'name_suggest', 'attributes' => [ - 'services' => $services, + 'services' => ['suggestion', 'premium', 'lookup'], 'searchstring' => implode(' ', $query), 'skip_registry_lookup' => 1, ], ]; + $tlds = !empty($tlds) ? array_map(fn ($tld) => '.' . ltrim($tld, '.'), $tlds) : []; if (!empty($tlds)) { $message['attributes']['tlds'] = $tlds; + $message['attributes']['service_override']['premium']['tlds'] = $tlds; + $message['attributes']['service_override']['suggestion']['tlds'] = $tlds; + $message['attributes']['service_override']['lookup']['tlds'] = $tlds; } - if ($limit || $hasPriceFilter) { - $formattedTlds = !empty($tlds) ? array_map(fn ($tld) => '.' . ltrim($tld, '.'), $tlds) : []; - - if ($hasPriceFilter) { - $message['attributes']['service_override']['premium'] = []; - - if (!empty($formattedTlds)) { - $message['attributes']['service_override']['premium']['tlds'] = $formattedTlds; - } - - if ($limit) { - $message['attributes']['service_override']['premium']['maximum'] = $limit; - } - - if ($priceMin !== null) { - $message['attributes']['service_override']['premium']['price_min'] = $priceMin; - } + if ($limit) { + $message['attributes']['service_override']['premium']['maximum'] = $limit; + $message['attributes']['service_override']['suggestion']['maximum'] = $limit; + } - if ($priceMax !== null) { - $message['attributes']['service_override']['premium']['price_max'] = $priceMax; - } - } elseif ($limit) { - $message['attributes']['service_override']['suggestion']['maximum'] = $limit; + if ($priceMin !== null) { + $message['attributes']['service_override']['premium']['price_min'] = $priceMin; + } - if (!empty($formattedTlds)) { - $message['attributes']['service_override']['suggestion']['tlds'] = $formattedTlds; - $message['attributes']['service_override']['lookup']['tlds'] = $formattedTlds; - } - } + if ($priceMax !== null) { + $message['attributes']['service_override']['premium']['price_max'] = $priceMax; } $result = $this->send($message); @@ -339,9 +313,7 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = 'dt_array', 'item', ]); - $premiumElements = $result->xpath($premiumXpath); - foreach ($premiumElements as $element) { $item = $element->xpath('dt_assoc/item'); @@ -375,6 +347,32 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = } } + // If limit is specified, prioritize premium domains and limit total results + if ($limit && count($items) > $limit) { + $premiumDomains = []; + $suggestionDomains = []; + + foreach ($items as $domain => $data) { + if ($data['type'] === 'premium') { + $premiumDomains[$domain] = $data; + } else { + $suggestionDomains[$domain] = $data; + } + } + + $result = []; + $premiumCount = min(count($premiumDomains), $limit); + $suggestionCount = $limit - $premiumCount; + + $result = array_slice($premiumDomains, 0, $premiumCount, true); + if ($suggestionCount > 0) { + $suggestions = array_slice($suggestionDomains, 0, $suggestionCount, true); + $result = array_merge($result, $suggestions); + } + + return $result; + } + return $items; } From 69dabbffbdf372f5abc710ea040d4e21a6457b13 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 5 Oct 2025 18:21:27 +0530 Subject: [PATCH 2/6] handle price mismatch --- src/Domains/Registrar/OpenSRS.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Domains/Registrar/OpenSRS.php b/src/Domains/Registrar/OpenSRS.php index 5c40e1d..031a78a 100644 --- a/src/Domains/Registrar/OpenSRS.php +++ b/src/Domains/Registrar/OpenSRS.php @@ -237,6 +237,10 @@ public function cancelPurchase(): bool */ public function suggest(array|string $query, array $tlds = [], int|null $limit = null, int|null $priceMax = null, int|null $priceMin = null): array { + if ($priceMin !== null && $priceMax !== null && $priceMin >= $priceMax) { + throw new Exception("Invalid price range: priceMin ($priceMin) must be less than priceMax ($priceMax)."); + } + $query = is_array($query) ? $query : [$query]; $message = [ 'object' => 'DOMAIN', From 499bcc9e48280cca2f74fc3a8fd163ed9d56b232 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 5 Oct 2025 19:11:00 +0530 Subject: [PATCH 3/6] fix: sorting --- src/Domains/Registrar/Adapter.php | 7 +- src/Domains/Registrar/OpenSRS.php | 288 ++++++++++++++++++++---------- tests/OpenSRSTest.php | 103 +++++++++-- 3 files changed, 288 insertions(+), 110 deletions(-) diff --git a/src/Domains/Registrar/Adapter.php b/src/Domains/Registrar/Adapter.php index c8c99ce..060bab5 100644 --- a/src/Domains/Registrar/Adapter.php +++ b/src/Domains/Registrar/Adapter.php @@ -24,11 +24,12 @@ abstract public function purchase(string $domain, array $contacts, array $namese * @param array $query * @param array $tlds * @param int|null $limit - * @param int|null $priceMax - * @param int|null $priceMin + * @param string|null $filterType Filter results by type: 'premium', 'suggestion', or null for both + * @param int|null $premiumPriceMax + * @param int|null $premiumPriceMin * @return array */ - abstract public function suggest(array|string $query, array $tlds = [], int|null $limit = null, int|null $priceMax = null, int|null $priceMin = null): array; + abstract public function suggest(array|string $query, array $tlds = [], int|null $limit = null, string|null $filterType = null, int|null $premiumPriceMax = null, int|null $premiumPriceMin = null): array; /** * @return array diff --git a/src/Domains/Registrar/OpenSRS.php b/src/Domains/Registrar/OpenSRS.php index 031a78a..643390b 100644 --- a/src/Domains/Registrar/OpenSRS.php +++ b/src/Domains/Registrar/OpenSRS.php @@ -231,14 +231,28 @@ public function cancelPurchase(): bool * @param array|string $query Search terms to generate suggestions from * @param array $tlds Top-level domains to search within (e.g., ['com', 'net', 'org']) * @param int|null $limit Maximum number of results to return - * @param int|null $priceMax Maximum price for premium domains - * @param int|null $priceMin Minimum price for premium domains + * @param string|null $filterType Filter results by type: 'premium', 'suggestion', or null for both + * @param int|null $premiumPriceMax Maximum price for premium domains + * @param int|null $premiumPriceMin Minimum price for premium domains + * @param string|null $sortOrder Sort order for prices: 'asc', 'desc', or null for no sorting * @return array Domains with metadata: `available` (bool), `price` (float|null), `type` (string) */ - public function suggest(array|string $query, array $tlds = [], int|null $limit = null, int|null $priceMax = null, int|null $priceMin = null): array + public function suggest(array|string $query, array $tlds = [], int|null $limit = null, string|null $filterType = null, int|null $premiumPriceMax = null, int|null $premiumPriceMin = null, string|null $sortOrder = null): array { - if ($priceMin !== null && $priceMax !== null && $priceMin >= $priceMax) { - throw new Exception("Invalid price range: priceMin ($priceMin) must be less than priceMax ($priceMax)."); + if ($premiumPriceMin !== null && $premiumPriceMax !== null && $premiumPriceMin >= $premiumPriceMax) { + throw new Exception("Invalid price range: premiumPriceMin ($premiumPriceMin) must be less than premiumPriceMax ($premiumPriceMax)."); + } + + if ($filterType !== null && !in_array($filterType, ['premium', 'suggestion'])) { + throw new Exception("Invalid filter type: filterType ($filterType) must be 'premium' or 'suggestion'."); + } + + if ($filterType !== null && $filterType === 'suggestion' && ($premiumPriceMin !== null || $premiumPriceMax !== null)) { + throw new Exception("Invalid price range: premiumPriceMin ($premiumPriceMin) and premiumPriceMax ($premiumPriceMax) cannot be set when filterType is 'suggestion'."); + } + + if ($sortOrder !== null && !in_array($sortOrder, ['asc', 'desc'])) { + throw new Exception("Invalid sort order: sortOrder ($sortOrder) must be 'asc' or 'desc'."); } $query = is_array($query) ? $query : [$query]; @@ -259,18 +273,15 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = $message['attributes']['service_override']['suggestion']['tlds'] = $tlds; $message['attributes']['service_override']['lookup']['tlds'] = $tlds; } - if ($limit) { $message['attributes']['service_override']['premium']['maximum'] = $limit; $message['attributes']['service_override']['suggestion']['maximum'] = $limit; } - - if ($priceMin !== null) { - $message['attributes']['service_override']['premium']['price_min'] = $priceMin; + if ($premiumPriceMin !== null) { + $message['attributes']['service_override']['premium']['price_min'] = $premiumPriceMin; } - - if ($priceMax !== null) { - $message['attributes']['service_override']['premium']['price_max'] = $priceMax; + if ($premiumPriceMax !== null) { + $message['attributes']['service_override']['premium']['price_max'] = $premiumPriceMax; } $result = $this->send($message); @@ -278,108 +289,193 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = $items = []; - $suggestionXpath = implode('/', [ - '//body', - 'data_block', - 'dt_assoc', - 'item[@key="attributes"]', - 'dt_assoc', - 'item[@key="suggestion"]', - 'dt_assoc', - 'item[@key="items"]', - 'dt_array', - 'item', - ]); - $suggestionElements = $result->xpath($suggestionXpath); - foreach ($suggestionElements as $element) { - $domainNode = $element->xpath('dt_assoc/item[@key="domain"]'); - $statusNode = $element->xpath('dt_assoc/item[@key="status"] | dt_assoc/item[@key="availability"]'); - $domain = isset($domainNode[0]) ? (string) $domainNode[0] : null; - $status = isset($statusNode[0]) ? strtolower((string) $statusNode[0]) : ''; - if ($domain) { - $items[$domain] = [ - 'available' => in_array($status, ['available', 'true', '1'], true), - 'price' => null, - 'type' => 'suggestion' - ]; - } - } + // Process suggestion domains + if ($filterType === 'suggestion' || $filterType === null) { + $suggestionXpath = implode('/', [ + '//body', + 'data_block', + 'dt_assoc', + 'item[@key="attributes"]', + 'dt_assoc', + 'item[@key="suggestion"]', + 'dt_assoc', + 'item[@key="items"]', + 'dt_array', + 'item', + ]); + $suggestionElements = $result->xpath($suggestionXpath); + + $processedCount = 0; + $suggestionLimit = $limit; + + foreach ($suggestionElements as $element) { + if ($suggestionLimit !== null && $processedCount >= $suggestionLimit) { + break; + } - $premiumXpath = implode('/', [ - '//body', - 'data_block', - 'dt_assoc', - 'item[@key="attributes"]', - 'dt_assoc', - 'item[@key="premium"]', - 'dt_assoc', - 'item[@key="items"]', - 'dt_array', - 'item', - ]); - $premiumElements = $result->xpath($premiumXpath); - foreach ($premiumElements as $element) { - $item = $element->xpath('dt_assoc/item'); - - $domain = null; - $available = false; - $price = null; - - foreach ($item as $field) { - $key = (string) $field['key']; - $value = (string) $field; - - switch ($key) { - case 'domain': - $domain = $value; - break; - case 'status': - $available = $value === 'available'; - break; - case 'price': - $price = is_numeric($value) ? (float) $value : null; - break; + $domainNode = $element->xpath('dt_assoc/item[@key="domain"]'); + $statusNode = $element->xpath('dt_assoc/item[@key="status"] | dt_assoc/item[@key="availability"]'); + $domain = isset($domainNode[0]) ? (string) $domainNode[0] : null; + $status = isset($statusNode[0]) ? strtolower((string) $statusNode[0]) : ''; + $available = in_array($status, ['available', 'true', '1'], true); + + if ($domain) { + $price = null; + if ($available) { + try { + $priceData = $this->getPrice($domain, 1, 'new'); + $price = $priceData['price']; + } catch (Exception $e) { + // If price lookup fails, continue without price + $price = null; + } + } + + $items[$domain] = [ + 'available' => $available, + 'price' => $price, + 'type' => 'suggestion' + ]; + + $processedCount++; } } - if ($domain) { - $items[$domain] = [ - 'available' => $available, - 'price' => $price, - 'type' => 'premium' - ]; + if ($filterType === 'suggestion' && $sortOrder === null) { + return $items; + } + + if ($limit && count($items) >= $limit && $sortOrder === null) { + return array_slice($items, 0, $limit, true); } } - // If limit is specified, prioritize premium domains and limit total results - if ($limit && count($items) > $limit) { - $premiumDomains = []; - $suggestionDomains = []; + // Process premium domains + if ( + ($filterType === 'premium' || $filterType === null) && + !($limit && count($items) >= $limit) + ) { + $premiumXpath = implode('/', [ + '//body', + 'data_block', + 'dt_assoc', + 'item[@key="attributes"]', + 'dt_assoc', + 'item[@key="premium"]', + 'dt_assoc', + 'item[@key="items"]', + 'dt_array', + 'item', + ]); + $premiumElements = $result->xpath($premiumXpath); + + $remainingLimit = $limit ? ($limit - count($items)) : null; + $processedCount = 0; + + foreach ($premiumElements as $element) { + if ($remainingLimit !== null && $processedCount >= $remainingLimit) { + break; + } + + $item = $element->xpath('dt_assoc/item'); + + $domain = null; + $available = false; + $price = null; + + foreach ($item as $field) { + $key = (string) $field['key']; + $value = (string) $field; + + switch ($key) { + case 'domain': + $domain = $value; + break; + case 'status': + $available = $value === 'available'; + break; + case 'price': + $price = is_numeric($value) ? floatval($value) : null; + break; + } + } - foreach ($items as $domain => $data) { - if ($data['type'] === 'premium') { - $premiumDomains[$domain] = $data; - } else { - $suggestionDomains[$domain] = $data; + if ($domain) { + $items[$domain] = [ + 'available' => $available, + 'price' => $price, + 'type' => 'premium' + ]; + + $processedCount++; } } + } - $result = []; - $premiumCount = min(count($premiumDomains), $limit); - $suggestionCount = $limit - $premiumCount; + if ($sortOrder !== null) { + uasort($items, function($a, $b) use ($sortOrder) { + $priceA = $a['price'] !== null ? $a['price'] : PHP_FLOAT_MAX; + $priceB = $b['price'] !== null ? $b['price'] : PHP_FLOAT_MAX; - $result = array_slice($premiumDomains, 0, $premiumCount, true); - if ($suggestionCount > 0) { - $suggestions = array_slice($suggestionDomains, 0, $suggestionCount, true); - $result = array_merge($result, $suggestions); - } + if ($sortOrder === 'asc') { + if ($priceA === $priceB) return 0; + return $priceA < $priceB ? -1 : 1; + } else { + if ($priceA === $priceB) return 0; + return $priceA > $priceB ? -1 : 1; + } + }); - return $result; + if ($limit && count($items) > $limit) { + $items = array_slice($items, 0, $limit, true); + } } return $items; } + /** + * Get the registration price for a domain + * + * @param string $domain The domain name to get pricing for + * @param int $period Registration period in years (default 1) + * @param string $regType Type of registration: 'new', 'renewal', 'transfer', or 'trade' + * @return array Contains 'price' (float), 'is_registry_premium' (bool), and 'registry_premium_group' (string|null) + */ + public function getPrice(string $domain, int $period = 1, string $regType = 'new'): array + { + $message = [ + 'object' => 'DOMAIN', + 'action' => 'GET_PRICE', + 'attributes' => [ + 'domain' => $domain, + 'period' => $period, + 'reg_type' => $regType, + ], + ]; + + $result = $this->send($message); + $result = $this->sanitizeResponse($result); + + $priceXpath = '//body/data_block/dt_assoc/item[@key="attributes"]/dt_assoc/item[@key="price"]'; + $priceElements = $result->xpath($priceXpath); + $price = isset($priceElements[0]) ? floatval((string) $priceElements[0]) : null; + + $isPremiumXpath = '//body/data_block/dt_assoc/item[@key="attributes"]/dt_assoc/item[@key="is_registry_premium"]'; + $isPremiumElements = $result->xpath($isPremiumXpath); + $isRegistryPremium = isset($isPremiumElements[0]) ? ((string) $isPremiumElements[0] === '1') : false; + + $premiumGroupXpath = '//body/data_block/dt_assoc/item[@key="attributes"]/dt_assoc/item[@key="registry_premium_group"]'; + $premiumGroupElements = $result->xpath($premiumGroupXpath); + $registryPremiumGroup = isset($premiumGroupElements[0]) ? (string) $premiumGroupElements[0] : null; + + return [ + 'price' => $price, + 'is_registry_premium' => $isRegistryPremium, + 'registry_premium_group' => $registryPremiumGroup, + ]; + } + public function tlds(): array { // OpenSRS offers no endpoint for this diff --git a/tests/OpenSRSTest.php b/tests/OpenSRSTest.php index 95f52a8..4305c1b 100644 --- a/tests/OpenSRSTest.php +++ b/tests/OpenSRSTest.php @@ -68,7 +68,7 @@ public function testCancelPurchase(): void public function testSuggest(): void { - // Test 1: Basic suggestion without filters + // Test 1: Suggestion domains only with prices $result = $this->client->suggest( [ 'monkeys', @@ -78,19 +78,21 @@ public function testSuggest(): void 'com', 'net', 'org', - ] + ], + 5, + 'suggestion' ); $this->assertIsArray($result); foreach ($result as $domain => $data) { - if ($data['type'] === 'premium') { + $this->assertEquals('suggestion', $data['type']); + if ($data['available'] && $data['price'] !== null) { + $this->assertIsFloat($data['price']); $this->assertGreaterThan(0, $data['price']); - } else { - $this->assertEquals(null, $data['price']); } } - // Test 2: Suggestion with limit + // Test 2: Mixed results (default behavior - both premium and suggestions) $result = $this->client->suggest( 'monkeys', [ @@ -98,34 +100,113 @@ public function testSuggest(): void 'net', 'org', ], - 10 + 5 ); $this->assertIsArray($result); - $this->assertCount(10, $result); + $this->assertCount(5, $result); - // Test 3: Premium suggestions with price filters + foreach ($result as $domain => $data) { + if ($data['type'] === 'premium') { + $this->assertIsFloat($data['price']); + $this->assertGreaterThan(0, $data['price']); + } elseif ($data['available'] && $data['price'] !== null) { + $this->assertIsFloat($data['price']); + } + } + + // Test 3: Premium domains only with price filters $result = $this->client->suggest( 'computer', [ 'com', 'net', ], - 10, + 5, + 'premium', 10000, 100 ); $this->assertIsArray($result); - $this->assertLessThanOrEqual(10, count($result)); + $this->assertLessThanOrEqual(5, count($result)); foreach ($result as $domain => $data) { $this->assertEquals('premium', $data['type']); if ($data['price'] !== null) { + $this->assertIsFloat($data['price']); $this->assertGreaterThanOrEqual(100, $data['price']); $this->assertLessThanOrEqual(10000, $data['price']); } } + + // Test 4: Premium domains without price filters + $result = $this->client->suggest( + 'business', + [ + 'com', + ], + 5, + 'premium' + ); + + $this->assertIsArray($result); + $this->assertLessThanOrEqual(5, count($result)); + + foreach ($result as $domain => $data) { + $this->assertEquals('premium', $data['type']); + if ($data['price'] !== null) { + $this->assertIsFloat($data['price']); + } + } + + // Test 5: Single TLD search + $result = $this->client->suggest( + 'example', + ['org'], + 3, + 'suggestion' + ); + + $this->assertIsArray($result); + $this->assertLessThanOrEqual(3, count($result)); + + foreach ($result as $domain => $data) { + $this->assertEquals('suggestion', $data['type']); + $this->assertStringEndsWith('.org', $domain); + } + + // Test 6: Sort by price ascending + $result = $this->client->suggest( + 'shop', + ['com', 'net'], + 5, + null, + null, + null, + 'asc' + ); + + $this->assertIsArray($result); + $this->assertGreaterThan(0, count($result)); + + $prices = []; + foreach ($result as $domain => $data) { + if ($data['price'] !== null) { + $prices[] = $data['price']; + } + } + + // Verify that prices are in ascending order (each price >= previous) + if (count($prices) > 1) { + for ($i = 1; $i < count($prices); $i++) { + $this->assertGreaterThanOrEqual( + $prices[$i - 1], + $prices[$i], + "Price at index $i ({$prices[$i]}) should be >= price at index " . ($i - 1) . " ({$prices[$i - 1]})" + ); + } + } } public function testUpdateNameservers(): void From 9e08bcc2a75301c582aba7602be31fdafa65dc0c Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 5 Oct 2025 19:14:12 +0530 Subject: [PATCH 4/6] lint --- src/Domains/Registrar/OpenSRS.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Domains/Registrar/OpenSRS.php b/src/Domains/Registrar/OpenSRS.php index 643390b..70eae61 100644 --- a/src/Domains/Registrar/OpenSRS.php +++ b/src/Domains/Registrar/OpenSRS.php @@ -352,7 +352,7 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = // Process premium domains if ( - ($filterType === 'premium' || $filterType === null) && + ($filterType === 'premium' || $filterType === null) && !($limit && count($items) >= $limit) ) { $premiumXpath = implode('/', [ @@ -413,15 +413,19 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = } if ($sortOrder !== null) { - uasort($items, function($a, $b) use ($sortOrder) { + uasort($items, function ($a, $b) use ($sortOrder) { $priceA = $a['price'] !== null ? $a['price'] : PHP_FLOAT_MAX; $priceB = $b['price'] !== null ? $b['price'] : PHP_FLOAT_MAX; if ($sortOrder === 'asc') { - if ($priceA === $priceB) return 0; + if ($priceA === $priceB) { + return 0; + } return $priceA < $priceB ? -1 : 1; } else { - if ($priceA === $priceB) return 0; + if ($priceA === $priceB) { + return 0; + } return $priceA > $priceB ? -1 : 1; } }); From aba0b34a2ccc6a520f1f11dc748630c2aea8d38a Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 6 Oct 2025 07:41:29 +0530 Subject: [PATCH 5/6] remove sorting --- src/Domains/Registrar/OpenSRS.php | 34 +++---------------------------- tests/OpenSRSTest.php | 32 ----------------------------- 2 files changed, 3 insertions(+), 63 deletions(-) diff --git a/src/Domains/Registrar/OpenSRS.php b/src/Domains/Registrar/OpenSRS.php index 70eae61..e37b9a8 100644 --- a/src/Domains/Registrar/OpenSRS.php +++ b/src/Domains/Registrar/OpenSRS.php @@ -234,10 +234,9 @@ public function cancelPurchase(): bool * @param string|null $filterType Filter results by type: 'premium', 'suggestion', or null for both * @param int|null $premiumPriceMax Maximum price for premium domains * @param int|null $premiumPriceMin Minimum price for premium domains - * @param string|null $sortOrder Sort order for prices: 'asc', 'desc', or null for no sorting * @return array Domains with metadata: `available` (bool), `price` (float|null), `type` (string) */ - public function suggest(array|string $query, array $tlds = [], int|null $limit = null, string|null $filterType = null, int|null $premiumPriceMax = null, int|null $premiumPriceMin = null, string|null $sortOrder = null): array + public function suggest(array|string $query, array $tlds = [], int|null $limit = null, string|null $filterType = null, int|null $premiumPriceMax = null, int|null $premiumPriceMin = null): array { if ($premiumPriceMin !== null && $premiumPriceMax !== null && $premiumPriceMin >= $premiumPriceMax) { throw new Exception("Invalid price range: premiumPriceMin ($premiumPriceMin) must be less than premiumPriceMax ($premiumPriceMax)."); @@ -251,10 +250,6 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = throw new Exception("Invalid price range: premiumPriceMin ($premiumPriceMin) and premiumPriceMax ($premiumPriceMax) cannot be set when filterType is 'suggestion'."); } - if ($sortOrder !== null && !in_array($sortOrder, ['asc', 'desc'])) { - throw new Exception("Invalid sort order: sortOrder ($sortOrder) must be 'asc' or 'desc'."); - } - $query = is_array($query) ? $query : [$query]; $message = [ 'object' => 'DOMAIN', @@ -341,11 +336,11 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = } } - if ($filterType === 'suggestion' && $sortOrder === null) { + if ($filterType === 'suggestion') { return $items; } - if ($limit && count($items) >= $limit && $sortOrder === null) { + if ($limit && count($items) >= $limit) { return array_slice($items, 0, $limit, true); } } @@ -412,29 +407,6 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = } } - if ($sortOrder !== null) { - uasort($items, function ($a, $b) use ($sortOrder) { - $priceA = $a['price'] !== null ? $a['price'] : PHP_FLOAT_MAX; - $priceB = $b['price'] !== null ? $b['price'] : PHP_FLOAT_MAX; - - if ($sortOrder === 'asc') { - if ($priceA === $priceB) { - return 0; - } - return $priceA < $priceB ? -1 : 1; - } else { - if ($priceA === $priceB) { - return 0; - } - return $priceA > $priceB ? -1 : 1; - } - }); - - if ($limit && count($items) > $limit) { - $items = array_slice($items, 0, $limit, true); - } - } - return $items; } diff --git a/tests/OpenSRSTest.php b/tests/OpenSRSTest.php index 4305c1b..8bf623b 100644 --- a/tests/OpenSRSTest.php +++ b/tests/OpenSRSTest.php @@ -175,38 +175,6 @@ public function testSuggest(): void $this->assertEquals('suggestion', $data['type']); $this->assertStringEndsWith('.org', $domain); } - - // Test 6: Sort by price ascending - $result = $this->client->suggest( - 'shop', - ['com', 'net'], - 5, - null, - null, - null, - 'asc' - ); - - $this->assertIsArray($result); - $this->assertGreaterThan(0, count($result)); - - $prices = []; - foreach ($result as $domain => $data) { - if ($data['price'] !== null) { - $prices[] = $data['price']; - } - } - - // Verify that prices are in ascending order (each price >= previous) - if (count($prices) > 1) { - for ($i = 1; $i < count($prices); $i++) { - $this->assertGreaterThanOrEqual( - $prices[$i - 1], - $prices[$i], - "Price at index $i ({$prices[$i]}) should be >= price at index " . ($i - 1) . " ({$prices[$i - 1]})" - ); - } - } } public function testUpdateNameservers(): void From 1b3cadfa02aacd4ad6c98b4619f44a7f9d338a7a Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 6 Oct 2025 08:13:04 +0530 Subject: [PATCH 6/6] remove price fetch --- src/Domains/Registrar/OpenSRS.php | 29 +++++++++++++---------------- tests/OpenSRSTest.php | 12 ++++++++++++ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Domains/Registrar/OpenSRS.php b/src/Domains/Registrar/OpenSRS.php index e37b9a8..22816c8 100644 --- a/src/Domains/Registrar/OpenSRS.php +++ b/src/Domains/Registrar/OpenSRS.php @@ -264,13 +264,21 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = if (!empty($tlds)) { $message['attributes']['tlds'] = $tlds; - $message['attributes']['service_override']['premium']['tlds'] = $tlds; - $message['attributes']['service_override']['suggestion']['tlds'] = $tlds; + if ($filterType === 'premium' || $filterType === null) { + $message['attributes']['service_override']['premium']['tlds'] = $tlds; + } + if ($filterType === 'suggestion' || $filterType === null) { + $message['attributes']['service_override']['suggestion']['tlds'] = $tlds; + } $message['attributes']['service_override']['lookup']['tlds'] = $tlds; } if ($limit) { - $message['attributes']['service_override']['premium']['maximum'] = $limit; - $message['attributes']['service_override']['suggestion']['maximum'] = $limit; + if ($filterType === 'premium' || $filterType === null) { + $message['attributes']['service_override']['premium']['maximum'] = $limit; + } + if ($filterType === 'suggestion' || $filterType === null) { + $message['attributes']['service_override']['suggestion']['maximum'] = $limit; + } } if ($premiumPriceMin !== null) { $message['attributes']['service_override']['premium']['price_min'] = $premiumPriceMin; @@ -315,20 +323,9 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit = $available = in_array($status, ['available', 'true', '1'], true); if ($domain) { - $price = null; - if ($available) { - try { - $priceData = $this->getPrice($domain, 1, 'new'); - $price = $priceData['price']; - } catch (Exception $e) { - // If price lookup fails, continue without price - $price = null; - } - } - $items[$domain] = [ 'available' => $available, - 'price' => $price, + 'price' => null, 'type' => 'suggestion' ]; diff --git a/tests/OpenSRSTest.php b/tests/OpenSRSTest.php index 8bf623b..fa9b5a1 100644 --- a/tests/OpenSRSTest.php +++ b/tests/OpenSRSTest.php @@ -177,6 +177,18 @@ public function testSuggest(): void } } + public function testGetPrice(): void + { + $result = $this->client->getPrice($this->domain, 1, 'new'); + + $this->assertIsArray($result); + $this->assertArrayHasKey('price', $result); + $this->assertArrayHasKey('is_registry_premium', $result); + $this->assertArrayHasKey('registry_premium_group', $result); + $this->assertIsFloat($result['price']); + $this->assertIsBool($result['is_registry_premium']); + } + public function testUpdateNameservers(): void { $result = $this->client->updateNameservers($this->domain, [