Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #1: Add addMember method, a few other protected methods.

  • Loading branch information...
commit 5979d1129962f7f4635fac043c938796cd2406fe 1 parent 32201c3
@geerlingguy geerlingguy authored
Showing with 101 additions and 6 deletions.
  1. +101 −6 flocknote.php
View
107 flocknote.php
@@ -6,7 +6,7 @@
*
* The official Flocknote API integration SDK for PHP.
*
- * @version 1.0-beta1
+ * @version 1.0-beta2
* @see http://www.flocknote.com/help/api
* @author Jeff Geerling
*/
@@ -39,6 +39,13 @@ public function setUserCredentials($username, $password) {
}
/**
+ * Set alternate endpoint (only used for testing).
+ */
+ public function setEndpoint($endpoint) {
+ $this->endpoint = $endpoint;
+ }
+
+ /**
* Get the user credentials.
*
* @return (array)
@@ -152,6 +159,44 @@ public function resetParams() {
}
/**
+ * Check a given array for given required keys.
+ *
+ * Each key must exist, and a value must be given for the key, otherwise, an
+ * error message will be returned.
+ *
+ * @param $type
+ * The type of array, used for building message (e.g. 'Note' or 'Member').
+ * @param $array
+ * Array to be checked.
+ * @param $fields
+ * Array keys that should appear in $array.
+ *
+ * @return (mixed)
+ * FALSE if all keys are found. Error message (string) if a key is missing.
+ */
+ protected function checkArrayRequiredKeys($type, $array, $keys) {
+ $missing_keys = array();
+
+ foreach ($keys as $key) {
+ if (empty($array[$key])) {
+ $missing_keys[] = $key;
+ }
+ }
+
+ // Return a message dependent on the number of missing keys found.
+ $count = count($missing_keys);
+ if ($count == 0) {
+ return FALSE;
+ }
+ elseif ($count == 1) {
+ return $type . " array is missing key: " . $missing_keys[0];
+ }
+ else {
+ return $type . " array is missing keys: " . implode(', ', $missing_keys);
+ }
+ }
+
+ /**
* Send an API request with certain parameters.
*/
protected function sendRequest() {
@@ -285,8 +330,8 @@ public function getNote($id) {
*
* @return (array)
* Same array passed in, and will include 'id' in array if note was created
- * successfully. If not was not created successfully, the array will include
- * an 'error' with a message.
+ * successfully. If note was not created successfully, the array will
+ * include an 'error' with a message.
*/
public function addNote($note = array()) {
// If there's already an ID set, someone's trying to add a note that exists!
@@ -294,9 +339,14 @@ public function addNote($note = array()) {
return $note;
}
- // Make sure a list_id, title and body were passed in.
- if (empty($note['list_id']) || empty($note['title']) || empty($note['body'])) {
- $note['error'] = 'Note was missing either list_id, title, or body values.';
+ // Make sure the proper fields were passed in.
+ $required = array(
+ 'list_id',
+ 'title',
+ 'body',
+ );
+ if ($error_message = $this->checkArrayRequiredKeys('Note', $note, $required)) {
+ $note['error'] = $error_message;
return $note;
}
@@ -449,4 +499,49 @@ public function getMember($id = NULL) {
$response = $this->sendRequest();
return $this->returnResponse($response);
}
+
+ /**
+ * Add a member.
+ *
+ * @param $member
+ * Array of member information to be saved. Must include at least:
+ * - first_name
+ * - last_name
+ * - email
+ * - password
+ * - list_id
+ *
+ * @return (array)
+ * Same array passed in, and will include 'id' in array if member was
+ * created successfully. If member was not created successfully, the array
+ * will include an 'error' with a message.
+ */
+ public function addMember($member = array()) {
+ // If there's already an ID set, someone's trying to add a member that
+ // exists!
+ if (isset($member['id'])) {
+ return $member;
+ }
+
+ // Make sure the proper fields were passed in.
+ $required = array(
+ 'first_name',
+ 'last_name',
+ 'email',
+ 'password',
+ 'list_id',
+ );
+ if ($error_message = $this->checkArrayRequiredKeys('Member', $member, $required)) {
+ $member['error'] = $error_message;
+ return $member;
+ }
+
+ $this->method = 'members';
+ $this->operation = 'POST';
+
+ // Build the request.
+ $this->setBody($member);
+ $response = $this->sendRequest();
+ return $this->returnResponse($response, 201);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.