diff --git a/samples/helpcenter/createSections.php b/samples/helpcenter/createSections.php new file mode 100644 index 00000000..4aa13d68 --- /dev/null +++ b/samples/helpcenter/createSections.php @@ -0,0 +1,32 @@ +setAuth('basic', ['username' => $username, 'token' => $token]); + +try { + // Create a new HelpCenter Section + $categoryId = 1; + $section = $client->helpCenter->categories($categoryId)->sections()->create([ + 'position' => 1, + 'locale' => 'en-us', + 'name' => 'Super Hero Tricks', + 'description' => 'This section contains a collection of super hero tricks', + ]); + echo "
"; + print_r($section); + echo ""; +} catch (\Zendesk\API\Exceptions\ApiResponseException $e) { + echo $e->getMessage().''; +} diff --git a/samples/helpcenter/findSections.php b/samples/helpcenter/findSections.php new file mode 100644 index 00000000..8cec98e6 --- /dev/null +++ b/samples/helpcenter/findSections.php @@ -0,0 +1,45 @@ +setAuth('basic', ['username' => $username, 'token' => $token]); + +try { + // Find all helpcenter sections + $sections = $client->helpCenter->sections()->findAll(); + echo "
"; + print_r($sections); + echo ""; + + // Find all helpcenter sections with a specific locale + $sections = $client->helpCenter->sections()->setLocale('en-us')->findAll(); + echo "
"; + print_r($sections); + echo ""; + + // Find all helpcenter sections within a specific category + $categoryId = 204009948; + $sections = $client->helpCenter->categories($categoryId)->sections()->findAll(); + echo "
"; + print_r($sections); + echo ""; + + // Find all helpcenter sections with a specific locale in a category + $sections = $client->helpCenter->categories($categoryId)->sections()->setLocale('en-us')->findAll(); + echo "
"; + print_r($sections); + echo ""; +} catch (\Zendesk\API\Exceptions\ApiResponseException $e) { + echo $e->getMessage().''; +} diff --git a/src/Zendesk/API/Resources/HelpCenter/Categories.php b/src/Zendesk/API/Resources/HelpCenter/Categories.php index b484b9e4..9484973e 100644 --- a/src/Zendesk/API/Resources/HelpCenter/Categories.php +++ b/src/Zendesk/API/Resources/HelpCenter/Categories.php @@ -4,13 +4,16 @@ use Zendesk\API\Traits\Resource\Defaults; use Zendesk\API\Traits\Resource\Locales; +use Zendesk\API\Traits\Utility\InstantiatorTrait; /** * Class Categories * https://developer.zendesk.com/rest_api/docs/help_center/categories + * @method Sections sections() */ class Categories extends ResourceAbstract { + use InstantiatorTrait; use Defaults; use Locales; @@ -19,6 +22,16 @@ class Categories extends ResourceAbstract */ protected $objectName = 'category'; + /** + * {@inheritdoc} + */ + public static function getValidSubResources() + { + return [ + 'sections' => Sections::class, + ]; + } + /** * @inheritdoc */ diff --git a/src/Zendesk/API/Resources/HelpCenter/Sections.php b/src/Zendesk/API/Resources/HelpCenter/Sections.php index d679c908..e28e8878 100644 --- a/src/Zendesk/API/Resources/HelpCenter/Sections.php +++ b/src/Zendesk/API/Resources/HelpCenter/Sections.php @@ -12,7 +12,9 @@ class Sections extends ResourceAbstract { use Defaults; - use Locales; + use Locales { + getRoute as protected localesGetRoute; + } /** * {@inheritdoc} @@ -24,6 +26,48 @@ class Sections extends ResourceAbstract */ protected function setUpRoutes() { - $this->setRoute('updateSourceLocale', "{$this->resourceName}/{sectionId}/source_locale.json"); + $this->setRoutes([ + 'updateSourceLocale' => "{$this->resourceName}/{sectionId}/source_locale.json", + 'create' => "{$this->prefix}categories/{category_id}/sections.json", + ]); + } + + /** + * Returns a route and replaces tokenized parts of the string with + * the passed params + * + * @param string $name + * @param array $params + * + * @return mixed The default routes, or if $name is set to `findAll`, any of the following formats + * based on the parent chain + * GET /api/v2/helpcenter/sections.json + * GET /api/v2/helpcenter/categories/{category_id}/sections.json + * + * @throws \Exception + */ + public function getRoute($name, array $params = []) + { + $lastChained = $this->getLatestChainedParameter(); + $params = $this->addChainedParametersToParams($params, [ + 'category_id' => Categories::class + ]); + + if (empty($lastChained) || $name !== 'findAll') { + return $this->localesGetRoute($name, $params); + } else { + $chainedResourceName = array_keys($lastChained)[0]; + $id = $lastChained[$chainedResourceName]; + if ($chainedResourceName === Categories::class) { + $locales = $this->getLocale(); + if ($locales) { + return "{$this->prefix}{$locales}/categories/$id/sections.json"; + } else { + return "{$this->prefix}categories/$id/sections.json"; + } + } else { + return $this->localesGetRoute($name, $params); + } + } } } diff --git a/tests/Zendesk/API/UnitTests/HelpCenter/SectionsTest.php b/tests/Zendesk/API/UnitTests/HelpCenter/SectionsTest.php index db8e8b68..2bb74610 100644 --- a/tests/Zendesk/API/UnitTests/HelpCenter/SectionsTest.php +++ b/tests/Zendesk/API/UnitTests/HelpCenter/SectionsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\HelpCenter; +use Faker\Factory; use Zendesk\API\Resources\HelpCenter\Sections; use Zendesk\API\UnitTests\BasicTest; @@ -12,31 +13,39 @@ class SectionsTest extends BasicTest */ public function testRoutesWithLocale() { + $faker = Factory::create(); + $locale = $faker->locale; + $sectionsResource = $this->client->helpCenter->sections(); - $sectionsResource->setLocale('en-US'); + $sectionsResource->setLocale($locale); $this->assertEndpointCalled(function () use ($sectionsResource) { $sectionsResource->findAll(); - }, 'help_center/en-US/sections.json'); + }, "help_center/{$locale}/sections.json"); - $this->assertEndpointCalled(function () use ($sectionsResource) { - $sectionsResource->find(1); - }, 'help_center/en-US/sections/1.json'); + $categoryId = $faker->numberBetween(1); + $this->assertEndpointCalled(function () use ($categoryId, $locale) { + $resource = $this->client->helpCenter->categories($categoryId)->sections(); + $resource->setLocale($locale)->findAll(); + }, "help_center/{$locale}/categories/{$categoryId}/sections.json"); + + $sectionId = $faker->numberBetween(1); + $this->assertEndpointCalled(function () use ($sectionsResource, $sectionId) { + $sectionsResource->find($sectionId); + }, "help_center/{$locale}/sections/{$sectionId}.json"); $postFields = [ - 'position' => 2, - 'locale' => 'en-us', - 'name' => 'Super Hero Tricks', - 'description' => 'This section contains a collection of super hero tricks', + 'position' => $faker->numberBetween(1), + 'locale' => $locale, + 'name' => $faker->sentence, + 'description' => $faker->sentence, ]; - $this->assertEndpointCalled(function () use ($sectionsResource, $postFields) { - $sectionsResource->create($postFields); - }, 'help_center/en-US/sections.json', 'POST', ['postFields' => ['section' => $postFields]]); - - $this->assertEndpointCalled(function () use ($sectionsResource, $postFields) { - $sectionsResource->update(1, $postFields); - }, 'help_center/en-US/sections/1.json', 'PUT', ['postFields' => ['section' => $postFields]]); + $this->assertEndpointCalled(function () use ($sectionsResource, $postFields, $sectionId) { + $sectionsResource->update($sectionId, $postFields); + }, "help_center/{$locale}/sections/{$sectionId}.json", 'PUT', [ + 'postFields' => ['section' => $postFields] + ]); } /** @@ -44,9 +53,15 @@ public function testRoutesWithLocale() */ public function testRouteWithoutLocale() { + $faker = Factory::create(); $this->assertEndpointCalled(function () { $this->client->helpCenter->sections()->findAll(); }, 'help_center/sections.json'); + + $categoryId = $faker->numberBetween(1); + $this->assertEndpointCalled(function () use ($categoryId) { + $this->client->helpCenter->categories($categoryId)->sections()->findAll(); + }, "help_center/categories/{$categoryId}/sections.json"); } /** @@ -68,4 +83,25 @@ public function testUpdateSectionSourceLocaleNoId() $this->client->helpCenter->sections(1)->updateSourceLocale(null, 'fr'); }, 'help_center/sections/1/source_locale.json', 'PUT', ['postFields' => ['section_locale' => 'fr']]); } + + /** + * Tests if the Create section endpoint can be called and passed the correct params + */ + public function testCanCreateSection() + { + $faker = Factory::create(); + $params = [ + 'position' => $faker->numberBetween(1), + 'name' => $faker->sentence, + 'description' => $faker->sentence, + 'locale' => $faker->locale, + ]; + $categoryId = $faker->numberBetween(1); + + $this->assertEndpointCalled(function () use ($params, $categoryId) { + $this->client->helpCenter->categories($categoryId)->sections()->create($params); + }, "help_center/categories/{$categoryId}/sections.json", 'POST', [ + 'postFields' => ['section' => $params] + ]); + } }