Permalink
Browse files

Converted User methods to accept json

  • Loading branch information...
1 parent 30dab16 commit 2999129222836a34402ebbd0cc95a387a41793b1 @chrisbarr committed Oct 18, 2011
Showing with 63 additions and 27 deletions.
  1. +63 −27 lib/beanstalkapi.class.php
@@ -365,27 +365,27 @@ public function create_invitation($email, $first_name, $last_name)
*
* @link http://api.beanstalkapp.com/public_key.html
* @param integer $user_id [optional]
- * @return SimpleXMLElement
+ * @return SimpleXMLElement|array
*/
public function find_all_public_keys($user_id = NULL) {
if(!is_null($user_id))
- return $this->_execute_curl("public_keys.xml?user_id=" . $user_id);
+ return $this->_execute_curl("public_keys." . $this->format . "?user_id=" . $user_id);
else
- return $this->_execute_curl("public_keys.xml");
+ return $this->_execute_curl("public_keys." . $this->format);
}
/**
* Return a single public key
*
* @link http://api.beanstalkapp.com/public_key.html
* @param integer $key_id
- * @return SimpleXMLElement
+ * @return SimpleXMLElement|array
*/
public function find_single_public_key($key_id) {
if(empty($key_id))
throw new InvalidArgumentException("Public key ID required");
- return $this->_execute_curl("public_keys", $key_id . ".xml");
+ return $this->_execute_curl("public_keys", $key_id . "." . $this->format);
}
/**
@@ -395,23 +395,42 @@ public function find_single_public_key($key_id) {
* @param string $content
* @param string $name [optional]
* @param integer $user_id [optional] Defaults to current user
- * @return SimpleXMLElement
+ * @return SimpleXMLElement|array
*/
public function create_public_key($content, $name = NULL, $user_id = NULL) {
if(empty($content))
throw new InvalidArgumentException("Key content required");
- $xml = new SimpleXMLElement('<public-key></public-key>');
-
- $xml->addChild('content', $content);
-
- if(!is_null($name))
- $xml->addChild('name', $name);
-
- if(!is_null($user_id))
- $xml->addChild('user-id', $user_id);
+ if($this->format == 'xml')
+ {
+ $xml = new SimpleXMLElement('<public-key></public-key>');
+
+ $xml->addChild('content', $content);
+
+ if(!is_null($name))
+ $xml->addChild('name', $name);
+
+ if(!is_null($user_id))
+ $xml->addChild('user-id', $user_id);
+
+ $data = $xml->asXml();
+ }
+ else
+ {
+ $data_array = array('public-key' => array());
+
+ $data_array['public-key']['content'] = $content;
+
+ if(!is_null($name))
+ $data_array['public-key']['name'] = $name;
+
+ if(!is_null($user_id))
+ $data_array['public-key']['user-id'] = $user_id;
+
+ $data = json_encode($data_array);
+ }
- return $this->_execute_curl("public_keys.xml", NULL, "POST", $xml->asXml());
+ return $this->_execute_curl("public_keys." . $this->format, NULL, "POST", $data);
}
/**
@@ -420,7 +439,7 @@ public function create_public_key($content, $name = NULL, $user_id = NULL) {
* @link http://api.beanstalkapp.com/public_key.html
* @param integer $key_id
* @param array $params Accepts - content, name
- * @return SimpleXMLElement
+ * @return SimpleXMLElement|array
*/
public function update_public_key($key_id, $params = array()) {
if(empty($key_id))
@@ -429,29 +448,46 @@ public function update_public_key($key_id, $params = array()) {
if(count($params) == 0)
throw new InvalidArgumentException("Nothing to update");
- $xml = new SimpleXMLElement('<public-key></public-key>');
-
- if(!is_null($params['content']))
- $xml->addChild('content', $params['content']);
-
- if(!is_null($params['name']))
- $xml->addChild('name', $params['name']);
+ if($this->format == 'xml')
+ {
+ $xml = new SimpleXMLElement('<public-key></public-key>');
+
+ if(!is_null($params['content']))
+ $xml->addChild('content', $params['content']);
+
+ if(!is_null($params['name']))
+ $xml->addChild('name', $params['name']);
+
+ $data = $xml->asXml();
+ }
+ else
+ {
+ $data_array = array('public-key' => array());
+
+ if(!is_null($params['content']))
+ $data_array['public-key']['content'] = $params['content'];
+
+ if(!is_null($params['name']))
+ $data_array['public-key']['name'] = $params['name'];
+
+ $data = json_encode($data_array);
+ }
- return $this->_execute_curl("public_keys", $key_id . ".xml", "PUT", $xml->asXml());
+ return $this->_execute_curl("public_keys", $key_id . "." . $this->format, "PUT", $data);
}
/**
* Delete a public key - can only delete own keys unless admin
*
* @link http://api.beanstalkapp.com/public_key.html
* @param integer $key_id
- * @return SimpleXMLElement
+ * @return SimpleXMLElement|array
*/
public function delete_public_key($key_id) {
if(empty($key_id))
throw new InvalidArgumentException("Public key ID required");
- return $this->_execute_curl("public_keys", $key_id . ".xml", "DELETE");
+ return $this->_execute_curl("public_keys", $key_id . "." . $this->format, "DELETE");
}

0 comments on commit 2999129

Please sign in to comment.