@@ -21,6 +21,8 @@ public function getDefaultCharset();
2121 public function beginTransaction ();
2222 public function commitTransaction ();
2323 public function rollbackTransaction ();
24+ public function jsonEncode ($ object );
25+ public function jsonDecode ($ string );
2426}
2527
2628class MySQL implements DatabaseInterface {
@@ -217,6 +219,13 @@ public function rollbackTransaction() {
217219 //return mysqli_rollback($this->db);
218220 }
219221
222+ public function jsonEncode ($ object ) {
223+ return json_encode ($ object );
224+ }
225+
226+ public function jsonDecode ($ string ) {
227+ return json_decode ($ string );
228+ }
220229}
221230
222231class PostgreSQL implements DatabaseInterface {
@@ -450,6 +459,14 @@ public function commitTransaction() {
450459 public function rollbackTransaction () {
451460 return $ this ->query ('ROLLBACK ' );
452461 }
462+
463+ public function jsonEncode ($ object ) {
464+ return json_encode ($ object );
465+ }
466+
467+ public function jsonDecode ($ string ) {
468+ return json_decode ($ string );
469+ }
453470}
454471
455472class SQLServer implements DatabaseInterface {
@@ -715,6 +732,111 @@ public function commitTransaction() {
715732 public function rollbackTransaction () {
716733 return sqlsrv_rollback ($ this ->db );
717734 }
735+
736+ public function jsonEncode ($ object ) {
737+ $ t = function ($ v ) {
738+ switch (gettype ($ v )) {
739+ case 'boolean ' : return 'boolean ' ;
740+ case 'integer ' : return 'number ' ;
741+ case 'double ' : return 'number ' ;
742+ case 'string ' : return 'string ' ;
743+ case 'array ' : return 'array ' ;
744+ case 'object ' : return 'object ' ;
745+ case 'NULL ' : return 'null ' ;
746+ }
747+ };
748+ $ a = $ object ;
749+ $ c = new SimpleXMLElement ('<root/> ' );
750+ $ f = function ($ f ,$ c ,$ a ,$ s =false ) use ($ t ) {
751+ $ c ->addAttribute ('type ' , $ t ($ a ));
752+ if (is_scalar ($ a )||is_null ($ a )) {
753+ if (is_bool ($ a )){
754+ $ c [0 ] = $ a ?'true ' :'false ' ;
755+ } else {
756+ $ c [0 ] = $ a ;
757+ }
758+ } else {
759+ foreach ($ a as $ k =>$ v ) {
760+ if ($ k =='__type ' && is_object ($ a )) {
761+ $ c ->addAttribute ('__type ' , $ v );
762+ } else {
763+ if (is_object ($ v )) {
764+ $ ch =$ c ->addChild ($ s ?'item ' :$ k );
765+ $ f ($ f ,$ ch ,$ v );
766+ } else if (is_array ($ v )) {
767+ $ ch =$ c ->addChild ($ s ?'item ' :$ k );
768+ $ f ($ f ,$ ch ,$ v ,true );
769+ } else if (is_bool ($ v )) {
770+ $ ch =$ c ->addChild ($ s ?'item ' :$ k ,$ v ?'true ' :'false ' );
771+ $ ch ->addAttribute ('type ' , $ t ($ v ));
772+ } else {
773+ $ ch =$ c ->addChild ($ s ?'item ' :$ k ,$ v );
774+ $ ch ->addAttribute ('type ' , $ t ($ v ));
775+ }
776+ }
777+ }
778+ }
779+ };
780+ $ f ($ f ,$ c ,$ a ,$ t ($ a )=='array ' );
781+ return trim ($ c ->asXML ());
782+ }
783+
784+ public function jsonDecode ($ string ) {
785+ $ p = function ($ p ,$ n ) {
786+ foreach ($ n ->childNodes as $ node ) {
787+ if ($ node ->hasChildNodes ()) {
788+ $ p ($ p ,$ node );
789+ } else {
790+ if ($ n ->hasAttributes () && strlen ($ n ->nodeValue )){
791+ $ n ->setAttribute ('value ' , $ node ->textContent );
792+ $ node ->nodeValue = '' ;
793+ }
794+ }
795+ }
796+ };
797+ $ dom = new DOMDocument ();
798+ $ dom ->loadXML ($ string );
799+ $ p ($ p ,$ dom );
800+ $ xml = simplexml_load_string ($ dom ->saveXML ());
801+ $ a = json_decode (json_encode ($ xml ));
802+ $ f = function ($ f ,&$ a ) {
803+ foreach ($ a as $ k =>&$ v ) {
804+ if ($ k ==='@attributes ' ) {
805+ if (isset ($ v ->__type ) && is_object ($ a )) {
806+ $ a ->__type = $ v ->__type ;
807+ }
808+ if ($ v ->type =='null ' ) {
809+ $ a = null ;
810+ return ;
811+ }
812+ if ($ v ->type =='boolean ' ) {
813+ $ b = substr (strtolower ($ v ->value [0 ]),0 ,1 );
814+ $ a = in_array ($ b ,array ('1 ' ,'t ' ));
815+ return ;
816+ }
817+ if ($ v ->type =='number ' ) {
818+ $ a = $ v ->value +0 ;
819+ return ;
820+ }
821+ if ($ v ->type =='string ' ) {
822+ $ a = $ v ->value ;
823+ return ;
824+ }
825+ unset($ a ->$ k );
826+ } else {
827+ if (is_object ($ v )) {
828+ $ f ($ f ,$ v );
829+ } else if (is_array ($ v )) {
830+ $ f ($ f ,$ v );
831+ $ a = $ v ;
832+ return ;
833+ }
834+ }
835+ }
836+ };
837+ $ f ($ f ,$ a );
838+ return $ a ;
839+ }
718840}
719841
720842class SQLite implements DatabaseInterface {
@@ -929,6 +1051,13 @@ public function rollbackTransaction() {
9291051 return $ this ->query ('ROLLBACK ' );
9301052 }
9311053
1054+ public function jsonEncode ($ object ) {
1055+ return json_encode ($ object );
1056+ }
1057+
1058+ public function jsonDecode ($ string ) {
1059+ return json_decode ($ string );
1060+ }
9321061}
9331062
9341063class PHP_CRUD_API {
@@ -1607,7 +1736,7 @@ protected function convertInputs(&$input,$fields) {
16071736 $ input ->$ key = (object )array ('type ' =>'wkt ' ,'value ' =>$ input ->$ key );
16081737 }
16091738 if (isset ($ input ->$ key ) && $ input ->$ key && $ this ->db ->isJsonType ($ field )) {
1610- $ input ->$ key = ( object ) array ( ' type ' => ' json ' , ' value ' => json_encode ($ input ->$ key) );
1739+ $ input ->$ key = $ this -> db -> jsonEncode ($ input ->$ key );
16111740 }
16121741 }
16131742 }
@@ -1637,7 +1766,7 @@ protected function convertTypes($result,&$values,&$fields) {
16371766 $ v =base64_encode (hex2bin ($ v ));
16381767 }
16391768 else if ($ this ->db ->isJsonType ($ fields [$ i ])) {
1640- $ v =json_decode ($ v );
1769+ $ v =$ this -> db -> jsonDecode ($ v );
16411770 }
16421771 }
16431772 });
0 commit comments