-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added New Zoho Polymorphism model to reused across laravel project mo…
…dels, that holds the zoho id inside local database.
- Loading branch information
Showing
15 changed files
with
484 additions
and
72 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
database/migrations/2020_06_04_000001_create_zohos_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateZohosTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('zohos', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('zoho_id')->unique()->index(); | ||
$table->morphs('zohoable'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('personal_access_tokens'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Asciisd\Zoho\Contracts\Repositories; | ||
|
||
use Asciisd\Zoho\CriteriaBuilder; | ||
|
||
interface ZohoableRepository | ||
{ | ||
/** | ||
* This used when we need to search for your current model record on zoho | ||
* | ||
* @return String|CriteriaBuilder | ||
*/ | ||
public function searchCriteria(); | ||
|
||
/** | ||
* Array for mandatory fields that required to create new record | ||
* | ||
* @return array | ||
*/ | ||
public function zohoMandatoryFields(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
|
||
namespace Asciisd\Zoho; | ||
|
||
|
||
class CriteriaBuilder | ||
{ | ||
protected $criteria; | ||
protected $operators = ['equals', 'starts_with']; | ||
|
||
/** | ||
* 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(); | ||
$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; | ||
} | ||
|
||
public function andWhere($field, $value, $operator = 'equals') | ||
{ | ||
$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); | ||
|
||
return $this; | ||
} | ||
|
||
private static function queryBuilder(...$args) | ||
{ | ||
return "($args[0]:$args[1]:$args[2])"; | ||
} | ||
|
||
public function toString() | ||
{ | ||
return $this->getCriteria() ?? ''; | ||
} | ||
|
||
public function getCriteria() | ||
{ | ||
return $this->criteria; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Asciisd\Zoho\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class InvalidZohoable extends Exception | ||
{ | ||
/** | ||
* Create a new InvalidTapCustomer instance. | ||
* | ||
* @param Model $owner | ||
* @return static | ||
*/ | ||
public static function nonZohoable($owner) | ||
{ | ||
return new static(class_basename($owner) . ' is not a Zohoable. See the createAsZohoable method.'); | ||
} | ||
|
||
/** | ||
* Create a new InvalidTapCustomer instance. | ||
* | ||
* @param Model $owner | ||
* @return static | ||
*/ | ||
public static function exists($owner) | ||
{ | ||
return new static(class_basename($owner) . " is already a Zohoable with ID {$owner->zohoId()}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Asciisd\Zoho\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Zoho extends Model | ||
{ | ||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'zohoable_type', 'zohoable_id', 'zoho_id' | ||
]; | ||
|
||
/** | ||
* Get the owning zohoable model. | ||
*/ | ||
public function zohoable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.