From 3fff3ea68822116165fefe19d0481f0f713bc28c Mon Sep 17 00:00:00 2001 From: Smith Date: Thu, 14 May 2026 12:18:21 +0300 Subject: [PATCH] feat(custom-properties): implement custom properties commands (#17) Co-Authored-By: Claude Sonnet 4.6 --- COMMANDS.md | 8 +- .../Resources/CustomPropertiesContract.php | 16 +++- src/Resources/CustomProperties.php | 36 +++++++++ .../CustomProperties/InfoResponse.php | 25 ++++++ .../CustomProperties/InfoResponseProperty.php | 26 +++++++ .../CustomProperties/SearchResponse.php | 25 ++++++ .../CustomProperties/SearchResponseResult.php | 26 +++++++ tests/Unit/Resources/CustomPropertiesTest.php | 76 +++++++++++++++++++ .../InfoResponsePropertyTest.php | 17 +++++ .../CustomProperties/InfoResponseTest.php | 17 +++++ .../SearchResponseResultTest.php | 17 +++++ .../CustomProperties/SearchResponseTest.php | 17 +++++ 12 files changed, 303 insertions(+), 3 deletions(-) create mode 100644 src/Responses/CustomProperties/InfoResponse.php create mode 100644 src/Responses/CustomProperties/InfoResponseProperty.php create mode 100644 src/Responses/CustomProperties/SearchResponse.php create mode 100644 src/Responses/CustomProperties/SearchResponseResult.php create mode 100644 tests/Unit/Resources/CustomPropertiesTest.php create mode 100644 tests/Unit/Responses/CustomProperties/InfoResponsePropertyTest.php create mode 100644 tests/Unit/Responses/CustomProperties/InfoResponseTest.php create mode 100644 tests/Unit/Responses/CustomProperties/SearchResponseResultTest.php create mode 100644 tests/Unit/Responses/CustomProperties/SearchResponseTest.php diff --git a/COMMANDS.md b/COMMANDS.md index b0239a5..cd9866d 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -767,11 +767,15 @@ $teamspeak->bans()->deleteAll(); ## customsearch -Not implemented. +```php +$teamspeak->customProperties()->search(ident: 'forum_id', pattern: '12345'); +``` ## custominfo -Not implemented. +```php +$teamspeak->customProperties()->info(clientDatabaseId: 18); +``` ## whoami diff --git a/src/Contracts/Resources/CustomPropertiesContract.php b/src/Contracts/Resources/CustomPropertiesContract.php index d7a530d..cf35d97 100644 --- a/src/Contracts/Resources/CustomPropertiesContract.php +++ b/src/Contracts/Resources/CustomPropertiesContract.php @@ -4,4 +4,18 @@ namespace TeamSpeak\WebQuery\Contracts\Resources; -interface CustomPropertiesContract {} +use TeamSpeak\WebQuery\Responses\CustomProperties\InfoResponse; +use TeamSpeak\WebQuery\Responses\CustomProperties\SearchResponse; + +interface CustomPropertiesContract +{ + /** + * Searches for clients with a custom property matching the given ident and pattern. + */ + public function search(string $ident, string $pattern): SearchResponse; + + /** + * Displays all custom properties for the client with the given database ID. + */ + public function info(int $clientDatabaseId): InfoResponse; +} diff --git a/src/Resources/CustomProperties.php b/src/Resources/CustomProperties.php index ec83a2a..713c25a 100644 --- a/src/Resources/CustomProperties.php +++ b/src/Resources/CustomProperties.php @@ -5,8 +5,44 @@ namespace TeamSpeak\WebQuery\Resources; use TeamSpeak\WebQuery\Contracts\Resources\CustomPropertiesContract; +use TeamSpeak\WebQuery\Enums\Query\Command; +use TeamSpeak\WebQuery\Responses\CustomProperties\InfoResponse; +use TeamSpeak\WebQuery\Responses\CustomProperties\SearchResponse; +use TeamSpeak\WebQuery\ValueObjects\Transporter\Payload; final class CustomProperties implements CustomPropertiesContract { use Concerns\Transportable; + + /** + * Searches for clients with a custom property matching the given ident and pattern. + */ + public function search(string $ident, string $pattern): SearchResponse + { + $payload = new Payload( + command: Command::CustomSearch, + parameters: ['ident' => $ident, 'pattern' => $pattern], + ); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response> $response */ + $response = $this->transporter->request($payload); + + return SearchResponse::from($response->body()); + } + + /** + * Displays all custom properties for the client with the given database ID. + */ + public function info(int $clientDatabaseId): InfoResponse + { + $payload = new Payload( + command: Command::CustomInfo, + parameters: ['cldbid' => $clientDatabaseId], + ); + + /** @var \TeamSpeak\WebQuery\ValueObjects\Transporter\Response> $response */ + $response = $this->transporter->request($payload); + + return InfoResponse::from($response->body()); + } } diff --git a/src/Responses/CustomProperties/InfoResponse.php b/src/Responses/CustomProperties/InfoResponse.php new file mode 100644 index 0000000..65373fa --- /dev/null +++ b/src/Responses/CustomProperties/InfoResponse.php @@ -0,0 +1,25 @@ + $properties + */ + private function __construct( + public array $properties, + ) {} + + /** + * @param list $attributes + */ + public static function from(array $attributes): self + { + return new self( + array_map(InfoResponseProperty::from(...), $attributes), + ); + } +} diff --git a/src/Responses/CustomProperties/InfoResponseProperty.php b/src/Responses/CustomProperties/InfoResponseProperty.php new file mode 100644 index 0000000..5284578 --- /dev/null +++ b/src/Responses/CustomProperties/InfoResponseProperty.php @@ -0,0 +1,26 @@ + $results + */ + private function __construct( + public array $results, + ) {} + + /** + * @param list $attributes + */ + public static function from(array $attributes): self + { + return new self( + array_map(SearchResponseResult::from(...), $attributes), + ); + } +} diff --git a/src/Responses/CustomProperties/SearchResponseResult.php b/src/Responses/CustomProperties/SearchResponseResult.php new file mode 100644 index 0000000..78165c0 --- /dev/null +++ b/src/Responses/CustomProperties/SearchResponseResult.php @@ -0,0 +1,26 @@ +client = Mockery::mock(ClientInterface::class); + + $apiKey = ApiKey::from('foo'); + + $this->http = new HttpTransporter( + $this->client, + BaseUri::from('teamspeak.com'), + Headers::withXApiKey($apiKey), + ); +}); + +test('search', function () { + $resource = new CustomProperties($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'cldbid' => '18', + 'ident' => 'forum_id', + 'value' => '12345', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['ident' => 'forum_id', 'pattern' => '12345']); + + return true; + })->andReturn($response); + + expect($resource->search('forum_id', '12345')->results)->toBeArray()->toHaveCount(1); +}); + +test('info', function () { + $resource = new CustomProperties($this->http); + + $response = new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'body' => [[ + 'cldbid' => '18', + 'ident' => 'forum_id', + 'value' => '12345', + ]], + 'status' => ['code' => 0, 'message' => 'ok'], + ])); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->withArgs(function (Psr7Request $request) { + expect($request->getBody()->getContents())->toBeJson() + ->json() + ->toBe(['cldbid' => '18']); + + return true; + })->andReturn($response); + + expect($resource->info(18)->properties)->toBeArray()->toHaveCount(1); +}); diff --git a/tests/Unit/Responses/CustomProperties/InfoResponsePropertyTest.php b/tests/Unit/Responses/CustomProperties/InfoResponsePropertyTest.php new file mode 100644 index 0000000..2ae1e6b --- /dev/null +++ b/tests/Unit/Responses/CustomProperties/InfoResponsePropertyTest.php @@ -0,0 +1,17 @@ + '18', + 'ident' => 'forum_id', + 'value' => '12345', + ]); + + expect($property->clientDatabaseId)->toBe(18) + ->and($property->ident)->toBe('forum_id') + ->and($property->value)->toBe('12345'); +}); diff --git a/tests/Unit/Responses/CustomProperties/InfoResponseTest.php b/tests/Unit/Responses/CustomProperties/InfoResponseTest.php new file mode 100644 index 0000000..3bc49a0 --- /dev/null +++ b/tests/Unit/Responses/CustomProperties/InfoResponseTest.php @@ -0,0 +1,17 @@ + '18', + 'ident' => 'forum_id', + 'value' => '12345', + ]]); + + expect($response->properties)->toHaveCount(1) + ->and($response->properties[0])->toBeInstanceOf(InfoResponseProperty::class); +}); diff --git a/tests/Unit/Responses/CustomProperties/SearchResponseResultTest.php b/tests/Unit/Responses/CustomProperties/SearchResponseResultTest.php new file mode 100644 index 0000000..0ab9347 --- /dev/null +++ b/tests/Unit/Responses/CustomProperties/SearchResponseResultTest.php @@ -0,0 +1,17 @@ + '18', + 'ident' => 'forum_id', + 'value' => '12345', + ]); + + expect($result->clientDatabaseId)->toBe(18) + ->and($result->ident)->toBe('forum_id') + ->and($result->value)->toBe('12345'); +}); diff --git a/tests/Unit/Responses/CustomProperties/SearchResponseTest.php b/tests/Unit/Responses/CustomProperties/SearchResponseTest.php new file mode 100644 index 0000000..f28962a --- /dev/null +++ b/tests/Unit/Responses/CustomProperties/SearchResponseTest.php @@ -0,0 +1,17 @@ + '18', + 'ident' => 'forum_id', + 'value' => '12345', + ]]); + + expect($response->results)->toHaveCount(1) + ->and($response->results[0])->toBeInstanceOf(SearchResponseResult::class); +});