@@ -7347,7 +7347,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
73477347 use Psr \Http \Message \ResponseInterface ;
73487348 use Psr \Http \Message \ServerRequestInterface ;
73497349 use Psr \Http \Server \RequestHandlerInterface ;
7350- use Tqdev \PhpCrudApi \Controller \Responder ;
73517350 use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
73527351 use Tqdev \PhpCrudApi \Record \ErrorCode ;
73537352 use Tqdev \PhpCrudApi \ResponseFactory ;
@@ -8517,6 +8516,122 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
85178516 }
85188517}
85198518
8519+ // file: src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php
8520+ namespace Tqdev \PhpCrudApi \Middleware {
8521+
8522+ use Psr \Http \Message \ResponseInterface ;
8523+ use Psr \Http \Message \ServerRequestInterface ;
8524+ use Psr \Http \Server \RequestHandlerInterface ;
8525+ use Tqdev \PhpCrudApi \Column \ReflectionService ;
8526+ use Tqdev \PhpCrudApi \Controller \Responder ;
8527+ use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
8528+ use Tqdev \PhpCrudApi \Middleware \Router \Router ;
8529+ use Tqdev \PhpCrudApi \RequestUtils ;
8530+ use Tqdev \PhpCrudApi \ResponseFactory ;
8531+
8532+ class XmlMiddleware extends Middleware
8533+ {
8534+ private $ reflection ;
8535+
8536+ public function __construct (Router $ router , Responder $ responder , array $ properties , ReflectionService $ reflection )
8537+ {
8538+ parent ::__construct ($ router , $ responder , $ properties );
8539+ $ this ->reflection = $ reflection ;
8540+ }
8541+
8542+ private function convertFromJsonToXml (string $ body ): string
8543+ {
8544+ $ objectElement = $ this ->getProperty ('objectElement ' , 'object ' );
8545+ $ prolog = '<?xml version="1.0" encoding="UTF-8"?> ' . "\n" ;
8546+ $ xml = new \SimpleXMLElement ($ prolog . '<root></root> ' );
8547+ $ object = json_decode ($ body );
8548+ if (is_scalar ($ object )) {
8549+ $ xml = $ xml ->addChild ($ objectElement , $ object );
8550+ } else {
8551+ $ xml = $ xml ->addChild ($ objectElement );
8552+ $ this ->convertFromObjectToXml ($ object , $ xml , $ objectElement );
8553+ }
8554+ return $ prolog . $ xml ->asXML ();
8555+ }
8556+
8557+ private function convertFromObjectToXml ($ object , $ xml , string $ objectElement ): void
8558+ {
8559+ if (is_array ($ object )) {
8560+ $ xml ->addAttribute ('type ' , 'list ' );
8561+ }
8562+ foreach ($ object as $ key => $ value ) {
8563+ if (!is_array ($ value ) && !is_object ($ value )) {
8564+ if (is_object ($ object )) {
8565+ $ xml ->addChild ($ key , (string ) $ value );
8566+ } else {
8567+ $ xml ->addChild ($ objectElement , (string ) $ value );
8568+ }
8569+ continue ;
8570+ }
8571+ $ node = $ xml ;
8572+ if (is_object ($ object )) {
8573+ $ node = $ node ->addChild ($ key );
8574+ } elseif (is_object ($ value )) {
8575+ $ node = $ node ->addChild ($ objectElement );
8576+ }
8577+ $ this ->convertFromObjectToXml ($ value , $ node , $ objectElement );
8578+ }
8579+ }
8580+
8581+ private function convertFromXmlToJson (string $ body )/*: object */
8582+ {
8583+ $ objectElement = $ this ->getProperty ('objectElement ' , 'object ' );
8584+ $ prolog = '<?xml version="1.0" encoding="UTF-8"?> ' . "\n" ;
8585+ $ xml = new \SimpleXMLElement ($ prolog . $ body );
8586+ $ object = $ this ->convertFromXmlToObject ($ xml , $ objectElement );
8587+ return json_decode (json_encode ($ object ));
8588+ }
8589+
8590+ private function convertFromXmlToObject ($ xml ): array
8591+ {
8592+ $ result = [];
8593+ foreach ($ xml ->children () as $ nodeName => $ nodeValue ) {
8594+ if (count ($ nodeValue ->children ()) == 0 ) {
8595+ $ object = strVal ($ nodeValue );
8596+ } else {
8597+ $ object = $ this ->convertFromXmlToObject ($ nodeValue );
8598+ }
8599+ $ attributes = $ xml ->attributes ();
8600+ if ($ attributes ['type ' ] == 'list ' ) {
8601+ $ result [] = $ object ;
8602+ } else {
8603+ $ result [$ nodeName ] = $ object ;
8604+ }
8605+ }
8606+ return $ result ;
8607+ }
8608+
8609+ public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
8610+ {
8611+ $ operation = RequestUtils::getOperation ($ request );
8612+
8613+ parse_str ($ request ->getUri ()->getQuery (), $ params );
8614+ $ isXml = isset ($ params ['format ' ]) && $ params ['format ' ] == 'xml ' ;
8615+ if ($ isXml ) {
8616+ $ body = $ request ->getBody ()->getContents ();
8617+ if ($ body ) {
8618+ $ json = $ this ->convertFromXmlToJson ($ body );
8619+ $ request = $ request ->withParsedBody ($ json );
8620+ }
8621+ }
8622+ $ response = $ next ->handle ($ request );
8623+ if ($ isXml ) {
8624+ $ body = $ response ->getBody ()->getContents ();
8625+ if ($ body ) {
8626+ $ xml = $ this ->convertFromJsonToXml ($ body );
8627+ $ response = ResponseFactory::fromXml (ResponseFactory::OK , $ xml );
8628+ }
8629+ }
8630+ return $ response ;
8631+ }
8632+ }
8633+ }
8634+
85208635// file: src/Tqdev/PhpCrudApi/Middleware/XsrfMiddleware.php
85218636namespace Tqdev \PhpCrudApi \Middleware {
85228637
@@ -10386,6 +10501,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
1038610501 use Tqdev \PhpCrudApi \Middleware \IpAddressMiddleware ;
1038710502 use Tqdev \PhpCrudApi \Middleware \JoinLimitsMiddleware ;
1038810503 use Tqdev \PhpCrudApi \Middleware \JwtAuthMiddleware ;
10504+ use Tqdev \PhpCrudApi \Middleware \XmlMiddleware ;
1038910505 use Tqdev \PhpCrudApi \Middleware \MultiTenancyMiddleware ;
1039010506 use Tqdev \PhpCrudApi \Middleware \PageLimitsMiddleware ;
1039110507 use Tqdev \PhpCrudApi \Middleware \ReconnectMiddleware ;
@@ -10467,6 +10583,9 @@ public function __construct(Config $config)
1046710583 case 'customization ' :
1046810584 new CustomizationMiddleware ($ router , $ responder , $ properties , $ reflection );
1046910585 break ;
10586+ case 'xml ' :
10587+ new XmlMiddleware ($ router , $ responder , $ properties , $ reflection );
10588+ break ;
1047010589 }
1047110590 }
1047210591 foreach ($ config ->getControllers () as $ controller ) {
@@ -10934,6 +11053,11 @@ class ResponseFactory
1093411053 const UNPROCESSABLE_ENTITY = 422 ;
1093511054 const INTERNAL_SERVER_ERROR = 500 ;
1093611055
11056+ public static function fromXml (int $ status , string $ xml ): ResponseInterface
11057+ {
11058+ return self ::from ($ status , 'text/xml ' , $ xml );
11059+ }
11060+
1093711061 public static function fromCsv (int $ status , string $ csv ): ResponseInterface
1093811062 {
1093911063 $ response = self ::from ($ status , 'text/csv ' , $ csv );
0 commit comments