Skip to content

Commit

Permalink
IBX-4123: [Backport] Added /languages and /languages/{code} endpo…
Browse files Browse the repository at this point in the history
…ints

(cherry picked from commit 674e612e176bc85556513a716556d643f81d722d)
  • Loading branch information
Steveb-p committed Nov 9, 2023
1 parent 4c8a173 commit 9f6fcce
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ ezpublish_rest_setObjectStatesForContent:
contentId: \d+


# Languages

ibexa.rest.languages.list:
path: /languages
methods: [GET]
controller: Ibexa\Rest\Server\Controller\Language::listLanguages

ibexa.rest.languages.view:
path: /languages/{languageCode}
methods: [GET]
controller: Ibexa\Rest\Server\Controller\Language::loadLanguage

# Locations


Expand Down
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ services:
- "@ezpublish.api.service.location"
tags: [controller.service_arguments]

Ibexa\Rest\Server\Controller\Language:
autowire: true
tags: [ controller.service_arguments ]

ezpublish_rest.controller.location:
class: "%ezpublish_rest.controller.location.class%"
parent: ezpublish_rest.controller.base
Expand Down
5 changes: 5 additions & 0 deletions src/bundle/Resources/config/value_object_visitors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ services:
- { name: ezpublish_rest.output.value_object_visitor, type: Symfony\Component\HttpKernel\Exception\HttpException }

# Language
Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList:
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
tags:
- { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Rest\Server\Values\LanguageList }

ezpublish_rest.output.value_object_visitor.Language:
parent: ezpublish_rest.output.value_object_visitor.base
class: EzSystems\EzPlatformRest\Server\Output\ValueObjectVisitor\Language
Expand Down
41 changes: 41 additions & 0 deletions src/lib/Server/Controller/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

Check warning on line 1 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Run code style check

Found violation(s) of type: header_comment

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Server\Controller;

use Ibexa\Contracts\Core\Repository\LanguageService;
use Ibexa\Contracts\Core\Repository\Values\Content\Language as ApiLanguage;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Values\LanguageList;
use Traversable;

final class Language extends RestController

Check failure on line 17 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Class Ibexa\Rest\Server\Controller\Language extends unknown class Ibexa\Rest\Server\Controller.

Check failure on line 17 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Class Ibexa\Rest\Server\Controller\Language extends unknown class Ibexa\Rest\Server\Controller.

Check failure on line 17 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Class Ibexa\Rest\Server\Controller\Language extends unknown class Ibexa\Rest\Server\Controller.

Check failure on line 17 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Class Ibexa\Rest\Server\Controller\Language extends unknown class Ibexa\Rest\Server\Controller.
{
private LanguageService $languageService;

Check failure on line 19 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Property Ibexa\Rest\Server\Controller\Language::$languageService has unknown class Ibexa\Contracts\Core\Repository\LanguageService as its type.

Check failure on line 19 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Property Ibexa\Rest\Server\Controller\Language::$languageService has unknown class Ibexa\Contracts\Core\Repository\LanguageService as its type.

Check failure on line 19 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Property Ibexa\Rest\Server\Controller\Language::$languageService has unknown class Ibexa\Contracts\Core\Repository\LanguageService as its type.

Check failure on line 19 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Property Ibexa\Rest\Server\Controller\Language::$languageService has unknown class Ibexa\Contracts\Core\Repository\LanguageService as its type.

public function __construct(LanguageService $languageService)

Check failure on line 21 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Parameter $languageService of method Ibexa\Rest\Server\Controller\Language::__construct() has invalid type Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 21 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Parameter $languageService of method Ibexa\Rest\Server\Controller\Language::__construct() has invalid type Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 21 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Parameter $languageService of method Ibexa\Rest\Server\Controller\Language::__construct() has invalid type Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 21 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Parameter $languageService of method Ibexa\Rest\Server\Controller\Language::__construct() has invalid type Ibexa\Contracts\Core\Repository\LanguageService.
{
$this->languageService = $languageService;
}

public function listLanguages(): LanguageList
{
$languages = $this->languageService->loadLanguages();

Check failure on line 28 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Call to method loadLanguages() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 28 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Call to method loadLanguages() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 28 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Call to method loadLanguages() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 28 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Call to method loadLanguages() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

if ($languages instanceof Traversable) {
$languages = iterator_to_array($languages);
}

return new LanguageList($languages);
}

public function loadLanguage(string $languageCode): ApiLanguage

Check failure on line 37 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Method Ibexa\Rest\Server\Controller\Language::loadLanguage() has invalid return type Ibexa\Contracts\Core\Repository\Values\Content\Language.

Check failure on line 37 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Method Ibexa\Rest\Server\Controller\Language::loadLanguage() has invalid return type Ibexa\Contracts\Core\Repository\Values\Content\Language.

Check failure on line 37 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Method Ibexa\Rest\Server\Controller\Language::loadLanguage() has invalid return type Ibexa\Contracts\Core\Repository\Values\Content\Language.

Check failure on line 37 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Method Ibexa\Rest\Server\Controller\Language::loadLanguage() has invalid return type Ibexa\Contracts\Core\Repository\Values\Content\Language.
{
return $this->languageService->loadLanguage($languageCode);

Check failure on line 39 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Call to method loadLanguage() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 39 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Call to method loadLanguage() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 39 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Call to method loadLanguage() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.

Check failure on line 39 in src/lib/Server/Controller/Language.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Call to method loadLanguage() on an unknown class Ibexa\Contracts\Core\Repository\LanguageService.
}
}
13 changes: 10 additions & 3 deletions src/lib/Server/Output/ValueObjectVisitor/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ public function visit(Visitor $visitor, Generator $generator, $data): void

private function visitLanguageAttributes(Visitor $visitor, Generator $generator, LanguageValue $language): void
{
$generator->valueElement('languageId', $language->id);
$generator->valueElement('languageCode', $language->languageCode);
$generator->valueElement('name', $language->name);
$generator->attribute(
'href',
$this->router->generate(
'ibexa.rest.languages.view',
['languageCode' => $language->getLanguageCode()],
),
);
$generator->valueElement('languageId', $language->getId());
$generator->valueElement('languageCode', $language->getLanguageCode());
$generator->valueElement('name', $language->getName());
}
}
35 changes: 35 additions & 0 deletions src/lib/Server/Output/ValueObjectVisitor/LanguageList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

Check warning on line 1 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Run code style check

Found violation(s) of type: header_comment

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Server\Output\ValueObjectVisitor;

use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
use Ibexa\Contracts\Rest\Output\Visitor;

final class LanguageList extends ValueObjectVisitor

Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Class Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList extends unknown class Ibexa\Contracts\Rest\Output\ValueObjectVisitor.

Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Class Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList extends unknown class Ibexa\Contracts\Rest\Output\ValueObjectVisitor.

Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Class Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList extends unknown class Ibexa\Contracts\Rest\Output\ValueObjectVisitor.

Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Class Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList extends unknown class Ibexa\Contracts\Rest\Output\ValueObjectVisitor.
{
/**
* @param \Ibexa\Rest\Server\Values\LanguageList $data
*/
public function visit(Visitor $visitor, Generator $generator, $data): void

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Parameter $generator of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Generator.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.0)

Parameter $visitor of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Visitor.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Parameter $generator of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Generator.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (8.2)

Parameter $visitor of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Visitor.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Parameter $generator of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Generator.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.4)

Parameter $visitor of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Visitor.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Parameter $generator of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Generator.

Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php

View workflow job for this annotation

GitHub Actions / Unit tests (7.3)

Parameter $visitor of method Ibexa\Rest\Server\Output\ValueObjectVisitor\LanguageList::visit() has invalid type Ibexa\Contracts\Rest\Output\Visitor.
{
$generator->startObjectElement('LanguageList');
$visitor->setHeader('Content-Type', $generator->getMediaType('LanguageList'));

$generator->attribute('href', $this->router->generate('ibexa.rest.languages.list'));

$generator->startList('Language');
foreach ($data->languages as $language) {
$visitor->visitValueObject($language);
}
$generator->endList('Language');

$generator->endObjectElement('LanguageList');
}
}
25 changes: 25 additions & 0 deletions src/lib/Server/Values/LanguageList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

Check warning on line 1 in src/lib/Server/Values/LanguageList.php

View workflow job for this annotation

GitHub Actions / Run code style check

Found violation(s) of type: header_comment

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Server\Values;

use Ibexa\Rest\Value as RestValue;

final class LanguageList extends RestValue
{
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Language[] */
public array $languages;

/**
* @param array<\Ibexa\Contracts\Core\Repository\Values\Content\Language> $languages
*/
public function __construct(array $languages)
{
$this->languages = $languages;
}
}
38 changes: 38 additions & 0 deletions tests/bundle/Functional/JsonSchema/Language.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Language": {
"type": "object",
"properties": {
"_media-type": {
"type": "string"
},
"_href": {
"type": "string"
},
"languageId": {
"type": "integer"
},
"languageCode": {
"type": "string",
"minLength": 1,
"pattern": "^[[:alnum:]_]+"
},
"name": {
"type": "string"
}
},
"required": [
"_media-type",
"_href",
"languageId",
"languageCode",
"name"
]
}
},
"required": [
"Language"
]
}
56 changes: 56 additions & 0 deletions tests/bundle/Functional/JsonSchema/LanguageList.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"LanguageList": {
"type": "object",
"properties": {
"_media-type": {
"type": "string"
},
"_href": {
"type": "string"
},
"Language": {
"type":"array",
"items": {
"properties": {
"_media-type": {
"type": "string"
},
"_href": {
"type": "string"
},
"languageId": {
"type": "integer"
},
"languageCode": {
"type": "string",
"minLength": 1,
"pattern": "^[[:alnum:]_]+"
},
"name": {
"type": "string"
}
},
"required": [
"_media-type",
"_href",
"languageId",
"languageCode",
"name"
]
}
}
},
"required": [
"_media-type",
"_href",
"Language"
]
}
},
"required": [
"LanguageList"
]
}
64 changes: 64 additions & 0 deletions tests/bundle/Functional/LanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

Check warning on line 1 in tests/bundle/Functional/LanguageTest.php

View workflow job for this annotation

GitHub Actions / Run code style check

Found violation(s) of type: header_comment

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Bundle\Rest\Functional;

use Ibexa\Tests\Bundle\Rest\Functional\TestCase as RESTFunctionalTestCase;

final class LanguageTest extends RESTFunctionalTestCase
{
use ResourceAssertionsTrait;

private const SNAPSHOT_DIR = __DIR__ . '/_snapshot';

public function testLanguageListJson(): void
{
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages', '', 'LanguageList+json');
$response = $this->sendHttpRequest($request);

self::assertHttpResponseCodeEquals($response, 200);
$content = $response->getBody()->getContents();
self::assertJson($content);

self::assertJsonResponseIsValid($content, 'LanguageList');
self::assertResponseMatchesJsonSnapshot($content, self::SNAPSHOT_DIR . '/LanguageList.json');
}

public function testLanguageListXml(): void
{
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages');
$response = $this->sendHttpRequest($request);

self::assertHttpResponseCodeEquals($response, 200);
$content = $response->getBody()->getContents();
self::assertResponseMatchesXmlSnapshot($content, self::SNAPSHOT_DIR . '/LanguageList.xml');
}

public function testLanguageViewJson(): void
{
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages/eng-GB', '', 'LanguageList+json');
$response = $this->sendHttpRequest($request);

self::assertHttpResponseCodeEquals($response, 200);
$content = $response->getBody()->getContents();
self::assertJson($content);

self::assertJsonResponseIsValid($content, 'Language');
self::assertResponseMatchesJsonSnapshot($content, self::SNAPSHOT_DIR . '/Language.json');
}

public function testLanguageViewXml(): void
{
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages/eng-GB');
$response = $this->sendHttpRequest($request);

self::assertHttpResponseCodeEquals($response, 200);
$content = $response->getBody()->getContents();
self::assertResponseMatchesXmlSnapshot($content, self::SNAPSHOT_DIR . '/Language.xml');
}
}
Loading

0 comments on commit 9f6fcce

Please sign in to comment.