25 lines
673 B
PHP
25 lines
673 B
PHP
<?php
|
|
// JsonContentHandler.php
|
|
// Created: 2024-03-28
|
|
// Updated: 2024-03-28
|
|
|
|
namespace Index\Http\ContentHandling;
|
|
|
|
use stdClass;
|
|
use JsonSerializable;
|
|
use Index\Http\HttpResponseBuilder;
|
|
use Index\Http\Content\JsonContent;
|
|
|
|
class JsonContentHandler implements IContentHandler {
|
|
public function match(mixed $content): bool {
|
|
return is_array($content) || $content instanceof JsonSerializable || $content instanceof stdClass;
|
|
}
|
|
|
|
public function handle(HttpResponseBuilder $response, mixed $content): void {
|
|
if(!$response->hasContentType())
|
|
$response->setTypeJson();
|
|
|
|
$response->setContent(new JsonContent($content));
|
|
}
|
|
}
|