diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index 417d5c6d..4ca93ec0 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -54,6 +54,7 @@ use Zendesk\API\Resources\Core\UserFields; use Zendesk\API\Resources\Core\Users; use Zendesk\API\Resources\Core\Views; +use Zendesk\API\Resources\Core\Webhooks; use Zendesk\API\Resources\Embeddable; use Zendesk\API\Resources\HelpCenter; use Zendesk\API\Resources\Talk; @@ -98,6 +99,7 @@ * @method SuspendedTickets suspendedTickets() * @method Tags tags($id = null) * @method Targets targets() + * @method Webhooks webhooks() * @method Tickets tickets($id = null) * @method TicketFields ticketFields($id = null) * @method TicketImports ticketImports() @@ -281,6 +283,7 @@ public static function getValidSubResources() 'userFields' => UserFields::class, 'users' => Users::class, 'views' => Views::class, + 'webhooks' => Webhooks::class, ]; } diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php new file mode 100644 index 00000000..6c462e06 --- /dev/null +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -0,0 +1,55 @@ +setRoutes([ + 'create' => "{$this->resourceName}", + 'update' => "{$this->resourceName}/{id}", + 'delete' => "{$this->resourceName}", + 'findAll' => "{$this->resourceName}", + ]); + } + + /** + * {@inheritdoc} + */ + public function findAll(array $params = []) + { + $queryParams = array_filter(array_flip($params), [$this, 'filterParams']); + $queryParams = array_merge($params, array_flip($queryParams)); + + return $this->traitFindAll($queryParams); + } + + /** + * Filter parameters passed and only allow valid query parameters. + * + * @param $param + * @return int + */ + private function filterParams($param) + { + return preg_match("/^sort|page[[a-zA-Z_]*]|filter[[a-zA-Z_]*](\\[\\]?)/", $param); + } +} diff --git a/tests/Zendesk/API/UnitTests/Core/WebhooksTest.php b/tests/Zendesk/API/UnitTests/Core/WebhooksTest.php new file mode 100644 index 00000000..0c0e40ef --- /dev/null +++ b/tests/Zendesk/API/UnitTests/Core/WebhooksTest.php @@ -0,0 +1,38 @@ + 'somerule', + ]; + + // We expect invalid parameters are removed. + // We also expect url encoded keys and values + $expectedQueryParams = []; + foreach ($queryParams as $key => $value) { + $expectedQueryParams = array_merge($expectedQueryParams, [urlencode($key) => $value]); + } + + $this->assertEndpointCalled( + function () use ($queryParams) { + $this->client->webhooks()->findAll($queryParams); + }, + 'webhooks', + 'GET', + ['queryParams' => $expectedQueryParams] + ); + } +}