Skip to content

Commit f4964d6

Browse files
committed
- Support Laravel v9
- Added where to ZohoManager module
1 parent 8847c5e commit f4964d6

File tree

5 files changed

+129
-74
lines changed

5 files changed

+129
-74
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"require": {
1717
"php": "^7.2|^8.0",
1818
"ext-json": "*",
19-
"illuminate/support": "^6.0|^7.0|^8.0",
19+
"illuminate/support": "^7.0|^8.0|^9.0",
2020
"nesbot/carbon": "^2.0",
2121
"zohocrm/php-sdk-archive": "^2.2"
2222
},

src/CriteriaBuilder.php

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,101 @@
44
namespace Asciisd\Zoho;
55

66

7+
use Asciisd\Zoho\Facades\ZohoManager;
8+
79
class CriteriaBuilder
810
{
911
protected $criteria;
12+
protected $moduleIns;
1013
protected $operators = ['equals', 'starts_with'];
14+
protected $page = 1;
15+
protected $perPage = 200;
16+
17+
public function __construct($moduleIns) {
18+
$this->moduleIns = $moduleIns ?? ZohoManager::useModule('leads');
19+
}
1120

1221
/**
1322
* add criteria to the search
1423
*
1524
* @param $field
1625
* @param $value
1726
* @param string $operator
27+
*
1828
* @return $this
1929
*/
20-
public static function where($field, $value, $operator = 'equals')
21-
{
22-
$builder = new CriteriaBuilder();
30+
public static function where(
31+
$field,
32+
$value,
33+
$operator = 'equals',
34+
$moduleIns = null
35+
) {
36+
$builder = new CriteriaBuilder($moduleIns);
2337
$builder->criteria = "";
2438

2539
$builder->criteria .= static::queryBuilder($field, $operator, $value);
2640

2741
return $builder;
2842
}
2943

30-
public function startsWith($field, $value, $operator = 'starts_with')
31-
{
32-
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator, $value);
33-
34-
return $this;
44+
private static function queryBuilder(...$args) {
45+
return "($args[0]:$args[1]:$args[2])";
3546
}
3647

37-
public function andWhere($field, $value, $operator = 'equals')
38-
{
39-
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator, $value);
48+
public function startsWith(
49+
$field,
50+
$value,
51+
$operator = 'starts_with'
52+
) {
53+
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator,
54+
$value);
4055

4156
return $this;
4257
}
4358

44-
public function orWhere($field, $value, $operator = 'equals')
45-
{
46-
$this->criteria .= ' or ' . $this->queryBuilder($field, $operator, $value);
59+
public function andWhere($field, $value, $operator = 'equals') {
60+
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator,
61+
$value);
4762

4863
return $this;
4964
}
5065

51-
private static function queryBuilder(...$args)
52-
{
53-
return "($args[0]:$args[1]:$args[2])";
66+
public function orWhere($field, $value, $operator = 'equals') {
67+
$this->criteria .= ' or ' . $this->queryBuilder($field, $operator,
68+
$value);
69+
70+
return $this;
5471
}
5572

56-
public function toString()
57-
{
73+
public function toString() {
5874
return $this->getCriteria() ?? '';
5975
}
6076

61-
public function getCriteria()
62-
{
77+
public function getCriteria() {
6378
return $this->criteria;
6479
}
80+
81+
public function page($page) {
82+
$this->page = $page;
83+
84+
return $this;
85+
}
86+
87+
public function perPage($per_page) {
88+
$this->perPage = $per_page;
89+
90+
return $this;
91+
}
92+
93+
public function get() {
94+
$param_map = ["page" => $this->page, "per_page" => $this->perPage];
95+
96+
return $this->moduleIns->searchRecordsByCriteria(
97+
$this->getCriteria(), $param_map
98+
)->getData();
99+
}
100+
101+
public function search() {
102+
return $this->get();
103+
}
65104
}

src/Zoho.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Zoho
99
*
1010
* @var string
1111
*/
12-
const VERSION = '1.2.7';
12+
const VERSION = '1.2.9';
1313

1414
/**
1515
* Indicates if Zoho migrations will be run.

src/ZohoModule.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class ZohoModule
2020
* @param $module_api_name
2121
*/
2222
public function __construct($rest, $module_api_name) {
23-
$this->rest = $rest;
23+
$this->rest = $rest;
2424
$this->module_api_name = $module_api_name;
25-
$this->moduleIns = $this->getModuleInstance();
25+
$this->moduleIns = $this->getModuleInstance();
2626
}
2727

2828
/**
@@ -73,6 +73,15 @@ public function getRecords() {
7373
return $this->moduleIns->getRecords()->getData();
7474
}
7575

76+
/**
77+
* get the records array of given module api name
78+
*
79+
* @return array
80+
*/
81+
public function getJsonRecords() {
82+
return $this->moduleIns->getRecords()->getResponseJSON();
83+
}
84+
7685
/**
7786
* get the record object of given module api name and record id
7887
*
@@ -149,11 +158,7 @@ public function searchRecordsByEmail(
149158
*
150159
* @return ZCRMRecord[]
151160
*/
152-
public function searchRecordsByCriteria(
153-
$criteria,
154-
$page = 1,
155-
$perPage = 200
156-
) {
161+
public function searchRecordsByCriteria($criteria, $page = 1, $perPage = 200) {
157162
$param_map = ["page" => $page, "per_page" => $perPage];
158163

159164
return $this->moduleIns->searchRecordsByCriteria($criteria, $param_map)
@@ -198,7 +203,7 @@ public function create(array $args = [], string $trigger = null) {
198203
* update existing entities in the module.
199204
*
200205
* @param ZCRMRecord $record
201-
* @param string $trigger array of triggers
206+
* @param string $trigger array of triggers
202207
*
203208
* @return ZCRMRecord[]
204209
*/
@@ -225,4 +230,8 @@ public function search($builder, $page = 1, $perPage = 200) {
225230

226231
return null;
227232
}
233+
234+
public function where($field, $value, $operator = 'equals') {
235+
return CriteriaBuilder::where($field, $value, $operator, $this->moduleIns);
236+
}
228237
}

tests/Integration/ZohoModuleTest.php

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Asciisd\Zoho\Tests\Integration;
44

5-
use Asciisd\Zoho\Facades\ZohoManager;
65
use Asciisd\Zoho\ZohoModule;
76
use zcrmsdk\crm\crud\ZCRMModule;
87
use zcrmsdk\crm\crud\ZCRMRecord;
8+
use Asciisd\Zoho\CriteriaBuilder;
9+
use Asciisd\Zoho\Facades\ZohoManager;
910
use zcrmsdk\crm\exception\ZCRMException;
1011

1112
class ZohoModuleTest extends IntegrationTestCase
@@ -15,123 +16,123 @@ class ZohoModuleTest extends IntegrationTestCase
1516
private $client;
1617
private $module;
1718

18-
protected function setUp(): void
19-
{
20-
parent::setUp();
21-
22-
$this->client = $this->getClient();
23-
$this->module = $this->client->useModule('Leads');
24-
}
25-
2619
/** @test */
27-
public function it_can_get_all_modules()
28-
{
20+
public function it_can_get_all_modules() {
2921
$leads = $this->client->getAllModules();
3022

3123
self::assertInstanceOf(ZCRMModule::class, $leads[0]);
3224
}
3325

3426
/** @test */
35-
public function is_can_get_module_by_name()
36-
{
27+
public function is_can_get_module_by_name() {
3728
$leads = $this->client->useModule();
3829

3930
self::assertInstanceOf(ZohoModule::class, $leads);
4031
}
4132

4233
/** @test */
43-
public function it_can_instantiate_a_record_with_id()
44-
{
34+
public function it_can_instantiate_a_record_with_id() {
4535
$record = $this->module->getRecordInstance(self::LEAD_ID_FOR_TEST);
4636
self::assertInstanceOf(ZCRMRecord::class, $record);
4737
self::assertEquals('Leads', $record->getModuleApiName());
4838
self::assertEquals(self::LEAD_ID_FOR_TEST, $record->getEntityId());
4939
}
5040

5141
/** @test */
52-
public function it_can_instantiate_a_module_with_api_name()
53-
{
42+
public function it_can_instantiate_a_module_with_api_name() {
5443
$module = $this->module->getModuleInstance();
5544
self::assertInstanceOf(ZCRMModule::class, $module);
5645
self::assertEquals('Leads', $module->getAPIName());
5746
}
5847

5948
/** @test */
60-
public function it_can_get_records_for_given_module_api_name()
61-
{
49+
public function it_can_get_records_for_given_module_api_name() {
6250
$records = $this->module->getRecords();
6351
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
6452
}
6553

6654
/** @test */
67-
public function it_can_get_record_by_module_api_name_and_record_id()
68-
{
55+
public function it_can_get_record_by_module_api_name_and_record_id() {
6956
$record = $this->module->getRecord(self::LEAD_ID_FOR_TEST);
7057
self::assertInstanceOf(ZCRMRecord::class, $record);
7158
}
7259

7360
/** @test */
74-
public function it_can_search_for_word_on_specific_module()
75-
{
61+
public function it_can_search_for_word_on_specific_module() {
7662
$records = $this->module->searchRecordsByWord('Amr Ahmed');
7763

7864
self::assertInstanceOf(ZCRMRecord::class, end($records));
79-
self::assertEquals(self::LEAD_ID_FOR_TEST, end($records)->getEntityId());
65+
self::assertEquals(self::LEAD_ID_FOR_TEST,
66+
end($records)->getEntityId());
8067
}
8168

8269
/** @test */
83-
public function it_can_search_for_phone_on_specific_module()
84-
{
70+
public function it_can_search_for_phone_on_specific_module() {
8571
$records = $this->module->searchRecordsByPhone('01011441444');
8672

73+
dump($records->getData());
74+
8775
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
8876
self::assertEquals('01011441444', $records[0]->getFieldValue('Phone'));
8977
}
9078

9179
/** @test */
92-
public function it_can_search_for_email_on_specific_module()
93-
{
80+
public function it_can_search_for_email_on_specific_module() {
9481
$records = $this->module->searchRecordsByEmail('aemaddin@gmail.com');
9582

9683
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
97-
self::assertEquals('aemaddin@gmail.com', $records[0]->getFieldValue('Email'));
84+
self::assertEquals('aemaddin@gmail.com',
85+
$records[0]->getFieldValue('Email'));
86+
}
87+
88+
/** @test */
89+
public function it_can_search_by_criteria() {
90+
$records =
91+
$this->module->searchRecordsByCriteria("(City:equals:Al Wasitah) and (State:equals:Al Fayyum)");
92+
93+
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
94+
self::assertEquals('falah.alhajeri6999@hotmail.com',
95+
$records[0]->getFieldValue('Email'));
9896
}
9997

10098
/** @test */
101-
public function it_can_search_by_criteria()
102-
{
103-
$records = $this->module->searchRecordsByCriteria("(City:equals:Al Wasitah) and (State:equals:Al Fayyum)");
99+
function it_can_search_by_criteria_builder() {
100+
$records =
101+
ZohoManager::useModule('Contacts')->search(
102+
CriteriaBuilder::where('City', 'Al Wasitah')
103+
->andWhere('State', 'Al Fayyum')
104+
);
104105

105106
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
106-
self::assertEquals('falah.alhajeri6999@hotmail.com', $records[0]->getFieldValue('Email'));
107+
self::assertEquals('falah.alhajeri6999@hotmail.com',
108+
$records[0]->getFieldValue('Email'));
107109
}
108110

109111
/** @test */
110-
public function it_can_search_by_field_name()
111-
{
112+
public function it_can_search_by_field_name() {
112113
$records = $this->module->where('City', 'Al Wasitah')->search();
113114

114115
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
115-
self::assertEquals('falah.alhajeri6999@hotmail.com', $records[0]->getFieldValue('Email'));
116+
self::assertEquals('falah.alhajeri6999@hotmail.com',
117+
$records[0]->getFieldValue('Email'));
116118
}
117119

118120
/** @test */
119-
public function it_can_search_with_multiple_criteria()
120-
{
121+
public function it_can_search_with_multiple_criteria() {
121122
$records = $this->module
122123
->where('City', 'Al Wasitah')
123124
->andWhere('State', 'Al Fayyum')
124125
->search();
125126

126127
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
127-
self::assertEquals('falah.alhajeri6999@hotmail.com', $records[0]->getFieldValue('Email'));
128+
self::assertEquals('falah.alhajeri6999@hotmail.com',
129+
$records[0]->getFieldValue('Email'));
128130
}
129131

130132
/** @test
131133
* @throws ZCRMException
132134
*/
133-
public function it_can_create_new_record()
134-
{
135+
public function it_can_create_new_record() {
135136
$lead = $this->module->getRecordInstance();
136137

137138
$lead->setFieldValue('First_Name', 'Amr');
@@ -152,8 +153,7 @@ public function it_can_create_new_record()
152153
/** @test
153154
* @throws ZCRMException
154155
*/
155-
public function it_can_update_records()
156-
{
156+
public function it_can_update_records() {
157157
$lead = $this->module->getRecord('3582074000002383003');
158158

159159
$lead->setFieldValue('Last_Name', 'Ahmed');
@@ -167,4 +167,11 @@ public function it_can_fetch_nested_data_with_module() {
167167
$contacts = ZohoManager::useModule('Contacts');
168168
$contacts->getAllModules();
169169
}
170+
171+
protected function setUp(): void {
172+
parent::setUp();
173+
174+
$this->client = $this->getClient();
175+
$this->module = $this->client->useModule('Leads');
176+
}
170177
}

0 commit comments

Comments
 (0)