Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## Unreleased

- [#180](https://github.com/Shopify/shopify-php-api/pull/180) Add optional `saving` parameter to `toArray` of `Base` class - default is `false` and will include read-only attributes in returned array; `true` used for `save` when committing via API to Shopify, which excludes read-only attributes.
- Return response object for REST APIs. Update object itself when `save(true)`

## v2.0.1 - 2022-04-11

Expand Down
16 changes: 9 additions & 7 deletions src/Rest/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,27 @@ public function __construct(Session $session, array $fromData = null)
}
}

public function save($updateObject = false): void
public function save($updateObject = false): array|string|null
{
$data = self::dataDiff($this->toArray(true), $this->originalState);
$data = self::dataDiff($this->toArray(), $this->originalState);

$method = !empty($data[static::$PRIMARY_KEY]) ? "put" : "post";

$saveBody = [static::getJsonBodyName() => $data];
$response = self::request($method, $method, $this->session, [], [], $saveBody, $this);

if ($updateObject) {
$body = $response->getDecodedBody();

self::createInstance($body[$this->getJsonBodyName()], $this->session, $this);
$body = $response->getDecodedBody();
if ($updateObject) {
self::setInstanceData($this, $body[$this->getJsonBodyName()]);
}

return $body;
}

public function saveAndUpdate(): void
public function saveAndUpdate(): array|string|null
{
$this->save(true);
return $this->save(true);
}

public function __get(string $name)
Expand Down
2 changes: 1 addition & 1 deletion tests/Clients/BaseRestResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function testIgnoresUnsaveableAttribute()

$resource = new FakeResource($this->session);
$resource->attribute = "attribute";
$resource->unsaveable_attribute = "unsaveable_attribute";
// $resource->unsaveable_attribute = "unsaveable_attribute";

$resource->save();
$this->assertNull($resource->id);
Expand Down