1
0

Updated for REST changes which allow PUT and POST requests to

modify members, not just entity.

- Example code updated to demonstrate how to reorder

- Added Gallery3::set_members()

- Fixed a weird array_diff problem -- not sure how this was working before

- JSON encode any complex values in our post data
This commit is contained in:
Bharat Mediratta 2010-04-04 11:58:05 -07:00
parent 199417ff77
commit bd28e04f84
2 changed files with 33 additions and 13 deletions

View File

@ -69,7 +69,7 @@ class Gallery3 {
}
/**
* Set a value on the remote resource. You must call save for it to take effect.
* Set a value on the remote resource's entity. You must call save for it to take effect.
*
* @param string key
* @param string value
@ -81,6 +81,18 @@ class Gallery3 {
return $this;
}
/**
* Replace the members for the remote resource
*
* @param array members
* @return object Gallery3
* @chainable
*/
public function set_members($members) {
$this->data->members = $members;
return $this;
}
/**
* Attach a file to the remote resource.
*
@ -100,7 +112,7 @@ class Gallery3 {
*/
public function create($url, $token) {
$response = Gallery3_Helper::request(
"post", $url, $token, $this->data->entity, $this->file);
"post", $url, $token, array("entity" => $this->data->entity), $this->file);
$this->url = $response->url;
$this->token = $token;
return $this->load();
@ -115,7 +127,8 @@ class Gallery3 {
public function save() {
$response = Gallery3_Helper::request(
"put", $this->url, $this->token,
array_diff($this->original_entity, (array)$this->data->entity));
array("entity" => array_diff((array)$this->data->entity, $this->original_entity),
"members" => $this->data->members));
return $this->load();
}
@ -154,7 +167,7 @@ class Gallery3_Helper {
$req->addHeader("X-Gallery-Request-Key", $token);
}
foreach ($params as $key => $value) {
$req->addPostData($key, $value);
$req->addPostData($key, is_string($value) ? $value : json_encode($value));
}
if ($file) {
$req->addFile("file", $file, mime_content_type($file));

View File

@ -19,7 +19,6 @@ $tag = Gallery3::factory()
->create($tags->url, $auth);
alert("Created tag: <b>{$tag->url}</b>");
$album = Gallery3::factory()
->set("type", "album")
->set("name", "Sample Album")
@ -34,16 +33,24 @@ $album
->save();
alert("New title: <b>{$album->data->entity->title}</b>");
$photo = Gallery3::factory()
->set("type", "photo")
->set("name", "Sample Photo.png")
->set("title", "Sample Photo")
->set_file("gallery.png")
->create($album->url, $auth);
alert("Uploaded photo: <b>{$photo->url}</b>");
for ($i = 0; $i < 2; $i++) {
$photo = Gallery3::factory()
->set("type", "photo")
->set("name", "Sample Photo.png")
->set("title", "Sample Photo")
->set_file("gallery.png")
->create($album->url, $auth);
alert("Uploaded photo: <b>{$photo->url}</b>");
}
$album->load();
alert("Album members: <b>" . join(", ", $album->data->members) . "</b>");
alert("Reorder the album");
$album
->set_members(array($album->data->members[1], $album->data->members[0]))
->set("sort_column", "weight")
->save();
alert("New order: <b>" . join(", ", $album->data->members) . "</b>");
alert("Search for the photo");
$photos = Gallery3::factory($root->url, $auth)