@@ -6577,7 +6577,8 @@ public function toJdbc(string $type, string $size): string
6577
6577
$ jdbcType = $ this ->toJdbc ['simplified ' ][$ jdbcType ];
6578
6578
}
6579
6579
if (!isset ($ this ->valid [$ jdbcType ])) {
6580
- throw new \Exception ("Unsupported type ' $ jdbcType' for driver ' $ this ->driver ' " );
6580
+ //throw new \Exception("Unsupported type '$jdbcType' for driver '$this->driver'");
6581
+ $ jdbcType = 'clob ' ;
6581
6582
}
6582
6583
return $ jdbcType ;
6583
6584
}
@@ -8539,91 +8540,131 @@ public function __construct(Router $router, Responder $responder, array $propert
8539
8540
$ this ->reflection = $ reflection ;
8540
8541
}
8541
8542
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 );
8543
+ private function json2xml ($ json , $ types = 'null,boolean,number,string,object,array ' )
8544
+ {
8545
+ $ a = json_decode ($ json );
8546
+ $ d = new \DOMDocument ();
8547
+ $ c = $ d ->createElement ("root " );
8548
+ $ d ->appendChild ($ c );
8549
+ $ t = function ($ v ) {
8550
+ $ type = gettype ($ v );
8551
+ switch ($ type ) {
8552
+ case 'integer ' :
8553
+ return 'number ' ;
8554
+ case 'double ' :
8555
+ return 'number ' ;
8556
+ default :
8557
+ return strtolower ($ type );
8558
+ }
8559
+ };
8560
+ $ ts = explode (', ' , $ types );
8561
+ $ f = function ($ f , $ c , $ a , $ s = false ) use ($ t , $ d , $ ts ) {
8562
+ if (in_array ($ t ($ a ), $ ts )) {
8563
+ $ c ->setAttribute ('type ' , $ t ($ a ));
8564
+ }
8565
+ if ($ t ($ a ) != 'array ' && $ t ($ a ) != 'object ' ) {
8566
+ if ($ t ($ a ) == 'boolean ' ) {
8567
+ $ c ->appendChild ($ d ->createTextNode ($ a ? 'true ' : 'false ' ));
8566
8568
} else {
8567
- $ xml ->addChild ($ objectElement , (string ) $ value );
8569
+ $ c ->appendChild ($ d ->createTextNode ($ a ));
8570
+ }
8571
+ } else {
8572
+ foreach ($ a as $ k => $ v ) {
8573
+ if ($ k == '__type ' && $ t ($ a ) == 'object ' ) {
8574
+ $ c ->setAttribute ('__type ' , $ v );
8575
+ } else {
8576
+ if ($ t ($ v ) == 'object ' ) {
8577
+ $ ch = $ c ->appendChild ($ d ->createElementNS (null , $ s ? 'item ' : $ k ));
8578
+ $ f ($ f , $ ch , $ v );
8579
+ } else if ($ t ($ v ) == 'array ' ) {
8580
+ $ ch = $ c ->appendChild ($ d ->createElementNS (null , $ s ? 'item ' : $ k ));
8581
+ $ f ($ f , $ ch , $ v , true );
8582
+ } else {
8583
+ $ va = $ d ->createElementNS (null , $ s ? 'item ' : $ k );
8584
+ if ($ t ($ v ) == 'boolean ' ) {
8585
+ $ va ->appendChild ($ d ->createTextNode ($ v ? 'true ' : 'false ' ));
8586
+ } else {
8587
+ $ va ->appendChild ($ d ->createTextNode ($ v ));
8588
+ }
8589
+ $ ch = $ c ->appendChild ($ va );
8590
+ if (in_array ($ t ($ v ), $ ts )) {
8591
+ $ ch ->setAttribute ('type ' , $ t ($ v ));
8592
+ }
8593
+ }
8594
+ }
8568
8595
}
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
8596
}
8577
- $ this ->convertFromObjectToXml ($ value , $ node , $ objectElement );
8578
- }
8597
+ };
8598
+ $ f ($ f , $ c , $ a , $ t ($ a ) == 'array ' );
8599
+ return $ d ->saveXML ($ d ->documentElement );
8579
8600
}
8580
8601
8581
- private function convertFromXmlToJson ( string $ body ) /*: object */
8602
+ private function xml2json ( $ xml )
8582
8603
{
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
- }
8604
+ $ a = @dom_import_simplexml (simplexml_load_string ($ xml ));
8605
+ if (!$ a ) {
8606
+ return null ;
8605
8607
}
8606
- return $ result ;
8608
+ $ t = function ($ v ) {
8609
+ $ t = $ v ->getAttribute ('type ' );
8610
+ $ txt = $ v ->firstChild ->nodeType == XML_TEXT_NODE ;
8611
+ return $ t ?: ($ txt ? 'string ' : 'object ' );
8612
+ };
8613
+ $ f = function ($ f , $ a ) use ($ t ) {
8614
+ $ c = null ;
8615
+ if ($ t ($ a ) == 'null ' ) {
8616
+ $ c = null ;
8617
+ } else if ($ t ($ a ) == 'boolean ' ) {
8618
+ $ b = substr (strtolower ($ a ->textContent ), 0 , 1 );
8619
+ $ c = in_array ($ b , array ('1 ' , 't ' ));
8620
+ } else if ($ t ($ a ) == 'number ' ) {
8621
+ $ c = $ a ->textContent + 0 ;
8622
+ } else if ($ t ($ a ) == 'string ' ) {
8623
+ $ c = $ a ->textContent ;
8624
+ } else if ($ t ($ a ) == 'object ' ) {
8625
+ $ c = array ();
8626
+ if ($ a ->getAttribute ('__type ' )) {
8627
+ $ c ['__type ' ] = $ a ->getAttribute ('__type ' );
8628
+ }
8629
+ for ($ i = 0 ; $ i < $ a ->childNodes ->length ; $ i ++) {
8630
+ $ v = $ a ->childNodes [$ i ];
8631
+ $ c [$ v ->nodeName ] = $ f ($ f , $ v );
8632
+ }
8633
+ $ c = (object ) $ c ;
8634
+ } else if ($ t ($ a ) == 'array ' ) {
8635
+ $ c = array ();
8636
+ for ($ i = 0 ; $ i < $ a ->childNodes ->length ; $ i ++) {
8637
+ $ v = $ a ->childNodes [$ i ];
8638
+ $ c [$ i ] = $ f ($ f , $ v );
8639
+ }
8640
+ }
8641
+ return $ c ;
8642
+ };
8643
+ $ c = $ f ($ f , $ a );
8644
+ return json_encode ($ c );
8607
8645
}
8608
8646
8609
8647
public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
8610
8648
{
8611
- $ operation = RequestUtils::getOperation ($ request );
8612
-
8613
8649
parse_str ($ request ->getUri ()->getQuery (), $ params );
8614
8650
$ isXml = isset ($ params ['format ' ]) && $ params ['format ' ] == 'xml ' ;
8615
8651
if ($ isXml ) {
8616
8652
$ body = $ request ->getBody ()->getContents ();
8617
8653
if ($ body ) {
8618
- $ json = $ this ->convertFromXmlToJson ($ body );
8619
- $ request = $ request ->withParsedBody ($ json );
8654
+ $ json = $ this ->xml2json ($ body );
8655
+ $ request = $ request ->withParsedBody (json_decode ( $ json) );
8620
8656
}
8621
8657
}
8622
8658
$ response = $ next ->handle ($ request );
8623
8659
if ($ isXml ) {
8624
8660
$ body = $ response ->getBody ()->getContents ();
8625
8661
if ($ body ) {
8626
- $ xml = $ this ->convertFromJsonToXml ($ body );
8662
+ $ types = implode (', ' , $ this ->getArrayProperty ('types ' , 'null,array ' ));
8663
+ if ($ types == '' || $ types == 'all ' ) {
8664
+ $ xml = $ this ->json2xml ($ body );
8665
+ } else {
8666
+ $ xml = $ this ->json2xml ($ body , $ types );
8667
+ }
8627
8668
$ response = ResponseFactory::fromXml (ResponseFactory::OK , $ xml );
8628
8669
}
8629
8670
}
@@ -11155,10 +11196,12 @@ public static function toString(ResponseInterface $response): string
11155
11196
use Tqdev \PhpCrudApi \ResponseUtils ;
11156
11197
11157
11198
$ config = new Config ([
11199
+ // 'driver' => 'mysql',
11200
+ // 'address' => 'localhost',
11158
11201
'username ' => 'php-crud-api ' ,
11159
11202
'password ' => 'php-crud-api ' ,
11160
11203
'database ' => 'php-crud-api ' ,
11161
- 'debug ' => false
11204
+ // d 'debug' => false
11162
11205
]);
11163
11206
$ request = RequestFactory::fromGlobals ();
11164
11207
$ api = new Api ($ config );
0 commit comments