33namespace Protechstudio \PrestashopWebService ;
44
55use PrestaShopWebservice as PSLibrary ;
6+ use SimpleXMLElement ;
67
78class PrestashopWebService extends PSLibrary
89{
910
11+ /**
12+ * @param $options
13+ * @param bool $assoc
14+ * @return mixed
15+ * @throws \PrestaShopWebserviceException
16+ */
17+ public function getJson ($ options , $ assoc = false )
18+ {
19+ $ options = $ this ->requestJsonOutput ($ options );
20+
21+ return json_decode ($ this ->get ($ options ), $ assoc );
22+ }
23+
24+ /**
25+ * @param $options
26+ * @param bool $assoc
27+ * @return mixed
28+ * @throws \PrestaShopWebserviceException
29+ */
30+ public function headJson ($ options , $ assoc = false )
31+ {
32+ $ options = $ this ->requestJsonOutput ($ options );
33+
34+ return json_decode ($ this ->head ($ options ), $ assoc );
35+ }
36+
37+ /**
38+ * @param $options
39+ * @param bool $assoc
40+ * @return mixed
41+ * @throws \PrestaShopWebserviceException
42+ */
43+ public function addJson ($ options , $ assoc = false )
44+ {
45+ $ options = $ this ->requestJsonOutput ($ options );
46+
47+ return json_decode ($ this ->add ($ options ), $ assoc );
48+ }
49+
50+ /**
51+ * @param $options
52+ * @param bool $assoc
53+ * @return mixed
54+ * @throws \PrestaShopWebserviceException
55+ */
56+ public function editJson ($ options , $ assoc = false )
57+ {
58+ $ options = $ this ->requestJsonOutput ($ options );
59+
60+ return json_decode ($ this ->edit ($ options ), $ assoc );
61+ }
62+
63+ /**
64+ * @param $options
65+ * @param bool $assoc
66+ * @return mixed
67+ */
68+ public function deleteJson ($ options , $ assoc = false )
69+ {
70+ $ options = $ this ->requestJsonOutput ($ options );
71+
72+ return json_decode ($ this ->delete ($ options ), $ assoc );
73+ }
74+
75+ /**
76+ * @param $options
77+ * @return mixed
78+ */
79+ private function requestJsonOutput ($ options )
80+ {
81+ $ options ['output_format ' ] = 'JSON ' ;
82+
83+ return $ options ;
84+ }
85+
86+ /**
87+ * @param $resource
88+ * @return SimpleXMLElement
89+ * @throws \PrestaShopWebserviceException
90+ */
91+ public function getSchema ($ resource )
92+ {
93+ return $ this ->get (['resource ' => $ resource . '?schema=blank ' ]);
94+ }
95+
96+ /**
97+ * Fill the provided schema with an associative array data
98+ *
99+ * @param SimpleXMLElement $xmlSchema
100+ * @param array $data
101+ * @return SimpleXMLElement
102+ */
103+ public function fillSchema (SimpleXMLElement $ xmlSchema , $ data )
104+ {
105+ $ toBeRemoved = array ();
106+ $ resource = $ xmlSchema ->children ()->children ();
107+ foreach ($ resource as $ key => $ value ) {
108+ if (array_key_exists ($ key , $ data )) {
109+ if (property_exists ($ resource ->$ key , 'language ' )) {
110+ $ this ->fillLanguageNode ($ resource ->$ key , $ data [$ key ]);
111+ } else {
112+ $ resource ->$ key = $ data [$ key ];
113+ }
114+ } else {
115+ $ toBeRemoved [] = $ key ;
116+ }
117+ }
118+ foreach ($ toBeRemoved as $ key ) {
119+ unset($ resource ->$ key );
120+ }
121+
122+ return $ xmlSchema ;
123+ }
124+
125+ /**
126+ * @param string|array $data
127+ * @param $languageId
128+ * @return string
129+ */
130+ private function getLanguageValue ($ data , $ languageId )
131+ {
132+ if (is_string ($ data )) {
133+ return $ data ;
134+ }
135+
136+ if (array_key_exists ($ languageId , $ data )) {
137+ return $ data [$ languageId ];
138+ } else {
139+ return $ data [1 ];
140+ }
141+ }
142+
143+ /**
144+ * @param $node
145+ * @param $data
146+ */
147+ private function fillLanguageNode ($ node , $ data )
148+ {
149+ for ($ i = 0 ; $ i < count ($ node ->language ); $ i ++) {
150+ $ node ->language [$ i ] = $ this ->getLanguageValue ($ data , (int )$ node ->language [$ i ]['id ' ]->__toString ());
151+ }
152+ }
10153}
0 commit comments