From e0931aea287a76d5db2d4c39c025cdb5472dd272 Mon Sep 17 00:00:00 2001 From: Collin Haines Date: Fri, 9 Jun 2017 16:37:40 -0400 Subject: [PATCH 1/3] Proper body parameters for updateSegment() --- src/Mailchimp.php | 16 +++++++++++----- src/MailchimpLists.php | 10 ++-------- tests/MailchimpListsTest.php | 12 ++++++++---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Mailchimp.php b/src/Mailchimp.php index e709683..12766b3 100644 --- a/src/Mailchimp.php +++ b/src/Mailchimp.php @@ -277,13 +277,15 @@ protected function addBatchOperation($method, $path, $parameters = []) { * TRUE if this request should be added to pending batch operations. * @param bool $returnAssoc * TRUE to return MailChimp API response as an associative array. + * @param bool $object + * FALSE to prevent converting the parameters to a stdObject. * * @return mixed * Object or Array if $returnAssoc is TRUE. * * @throws MailchimpAPIException */ - public function request($method, $path, $tokens = NULL, $parameters = NULL, $batch = FALSE, $returnAssoc = FALSE) { + public function request($method, $path, $tokens = NULL, $parameters = NULL, $batch = FALSE, $returnAssoc = FALSE, $object = TRUE) { if (!empty($tokens)) { foreach ($tokens as $key => $value) { $path = str_replace('{' . $key . '}', $value, $path); @@ -307,10 +309,10 @@ public function request($method, $path, $tokens = NULL, $parameters = NULL, $bat } if ($this->use_curl) { - return $this->handleRequestCURL($method, $this->endpoint . $path, $options, $parameters, $returnAssoc); + return $this->handleRequestCURL($method, $this->endpoint . $path, $options, $parameters, $returnAssoc, $object); } else { - return $this->handleRequest($method, $this->endpoint . $path, $options, $parameters, $returnAssoc); + return $this->handleRequest($method, $this->endpoint . $path, $options, $parameters, $returnAssoc, $object); } } @@ -319,7 +321,7 @@ public function request($method, $path, $tokens = NULL, $parameters = NULL, $bat * * @see Mailchimp::request() */ - public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE) { + public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE, $object = TRUE) { if (!empty($parameters)) { if ($method == 'GET') { // Send parameters as query string parameters. @@ -327,7 +329,11 @@ public function handleRequest($method, $uri = '', $options = [], $parameters = [ } else { // Send parameters as JSON in request body. - $options['json'] = (object) $parameters; + if ($object) { + $options['json'] = (object) $parameters; + } else { + $options['json'] = $parameters; + } } } diff --git a/src/MailchimpLists.php b/src/MailchimpLists.php index 349f42b..15fc97c 100644 --- a/src/MailchimpLists.php +++ b/src/MailchimpLists.php @@ -387,8 +387,6 @@ public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) { * The ID of the list. * @param int $segment_id * The ID of the segment. - * @param string $name - * The name of the segment. * @param array $parameters * Associative array of optional request parameters. * @param bool $batch @@ -398,17 +396,13 @@ public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) { * * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#edit-patch_lists_list_id_segments_segment_id */ - public function updateSegment($list_id, $segment_id, $name, $parameters = [], $batch = FALSE) { + public function updateSegment($list_id, $segment_id, $parameters = [], $batch = FALSE) { $tokens = [ 'list_id' => $list_id, 'segment_id' => $segment_id, ]; - $parameters += [ - 'name' => $name, - ]; - - return $this->request('PATCH', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch); + return $this->request('POST', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch, FALSE, FALSE); } /** diff --git a/tests/MailchimpListsTest.php b/tests/MailchimpListsTest.php index 6a83863..75773bf 100644 --- a/tests/MailchimpListsTest.php +++ b/tests/MailchimpListsTest.php @@ -249,19 +249,23 @@ public function testAddSegment() { public function testUpdateSegment() { $list_id = '57afe96172'; $segment_id = '49381'; - $name = 'Updated Test Segment'; + $emails = array( + 'members_to_add' => array( + 'new-employee@mailchimp.com' + ) + ); $mc = new MailchimpLists(); - $mc->updateSegment($list_id, $segment_id, $name); + $mc->updateSegment($list_id, $segment_id, $emails); - $this->assertEquals('PATCH', $mc->getClient()->method); + $this->assertEquals('POST', $mc->getClient()->method); $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id, $mc->getClient()->uri); $this->assertNotEmpty($mc->getClient()->options['json']); $request_body = $mc->getClient()->options['json']; - $this->assertEquals($name, $request_body->name); + $this->assertEquals('new-employee@mailchimp.com', $request_body['members_to_add'][0]); } /** From 6ac972c9a060ca402d4f14e5063fd5edee6f55cb Mon Sep 17 00:00:00 2001 From: Collin Haines Date: Mon, 12 Jun 2017 08:10:34 -0400 Subject: [PATCH 2/3] Revert extra parameter in favor of passed-in array key=>value --- src/Mailchimp.php | 24 ++++++++++++++++-------- src/MailchimpLists.php | 6 +++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Mailchimp.php b/src/Mailchimp.php index 12766b3..512fd51 100644 --- a/src/Mailchimp.php +++ b/src/Mailchimp.php @@ -277,15 +277,13 @@ protected function addBatchOperation($method, $path, $parameters = []) { * TRUE if this request should be added to pending batch operations. * @param bool $returnAssoc * TRUE to return MailChimp API response as an associative array. - * @param bool $object - * FALSE to prevent converting the parameters to a stdObject. * * @return mixed * Object or Array if $returnAssoc is TRUE. * * @throws MailchimpAPIException */ - public function request($method, $path, $tokens = NULL, $parameters = NULL, $batch = FALSE, $returnAssoc = FALSE, $object = TRUE) { + public function request($method, $path, $tokens = NULL, $parameters = NULL, $batch = FALSE, $returnAssoc = FALSE) { if (!empty($tokens)) { foreach ($tokens as $key => $value) { $path = str_replace('{' . $key . '}', $value, $path); @@ -301,6 +299,7 @@ public function request($method, $path, $tokens = NULL, $parameters = NULL, $bat 'headers' => [ 'Authorization' => $this->api_user . ' ' . $this->api_key, ], + 'convert_to_object' => TRUE ]; // Add trigger error header if a debug error code has been set. @@ -308,11 +307,19 @@ public function request($method, $path, $tokens = NULL, $parameters = NULL, $bat $options['headers']['X-Trigger-Error'] = $this->debug_error_code; } + // Set the object conversion status if its passed in. + if (isset($parameters['convert_to_object'])) { + $options['convert_to_object'] = $parameters['convert_to_object']; + + // Remove status from being sent to MailChimp. + unset($parameters['convert_to_object']); + } + if ($this->use_curl) { - return $this->handleRequestCURL($method, $this->endpoint . $path, $options, $parameters, $returnAssoc, $object); + return $this->handleRequestCURL($method, $this->endpoint . $path, $options, $parameters, $returnAssoc); } else { - return $this->handleRequest($method, $this->endpoint . $path, $options, $parameters, $returnAssoc, $object); + return $this->handleRequest($method, $this->endpoint . $path, $options, $parameters, $returnAssoc); } } @@ -321,7 +328,7 @@ public function request($method, $path, $tokens = NULL, $parameters = NULL, $bat * * @see Mailchimp::request() */ - public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE, $object = TRUE) { + public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE) { if (!empty($parameters)) { if ($method == 'GET') { // Send parameters as query string parameters. @@ -329,9 +336,10 @@ public function handleRequest($method, $uri = '', $options = [], $parameters = [ } else { // Send parameters as JSON in request body. - if ($object) { + if ($options['convert_to_object']) { $options['json'] = (object) $parameters; - } else { + } + else { $options['json'] = $parameters; } } diff --git a/src/MailchimpLists.php b/src/MailchimpLists.php index 15fc97c..af50d32 100644 --- a/src/MailchimpLists.php +++ b/src/MailchimpLists.php @@ -402,7 +402,11 @@ public function updateSegment($list_id, $segment_id, $parameters = [], $batch = 'segment_id' => $segment_id, ]; - return $this->request('POST', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch, FALSE, FALSE); + $parameters += [ + 'convert_to_object' => FALSE + ]; + + return $this->request('POST', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch, FALSE); } /** From 318995ea2333ad3217bfb79876334489790784b7 Mon Sep 17 00:00:00 2001 From: Collin Haines Date: Mon, 12 Jun 2017 08:58:27 -0400 Subject: [PATCH 3/3] Remove redundant parameter --- src/MailchimpLists.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MailchimpLists.php b/src/MailchimpLists.php index af50d32..0561910 100644 --- a/src/MailchimpLists.php +++ b/src/MailchimpLists.php @@ -406,7 +406,7 @@ public function updateSegment($list_id, $segment_id, $parameters = [], $batch = 'convert_to_object' => FALSE ]; - return $this->request('POST', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch, FALSE); + return $this->request('POST', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch); } /**