Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit 16b349b

Browse files
author
Marc Zahn
committed
Merge remote-tracking branch 'origin/develop'
2 parents 4efaeeb + 686d871 commit 16b349b

File tree

11 files changed

+577
-5
lines changed

11 files changed

+577
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ $opportunityApi = $this->apiWrapper->getOpportunityApi();
8787
$result = $opportunityApi->addOpportunity($opportunity);
8888
```
8989

90+
Updating custom fields
91+
----------------------
92+
```php
93+
$customField = new CustomField();
94+
$customField->setId('Custom field id')
95+
$customField->addChoice('Value for choices list');
96+
97+
$customFieldApi = $this->apiWrapper->getCustomFieldApi();
98+
$result = $customFieldApi->updateCustomField($customField);
99+
```
100+
90101
Info
91102
------------
92103
Right now just a few request are implemented, because the main need was to create leads.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace LooplineSystems\CloseIoApiWrapper\Api;
4+
5+
use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
6+
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
7+
use LooplineSystems\CloseIoApiWrapper\Model\CustomField;
8+
9+
class CustomFieldApi extends AbstractApi
10+
{
11+
const NAME = 'CustomFieldApi';
12+
13+
protected function initUrls()
14+
{
15+
$this->urls = [
16+
'get-customFields' => '/custom_fields/lead/',
17+
'update-customField' => '/custom_fields/lead/[:id]/'
18+
];
19+
}
20+
21+
/**
22+
* @param Curl $curl
23+
*/
24+
public function setCurl($curl)
25+
{
26+
$this->curl = $curl;
27+
}
28+
29+
/**
30+
* @return CustomField[]
31+
*/
32+
public function getAllCustomFields()
33+
{
34+
/** @var CustomField[] $customFields */
35+
$customFields = array();
36+
37+
$apiRequest = $this->prepareRequest('get-customFields');
38+
39+
/** @var CloseIoResponse $result */
40+
$result = $this->triggerGet($apiRequest);
41+
42+
if ($result->getReturnCode() == 200) {
43+
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
44+
45+
foreach ($rawData as $customField) {
46+
$customFields[] = new CustomField($customField);
47+
}
48+
}
49+
return $customFields;
50+
}
51+
52+
/**
53+
* @param CustomField $customField
54+
*
55+
* @return CustomField
56+
* @throws InvalidParamException
57+
* @throws ResourceNotFoundException
58+
*/
59+
public function updateCustomField(CustomField $customField)
60+
{
61+
if ($customField->getId() == null) {
62+
throw new InvalidParamException('When updating a custom field you must provide the custom field ID');
63+
}
64+
$id = $customField->getId();
65+
$customField->setId(null);
66+
67+
$customField = json_encode($customField);
68+
$apiRequest = $this->prepareRequest('update-customField', $customField, ['id' => $id]);
69+
$response = $this->triggerPut($apiRequest);
70+
71+
if ($response->getReturnCode() == 200 && ($response->getData() !== null)) {
72+
$customField = new CustomField($response->getData());
73+
} else {
74+
throw new ResourceNotFoundException();
75+
}
76+
77+
return $customField;
78+
}
79+
}

src/LooplineSystems/CloseIoApiWrapper/Api/LeadApi.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function getAllLeads()
4848
$result = $this->triggerGet($apiRequest);
4949

5050
if ($result->getReturnCode() == 200) {
51-
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
51+
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
5252

5353
foreach ($rawData as $lead) {
5454
$leads[] = new Lead($lead);
@@ -57,6 +57,46 @@ public function getAllLeads()
5757
return $leads;
5858
}
5959

60+
/**
61+
* @param array $queryParams
62+
*
63+
* @return Lead[]
64+
*/
65+
public function findLeads(array $queryParams)
66+
{
67+
/** @var Lead[] $leads */
68+
$leads = array();
69+
if (count($queryParams) > 0) {
70+
$queryParams = ['query' => $this->buildQueryString($queryParams)];
71+
}
72+
$apiRequest = $this->prepareRequest('get-leads', '', [], $queryParams);
73+
/** @var CloseIoResponse $result */
74+
$result = $this->triggerGet($apiRequest);
75+
if ($result->getReturnCode() == 200) {
76+
$rawData = $result->getData()['data'];
77+
foreach ($rawData as $lead) {
78+
$leads[] = new Lead($lead);
79+
}
80+
}
81+
82+
return $leads;
83+
}
84+
85+
/**
86+
* @param array $params
87+
*
88+
* @return string
89+
*/
90+
private function buildQueryString(array $params)
91+
{
92+
$flattened = [];
93+
foreach ($params as $key => $value) {
94+
$flattened[] = $key . '=' . $value;
95+
}
96+
$queryString = implode('&', $flattened);
97+
return $queryString;
98+
}
99+
60100
/**
61101
* @param $id
62102
* @return Lead

src/LooplineSystems/CloseIoApiWrapper/Api/LeadStatusApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getAllStatus()
8181
$result = $this->triggerGet($apiRequest);
8282

8383
if ($result->getReturnCode() == 200) {
84-
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
84+
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
8585
foreach ($rawData as $status) {
8686
$statuses[] = new LeadStatus($status);
8787
}

src/LooplineSystems/CloseIoApiWrapper/Api/OpportunityApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getAllOpportunities()
4747
$result = $this->triggerGet($apiRequest);
4848

4949
if ($result->getReturnCode() == 200) {
50-
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
50+
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
5151
foreach ($rawData as $opportunity) {
5252
$opportunities[] = new Opportunity($opportunity);
5353
}

src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace LooplineSystems\CloseIoApiWrapper;
1111

12+
use LooplineSystems\CloseIoApiWrapper\Api\CustomFieldApi;
1213
use LooplineSystems\CloseIoApiWrapper\Api\LeadApi;
1314
use LooplineSystems\CloseIoApiWrapper\Api\LeadStatusApi;
1415
use LooplineSystems\CloseIoApiWrapper\Api\OpportunityApi;
@@ -47,6 +48,7 @@ protected function initApiHandler(CloseIoConfig $config)
4748
$apiHandler->setApi(new LeadApi($apiHandler));
4849
$apiHandler->setApi(new OpportunityApi($apiHandler));
4950
$apiHandler->setApi(new LeadStatusApi($apiHandler));
51+
$apiHandler->setApi(new CustomFieldApi($apiHandler));
5052

5153
return $apiHandler;
5254
}
@@ -60,6 +62,15 @@ public function getLeadApi()
6062
return $this->apiHandler->getApi(LeadApi::NAME);
6163
}
6264

65+
/**
66+
* @return CustomFieldApi
67+
* @throws Library\Exception\ApiNotFoundException
68+
*/
69+
public function getCustomFieldApi()
70+
{
71+
return $this->apiHandler->getApi(CustomFieldApi::NAME);
72+
}
73+
6374
/**
6475
* @return OpportunityApi
6576
* @throws Library\Exception\ApiNotFoundException

src/LooplineSystems/CloseIoApiWrapper/CloseIoResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class CloseIoResponse
1313
{
1414

15-
const GET_ALL_RESPONSE_LEADS_KEY = 'data';
15+
const GET_RESPONSE_DATA_KEY = 'data';
1616
const GET_ALL_RESPONSE_HAS_MORE_KEY = 'has_more';
1717
const GET_ALL_RESPONSE_TOTAL_RESULTS_KEY = 'total_results';
1818

0 commit comments

Comments
 (0)