Segue am anexo a foto do resultado do meu XML, e ó código:
<?php
namespace Alura\Cursos\Controller;
use SimpleXMLElement;
use Nyholm\Psr7\Response;
use Alura\Cursos\Entity\Curso;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class CursosEmXml implements RequestHandlerInterface
{
/**
* @var ObjectRepository
*/
private $repositorioDeCursos;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repositorioDeCursos = $entityManager
->getRepository(Curso::class);
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
/** @var Curso[] $cursos */
$cursos = $this->repositorioDeCursos->findAll();
$cursosEmXml = new \SimpleXMLElement('<cursos/>');
foreach ($cursos as $curso) {
$cursoEmXml = $cursosEmXml->addChild('curso');
$cursoEmXml->addChild('id', $curso->getId());
$cursoEmXml->addChild('descricao', $curso->getDescricao());
}
return new Response(
200,
['Content-Type' => 'application/xml'],
$cursosEmXml->asXML()
);
}
}