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

Added option to create XML response #49

Merged
merged 1 commit into from
Feb 19, 2024
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
14 changes: 14 additions & 0 deletions Classes/Definition/ContentTypeDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// Copyright JAKOTA Design Group GmbH. All rights reserved.
declare(strict_types=1);

namespace JAKOTA\Typo3ToolBox\Definition;

class ContentTypeDefinition {
public const DEFAULT = 'default';

public const JSON = 'json';

public const XML = 'xml';
}
16 changes: 13 additions & 3 deletions Classes/Middleware/MiddlewareAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace JAKOTA\Typo3ToolBox\Middleware;

use JAKOTA\Typo3ToolBox\Definition\ContentTypeDefinition;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -64,16 +65,25 @@ public function checkRequest(string $path, callable $callable, string $method):
}
}

public function createResponse(string $string, bool $jsonOutput = true): ResponseInterface {
public function createResponse(string $string, string $contentType = ContentTypeDefinition::DEFAULT): ResponseInterface {
$typo3Version = GeneralUtility::makeInstance(Typo3Version::class);
if (version_compare($typo3Version->getVersion(), '10.1.0') >= 0) {
$response = $this->responseFactory->createResponse();
} else {
$response = new Response();
}

if ($jsonOutput) {
$response = $response->withHeader('Content-Type', 'application/json; charset=utf-8');
switch ($contentType) {
case ContentTypeDefinition::JSON:
$response = $response->withHeader('Content-Type', 'application/json; charset=utf-8');
break;

case ContentTypeDefinition::XML:
$response = $response->withHeader('Content-Type', 'application/xml; charset=utf-8');
break;

default:
break;
}

$response->getBody()->write($string);
Expand Down
Loading