Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-4123: [Backport] Added /languages and /languages/{code} endpoints #107

Merged
merged 11 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-symfony": "^1.3",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-webmozart-assert": "^1.2"
"phpstan/phpstan-webmozart-assert": "^1.2",
"justinrainbow/json-schema": "^5.2"
},
"config": {
"allow-plugins": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ parameters:

-
message: "#^Cannot access offset mixed on EzSystems\\\\EzPlatformRest\\\\Input\\\\Parser\\.$#"
count: 2
count: 1
path: src/lib/Input/ParsingDispatcher.php

-
Expand Down
1 change: 1 addition & 0 deletions phpunit-integration-rest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<php>
<env name="EZP_TEST_REST_HOST" value="localhost"/>
<env name="EZP_TEST_REST_AUTH" value="admin:publish"/>
<env name="IBEXA_REST_GENERATE_SNAPSHOTS" value="0"/>
</php>
<testsuites>
<testsuite name="eZ Publish REST Functional Tests">
Expand Down
13 changes: 12 additions & 1 deletion src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ 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 Expand Up @@ -583,7 +594,7 @@ ezpublish_rest_loadContentTypeFieldDefinition:
requirements:
contentTypeId: \d+
fieldDefinitionId: \d+

ibexa.rest.load_content_type_field_definition_by_identifier:
path: /content/types/{contentTypeId}/fieldDefinition/{fieldDefinitionIdentifier}
controller: ezpublish_rest.controller.content_type:loadContentTypeFieldDefinitionByIdentifier
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: ezpublish_rest.output.value_object_visitor.base
tags:
- { name: ezpublish_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
42 changes: 42 additions & 0 deletions src/lib/Server/Controller/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) eZ Systems 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 eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\Values\Content\Language as ApiLanguage;
use EzSystems\EzPlatformRest\Server\Controller as RestController;
use Ibexa\Rest\Server\Values\LanguageList;
use Traversable;

final class Language extends RestController
{
/** @var \eZ\Publish\API\Repository\LanguageService */
private $languageService;

public function __construct(LanguageService $languageService)
{
$this->languageService = $languageService;
}

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

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

return new LanguageList($languages);
}

public function loadLanguage(string $languageCode): ApiLanguage
{
return $this->languageService->loadLanguage($languageCode);
}
}
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

/**
* @copyright Copyright (C) eZ Systems 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 EzSystems\EzPlatformRest\Output\Generator;
use EzSystems\EzPlatformRest\Output\ValueObjectVisitor;
use EzSystems\EzPlatformRest\Output\Visitor;

final class LanguageList extends ValueObjectVisitor
{
/**
* @param \Ibexa\Rest\Server\Values\LanguageList $data
*/
public function visit(Visitor $visitor, Generator $generator, $data): void
{
$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

/**
* @copyright Copyright (C) eZ Systems 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 EzSystems\EzPlatformRest\Value as RestValue;

final class LanguageList extends RestValue
{
/** @var \eZ\Publish\API\Repository\Values\Content\Language[] */
public $languages;

/**
* @param array<\eZ\Publish\API\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

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

namespace EzSystems\EzPlatformRestBundle\Tests\Functional;

use EzSystems\EzPlatformRestBundle\Tests\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/ezp/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/ezp/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/ezp/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/ezp/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
Loading