Skip to content

Commit e6a0722

Browse files
Fixed composer dependency
Added helper methods to the PrestashopWebService class
1 parent 58e0e07 commit e6a0722

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
}
1515
},
1616
"require": {
17-
"prestashop/prestashop-webservice-lib": "dev-master"
17+
"prestashop/prestashop-webservice-lib": "dev-master#efa5a1b3de471a6a37a4be2ff75147e9e5fea010"
1818
}
1919
}

src/PrestashopWebService.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,151 @@
33
namespace Protechstudio\PrestashopWebService;
44

55
use PrestaShopWebservice as PSLibrary;
6+
use SimpleXMLElement;
67

78
class 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
}

src/PrestashopWebServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
class PrestashopWebServiceProvider extends ServiceProvider
99
{
1010

11+
protected $defer = true;
12+
1113
/**
1214
* Bootstrap the application services.
1315
*
@@ -33,6 +35,11 @@ public function register()
3335
});
3436
}
3537

38+
public function provides()
39+
{
40+
return ['Protechstudio\PrestashopWebService\PrestashopWebService'];
41+
}
42+
3643
private function publish()
3744
{
3845
$this->publishes([

0 commit comments

Comments
 (0)