Skip to content

Commit 7125c6c

Browse files
committed
Support ArrayAccess for mapped models.
This allows pluck() to be used on fetched resource collections, which is very handy when scanning.
1 parent f2e2fd2 commit 7125c6c

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

src/HasModelDataTrait.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,36 @@ public function jsonSerialize()
4949
{
5050
return $this->data;
5151
}
52+
53+
/**
54+
* Supports ArrayAccess
55+
*/
56+
public function offsetExists($offset): bool
57+
{
58+
return $this->get($offset) !== null;
59+
}
60+
61+
/**
62+
* Supports ArrayAccess
63+
*/
64+
public function offsetGet($offset)
65+
{
66+
return $this->get($offset);
67+
}
68+
69+
/**
70+
* Supports ArrayAccess
71+
*/
72+
public function offsetSet($offset, $value): void
73+
{
74+
data_set($this->data, $offset, $value);
75+
}
76+
77+
/**
78+
* Supports ArrayAccess
79+
*/
80+
public function offsetUnset($offset): void
81+
{
82+
$this->offsetSet($offset, null);
83+
}
5284
}

src/ModelInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88

99
use JsonSerializable;
10+
use ArrayAccess;
1011

11-
interface ModelInterface extends JsonSerializable
12+
interface ModelInterface extends JsonSerializable, ArrayAccess
1213
{
1314
/**
1415
* Get a model instance data item, using "dot" notation.

src/OdooClient.php

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class OdooClient
6666
// (6, _, ids)
6767
const RELATION_REPLACE_ALL_LINKS = 6;
6868

69+
const API_TYPE_COMMON = 'common';
70+
const API_TYPE_OBJECT = 'object';
71+
const API_TYPE_DB = 'db';
72+
6973
/**
7074
* Later versions of the API include a version number e.g. /xmlrpc/2/
7175
*/
@@ -162,7 +166,7 @@ public function getUserId()
162166

163167
// Fetch the user ID from the server.
164168

165-
$xmlRpcClient = $this->getXmlRpcClient('common');
169+
$xmlRpcClient = $this->getXmlRpcClient(static::API_TYPE_COMMON);
166170

167171
// Build login parameters array.
168172

@@ -278,7 +282,7 @@ public function search(
278282
$msg->addParam($this->intValue($limit));
279283
$msg->addParam($this->stringValue($order));
280284

281-
$this->response = $this->getXmlRpcClient('object')->send($msg);
285+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
282286

283287
return collect($this->responseAsNative());
284288
}
@@ -297,7 +301,7 @@ public function searchCount(
297301

298302
$msg->addParam($this->nativeToValue($criteria));
299303

300-
$this->response = $this->getXmlRpcClient('object')->send($msg);
304+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
301305

302306
return $this->valueToNative();
303307
}
@@ -314,7 +318,7 @@ public function searchRead(
314318
$order = ''
315319
) {
316320
if (version_compare('8.0', $this->serverVersion) === 1) {
317-
// Less than Odoo 8.0, so search_read is not supported.
321+
// For less than Odoo 8.0, search_read is not supported.
318322
// However, we will emulate it.
319323

320324
$ids = $this->search(
@@ -335,38 +339,20 @@ public function searchRead(
335339
$msg->addParam($this->intValue($limit));
336340
$msg->addParam($this->stringValue($order));
337341

338-
$this->response = $this->getXmlRpcClient('object')->send($msg);
339-
}
342+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
340343

341-
return $this->response;
342-
}
344+
// FIXME: these need to be mapped onto models instead of
345+
// being returned as native arrays.
343346

344-
/**
345-
* Same as searchRead but returning a native PHP array.
346-
*/
347-
public function searchReadArray(
348-
string $modelName,
349-
array $criteria = [],
350-
$offset = 0,
351-
$limit = self::DEFAULT_LIMIT,
352-
$order = ''
353-
) {
354-
$this->response = $this->searchRead(
355-
$modelName,
356-
$criteria,
357-
$offset,
358-
$limit,
359-
$order
360-
);
361-
362-
return $this->responseAsNative();
347+
return $this->responseAsNative();
348+
}
363349
}
364350

365351
/**
366352
* @param string $modelName example res.partner
367353
* @param array $instanceIds list of model instance IDs to read and return
368354
* @param array $options varies with API versions see documentation
369-
* @return Response
355+
* @return Collection of ModelInterface
370356
*/
371357
public function read(
372358
string $modelName,
@@ -381,7 +367,7 @@ public function read(
381367
$msg->addParam($this->nativeToValue($options));
382368
}
383369

384-
$this->response = $this->getXmlRpcClient('object')->send($msg);
370+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
385371

386372
$data = $this->responseAsNative();
387373

@@ -401,7 +387,7 @@ public function version()
401387
{
402388
$msg = new Request('version');
403389

404-
$this->response = $this->getXmlRpcClient('common')->send($msg);
390+
$this->response = $this->getXmlRpcClient(static::API_TYPE_COMMON)->send($msg);
405391

406392
return $this->responseAsNative();
407393
}
@@ -417,7 +403,7 @@ public function create(string $modelName, array $fields)
417403

418404
$msg->addParam($this->nativeToValue($fields));
419405

420-
$this->response = $this->getXmlRpcClient('object')->send($msg);
406+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
421407

422408
return $this->responseAsNative();
423409
}
@@ -434,7 +420,7 @@ public function write(string $modelName, int $resourceId, array $fields)
434420
$msg->addParam($this->nativeToValue([$resourceId]));
435421
$msg->addParam($this->nativeToValue($fields));
436422

437-
$this->response = $this->getXmlRpcClient('object')->send($msg);
423+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
438424

439425
return $this->responseAsNative();
440426
}
@@ -450,7 +436,7 @@ public function unlink(string $modelName, int $resourceId)
450436

451437
$msg->addParam($this->nativeToValue([$resourceId]));
452438

453-
$this->response = $this->getXmlRpcClient('object')->send($msg);
439+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
454440

455441
return $this->responseAsNative();
456442
}
@@ -465,7 +451,7 @@ public function fieldsGet(string $modelName)
465451
{
466452
$msg = $this->getBaseObjectRequest($modelName, 'fields_get');
467453

468-
$this->response = $this->getXmlRpcClient('object')->send($msg);
454+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
469455

470456
return $this->responseAsNative();
471457
}
@@ -625,7 +611,7 @@ public function load(string $modelName, iterable $records)
625611
$msg->addParam($this->nativeToValue($group['keys']));
626612
$msg->addParam($this->nativeToValue($group['records']));
627613

628-
$this->response = $this->getXmlRpcClient('object')->send($msg);
614+
$this->response = $this->getXmlRpcClient(static::API_TYPE_OBJECT)->send($msg);
629615

630616
$groupResult = $this->responseAsNative();
631617

0 commit comments

Comments
 (0)