Skip to content

Commit

Permalink
- Support Laravel v9
Browse files Browse the repository at this point in the history
- Added where to ZohoManager module
  • Loading branch information
aemaddin committed Mar 22, 2022
1 parent 8847c5e commit f4964d6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 74 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": "^7.2|^8.0",
"ext-json": "*",
"illuminate/support": "^6.0|^7.0|^8.0",
"illuminate/support": "^7.0|^8.0|^9.0",
"nesbot/carbon": "^2.0",
"zohocrm/php-sdk-archive": "^2.2"
},
Expand Down
81 changes: 60 additions & 21 deletions src/CriteriaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,101 @@
namespace Asciisd\Zoho;


use Asciisd\Zoho\Facades\ZohoManager;

class CriteriaBuilder
{
protected $criteria;
protected $moduleIns;
protected $operators = ['equals', 'starts_with'];
protected $page = 1;
protected $perPage = 200;

public function __construct($moduleIns) {
$this->moduleIns = $moduleIns ?? ZohoManager::useModule('leads');
}

/**
* add criteria to the search
*
* @param $field
* @param $value
* @param string $operator
*
* @return $this
*/
public static function where($field, $value, $operator = 'equals')
{
$builder = new CriteriaBuilder();
public static function where(
$field,
$value,
$operator = 'equals',
$moduleIns = null
) {
$builder = new CriteriaBuilder($moduleIns);
$builder->criteria = "";

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

return $builder;
}

public function startsWith($field, $value, $operator = 'starts_with')
{
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator, $value);

return $this;
private static function queryBuilder(...$args) {
return "($args[0]:$args[1]:$args[2])";
}

public function andWhere($field, $value, $operator = 'equals')
{
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator, $value);
public function startsWith(
$field,
$value,
$operator = 'starts_with'
) {
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator,
$value);

return $this;
}

public function orWhere($field, $value, $operator = 'equals')
{
$this->criteria .= ' or ' . $this->queryBuilder($field, $operator, $value);
public function andWhere($field, $value, $operator = 'equals') {
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator,
$value);

return $this;
}

private static function queryBuilder(...$args)
{
return "($args[0]:$args[1]:$args[2])";
public function orWhere($field, $value, $operator = 'equals') {
$this->criteria .= ' or ' . $this->queryBuilder($field, $operator,
$value);

return $this;
}

public function toString()
{
public function toString() {
return $this->getCriteria() ?? '';
}

public function getCriteria()
{
public function getCriteria() {
return $this->criteria;
}

public function page($page) {
$this->page = $page;

return $this;
}

public function perPage($per_page) {
$this->perPage = $per_page;

return $this;
}

public function get() {
$param_map = ["page" => $this->page, "per_page" => $this->perPage];

return $this->moduleIns->searchRecordsByCriteria(
$this->getCriteria(), $param_map
)->getData();
}

public function search() {
return $this->get();
}
}
2 changes: 1 addition & 1 deletion src/Zoho.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Zoho
*
* @var string
*/
const VERSION = '1.2.7';
const VERSION = '1.2.9';

/**
* Indicates if Zoho migrations will be run.
Expand Down
25 changes: 17 additions & 8 deletions src/ZohoModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class ZohoModule
* @param $module_api_name
*/
public function __construct($rest, $module_api_name) {
$this->rest = $rest;
$this->rest = $rest;
$this->module_api_name = $module_api_name;
$this->moduleIns = $this->getModuleInstance();
$this->moduleIns = $this->getModuleInstance();
}

/**
Expand Down Expand Up @@ -73,6 +73,15 @@ public function getRecords() {
return $this->moduleIns->getRecords()->getData();
}

/**
* get the records array of given module api name
*
* @return array
*/
public function getJsonRecords() {
return $this->moduleIns->getRecords()->getResponseJSON();
}

/**
* get the record object of given module api name and record id
*
Expand Down Expand Up @@ -149,11 +158,7 @@ public function searchRecordsByEmail(
*
* @return ZCRMRecord[]
*/
public function searchRecordsByCriteria(
$criteria,
$page = 1,
$perPage = 200
) {
public function searchRecordsByCriteria($criteria, $page = 1, $perPage = 200) {
$param_map = ["page" => $page, "per_page" => $perPage];

return $this->moduleIns->searchRecordsByCriteria($criteria, $param_map)
Expand Down Expand Up @@ -198,7 +203,7 @@ public function create(array $args = [], string $trigger = null) {
* update existing entities in the module.
*
* @param ZCRMRecord $record
* @param string $trigger array of triggers
* @param string $trigger array of triggers
*
* @return ZCRMRecord[]
*/
Expand All @@ -225,4 +230,8 @@ public function search($builder, $page = 1, $perPage = 200) {

return null;
}

public function where($field, $value, $operator = 'equals') {
return CriteriaBuilder::where($field, $value, $operator, $this->moduleIns);
}
}
93 changes: 50 additions & 43 deletions tests/Integration/ZohoModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Asciisd\Zoho\Tests\Integration;

use Asciisd\Zoho\Facades\ZohoManager;
use Asciisd\Zoho\ZohoModule;
use zcrmsdk\crm\crud\ZCRMModule;
use zcrmsdk\crm\crud\ZCRMRecord;
use Asciisd\Zoho\CriteriaBuilder;
use Asciisd\Zoho\Facades\ZohoManager;
use zcrmsdk\crm\exception\ZCRMException;

class ZohoModuleTest extends IntegrationTestCase
Expand All @@ -15,123 +16,123 @@ class ZohoModuleTest extends IntegrationTestCase
private $client;
private $module;

protected function setUp(): void
{
parent::setUp();

$this->client = $this->getClient();
$this->module = $this->client->useModule('Leads');
}

/** @test */
public function it_can_get_all_modules()
{
public function it_can_get_all_modules() {
$leads = $this->client->getAllModules();

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

/** @test */
public function is_can_get_module_by_name()
{
public function is_can_get_module_by_name() {
$leads = $this->client->useModule();

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

/** @test */
public function it_can_instantiate_a_record_with_id()
{
public function it_can_instantiate_a_record_with_id() {
$record = $this->module->getRecordInstance(self::LEAD_ID_FOR_TEST);
self::assertInstanceOf(ZCRMRecord::class, $record);
self::assertEquals('Leads', $record->getModuleApiName());
self::assertEquals(self::LEAD_ID_FOR_TEST, $record->getEntityId());
}

/** @test */
public function it_can_instantiate_a_module_with_api_name()
{
public function it_can_instantiate_a_module_with_api_name() {
$module = $this->module->getModuleInstance();
self::assertInstanceOf(ZCRMModule::class, $module);
self::assertEquals('Leads', $module->getAPIName());
}

/** @test */
public function it_can_get_records_for_given_module_api_name()
{
public function it_can_get_records_for_given_module_api_name() {
$records = $this->module->getRecords();
self::assertInstanceOf(ZCRMRecord::class, $records[0]);
}

/** @test */
public function it_can_get_record_by_module_api_name_and_record_id()
{
public function it_can_get_record_by_module_api_name_and_record_id() {
$record = $this->module->getRecord(self::LEAD_ID_FOR_TEST);
self::assertInstanceOf(ZCRMRecord::class, $record);
}

/** @test */
public function it_can_search_for_word_on_specific_module()
{
public function it_can_search_for_word_on_specific_module() {
$records = $this->module->searchRecordsByWord('Amr Ahmed');

self::assertInstanceOf(ZCRMRecord::class, end($records));
self::assertEquals(self::LEAD_ID_FOR_TEST, end($records)->getEntityId());
self::assertEquals(self::LEAD_ID_FOR_TEST,
end($records)->getEntityId());
}

/** @test */
public function it_can_search_for_phone_on_specific_module()
{
public function it_can_search_for_phone_on_specific_module() {
$records = $this->module->searchRecordsByPhone('01011441444');

dump($records->getData());

self::assertInstanceOf(ZCRMRecord::class, $records[0]);
self::assertEquals('01011441444', $records[0]->getFieldValue('Phone'));
}

/** @test */
public function it_can_search_for_email_on_specific_module()
{
public function it_can_search_for_email_on_specific_module() {
$records = $this->module->searchRecordsByEmail('[email protected]');

self::assertInstanceOf(ZCRMRecord::class, $records[0]);
self::assertEquals('[email protected]', $records[0]->getFieldValue('Email'));
self::assertEquals('[email protected]',
$records[0]->getFieldValue('Email'));
}

/** @test */
public function it_can_search_by_criteria() {
$records =
$this->module->searchRecordsByCriteria("(City:equals:Al Wasitah) and (State:equals:Al Fayyum)");

self::assertInstanceOf(ZCRMRecord::class, $records[0]);
self::assertEquals('[email protected]',
$records[0]->getFieldValue('Email'));
}

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

self::assertInstanceOf(ZCRMRecord::class, $records[0]);
self::assertEquals('[email protected]', $records[0]->getFieldValue('Email'));
self::assertEquals('[email protected]',
$records[0]->getFieldValue('Email'));
}

/** @test */
public function it_can_search_by_field_name()
{
public function it_can_search_by_field_name() {
$records = $this->module->where('City', 'Al Wasitah')->search();

self::assertInstanceOf(ZCRMRecord::class, $records[0]);
self::assertEquals('[email protected]', $records[0]->getFieldValue('Email'));
self::assertEquals('[email protected]',
$records[0]->getFieldValue('Email'));
}

/** @test */
public function it_can_search_with_multiple_criteria()
{
public function it_can_search_with_multiple_criteria() {
$records = $this->module
->where('City', 'Al Wasitah')
->andWhere('State', 'Al Fayyum')
->search();

self::assertInstanceOf(ZCRMRecord::class, $records[0]);
self::assertEquals('[email protected]', $records[0]->getFieldValue('Email'));
self::assertEquals('[email protected]',
$records[0]->getFieldValue('Email'));
}

/** @test
* @throws ZCRMException
*/
public function it_can_create_new_record()
{
public function it_can_create_new_record() {
$lead = $this->module->getRecordInstance();

$lead->setFieldValue('First_Name', 'Amr');
Expand All @@ -152,8 +153,7 @@ public function it_can_create_new_record()
/** @test
* @throws ZCRMException
*/
public function it_can_update_records()
{
public function it_can_update_records() {
$lead = $this->module->getRecord('3582074000002383003');

$lead->setFieldValue('Last_Name', 'Ahmed');
Expand All @@ -167,4 +167,11 @@ public function it_can_fetch_nested_data_with_module() {
$contacts = ZohoManager::useModule('Contacts');
$contacts->getAllModules();
}

protected function setUp(): void {
parent::setUp();

$this->client = $this->getClient();
$this->module = $this->client->useModule('Leads');
}
}

0 comments on commit f4964d6

Please sign in to comment.