Skip to content

Commit 8dd8942

Browse files
committed
Added converter from Value object to navitive PHP arrays and scalers
1 parent 7659da0 commit 8dd8942

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"php": ">= 7.0.0",
13-
"phpxmlrpc/phpxmlrpc": "~4.0"
13+
"phpxmlrpc/phpxmlrpc": "~4.3"
1414
},
1515
"autoload": {
1616
"psr-4": {

src/OdooClient.php

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpXmlRpc\Client;
1010
use PhpXmlRpc\Request;
1111
use PhpXmlRpc\Value;
12+
use PhpXmlRpc\Response;
1213

1314
use Exception;
1415

@@ -188,6 +189,13 @@ public function nullValue()
188189
/**
189190
* Example:
190191
* OdooApi::getClient()->search('res.partner', $criteria, 0, 10)
192+
*
193+
* @param string $modelName example res.partner
194+
* @param array $criteria nested array of search criteria (Polish notation logic)
195+
* @param int $offset
196+
* @param int $limit
197+
* @param string $order comma-separated list of fields
198+
* @return Response
191199
*/
192200
public function search(
193201
string $modelName,
@@ -198,11 +206,11 @@ public function search(
198206
) {
199207
$msg = $this->getBaseObjectRequest($modelName, 'search');
200208

201-
$msg->addParam($this->objectifyArray($criteria));
209+
$msg->addParam($this->nativeToValue($criteria));
202210

203-
$msg->addParam($this->intValue($offset)); // offset
204-
$msg->addParam($this->intValue($limit)); // limit
205-
$msg->addParam($this->stringValue($order)); // order, CSV list
211+
$msg->addParam($this->intValue($offset));
212+
$msg->addParam($this->intValue($limit));
213+
$msg->addParam($this->stringValue($order));
206214

207215
$response = $this->getXmlRpcClient('object')->send($msg);
208216

@@ -212,14 +220,16 @@ public function search(
212220
/**
213221
* Example:
214222
* OdooApi::getClient()->search_count('res.partner', $criteria)
223+
*
224+
* @return integer
215225
*/
216226
public function searchCount(
217227
string $modelName,
218228
array $criteria = []
219229
) {
220230
$msg = $this->getBaseObjectRequest($modelName, 'search_count');
221231

222-
$msg->addParam($this->objectifyArray($criteria));
232+
$msg->addParam($this->nativeToValue($criteria));
223233

224234
$response = $this->getXmlRpcClient('object')->send($msg);
225235

@@ -239,7 +249,7 @@ public function searchRead(
239249
) {
240250
$msg = $this->getBaseObjectRequest($modelName, 'search_read');
241251

242-
$msg->addParam($this->objectifyArray($criteria));
252+
$msg->addParam($this->nativeToValue($criteria));
243253

244254
// To be fixed when we have Odoo 8 available to develop against.
245255

@@ -264,7 +274,7 @@ public function read(
264274
) {
265275
$msg = $this->getBaseObjectRequest($modelName, 'read');
266276

267-
$msg->addParam($this->objectifyArray($criteria));
277+
$msg->addParam($this->nativeToValue($criteria));
268278

269279
$response = $this->getXmlRpcClient('object')->send($msg);
270280

@@ -308,7 +318,7 @@ public function getBaseObjectRequest(
308318
* Walk through the criteria array and convert scalar values to
309319
* XML-RPC objects, and nested arrays to array and struct objects.
310320
*/
311-
public function objectifyArray($item)
321+
public function nativeToValue($item)
312322
{
313323
// If a scalar, then map to the appropriate object.
314324

@@ -338,7 +348,7 @@ public function objectifyArray($item)
338348
// If an array, then deal with the children first.
339349

340350
foreach ($item as $key => $element) {
341-
$item[$key] = $this->objectifyArray($element);
351+
$item[$key] = $this->nativeToValue($element);
342352
}
343353

344354
// Map to an array or a struct, depending on whether a numeric
@@ -350,4 +360,39 @@ public function objectifyArray($item)
350360
return $this->structValue($item);
351361
}
352362
}
363+
364+
/**
365+
* Convert a Value object into native PHP types.
366+
* Basically the reverse of nativeToValue().
367+
*
368+
* @param Value the object to convert, which may contain nested objects
369+
* @returns mixed a null, an array, a scalar, and may be nested
370+
*/
371+
public function valueToNative(Value $value)
372+
{
373+
switch ($value->kindOf()) {
374+
case 'array':
375+
$result = [];
376+
foreach ($value->getIterator() as $element) {
377+
$result[] = $this->valueToNative($element);
378+
}
379+
break;
380+
case 'struct':
381+
$result = [];
382+
foreach ($value->getIterator() as $key => $element) {
383+
$result[$key] = $this->valueToNative($element);
384+
}
385+
break;
386+
case 'scalar':
387+
return $value->scalarval();
388+
break;
389+
default:
390+
throw new Exception(sprintf(
391+
'Unexpected data type %s',
392+
$value->kindOf()
393+
));
394+
}
395+
396+
return $result;
397+
}
353398
}

0 commit comments

Comments
 (0)