@@ -21,6 +21,8 @@ public function getDefaultCharset();
21
21
public function beginTransaction ();
22
22
public function commitTransaction ();
23
23
public function rollbackTransaction ();
24
+ public function jsonEncode ($ object );
25
+ public function jsonDecode ($ string );
24
26
}
25
27
26
28
class MySQL implements DatabaseInterface {
@@ -217,6 +219,13 @@ public function rollbackTransaction() {
217
219
//return mysqli_rollback($this->db);
218
220
}
219
221
222
+ public function jsonEncode ($ object ) {
223
+ return json_encode ($ object );
224
+ }
225
+
226
+ public function jsonDecode ($ string ) {
227
+ return json_decode ($ string );
228
+ }
220
229
}
221
230
222
231
class PostgreSQL implements DatabaseInterface {
@@ -450,6 +459,14 @@ public function commitTransaction() {
450
459
public function rollbackTransaction () {
451
460
return $ this ->query ('ROLLBACK ' );
452
461
}
462
+
463
+ public function jsonEncode ($ object ) {
464
+ return json_encode ($ object );
465
+ }
466
+
467
+ public function jsonDecode ($ string ) {
468
+ return json_decode ($ string );
469
+ }
453
470
}
454
471
455
472
class SQLServer implements DatabaseInterface {
@@ -715,6 +732,111 @@ public function commitTransaction() {
715
732
public function rollbackTransaction () {
716
733
return sqlsrv_rollback ($ this ->db );
717
734
}
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
+ }
718
840
}
719
841
720
842
class SQLite implements DatabaseInterface {
@@ -929,6 +1051,13 @@ public function rollbackTransaction() {
929
1051
return $ this ->query ('ROLLBACK ' );
930
1052
}
931
1053
1054
+ public function jsonEncode ($ object ) {
1055
+ return json_encode ($ object );
1056
+ }
1057
+
1058
+ public function jsonDecode ($ string ) {
1059
+ return json_decode ($ string );
1060
+ }
932
1061
}
933
1062
934
1063
class PHP_CRUD_API {
@@ -1607,7 +1736,7 @@ protected function convertInputs(&$input,$fields) {
1607
1736
$ input ->$ key = (object )array ('type ' =>'wkt ' ,'value ' =>$ input ->$ key );
1608
1737
}
1609
1738
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 );
1611
1740
}
1612
1741
}
1613
1742
}
@@ -1637,7 +1766,7 @@ protected function convertTypes($result,&$values,&$fields) {
1637
1766
$ v =base64_encode (hex2bin ($ v ));
1638
1767
}
1639
1768
else if ($ this ->db ->isJsonType ($ fields [$ i ])) {
1640
- $ v =json_decode ($ v );
1769
+ $ v =$ this -> db -> jsonDecode ($ v );
1641
1770
}
1642
1771
}
1643
1772
});
0 commit comments