@@ -2,8 +2,8 @@ |
||
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Here goes your notification messages. |
|
| 7 | - */ |
|
| 5 | + /** |
|
| 6 | + * Here goes your notification messages. |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | 9 | ]; |
| 10 | 10 | \ No newline at end of file |
@@ -4,39 +4,39 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class AbstractRepositoryContainer implements RepositoryContainerInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Construct the repository class name based on |
|
| 9 | - * the method name called, search in the |
|
| 10 | - * given namespaces for the class and |
|
| 11 | - * return an instance. |
|
| 12 | - * |
|
| 13 | - * @param string $name the called method name |
|
| 14 | - * @param array $arguments the method arguments |
|
| 15 | - * @return object |
|
| 16 | - */ |
|
| 17 | - public function __call($name, $arguments) |
|
| 18 | - { |
|
| 19 | - foreach ($this->getRepoNameSpace() as $repoNameSpace) |
|
| 20 | - { |
|
| 21 | - $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
| 22 | - if (class_exists($class)) |
|
| 23 | - { |
|
| 24 | - \App::singleton($class, function ($app) use ($class) { |
|
| 7 | + /** |
|
| 8 | + * Construct the repository class name based on |
|
| 9 | + * the method name called, search in the |
|
| 10 | + * given namespaces for the class and |
|
| 11 | + * return an instance. |
|
| 12 | + * |
|
| 13 | + * @param string $name the called method name |
|
| 14 | + * @param array $arguments the method arguments |
|
| 15 | + * @return object |
|
| 16 | + */ |
|
| 17 | + public function __call($name, $arguments) |
|
| 18 | + { |
|
| 19 | + foreach ($this->getRepoNameSpace() as $repoNameSpace) |
|
| 20 | + { |
|
| 21 | + $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
| 22 | + if (class_exists($class)) |
|
| 23 | + { |
|
| 24 | + \App::singleton($class, function ($app) use ($class) { |
|
| 25 | 25 | |
| 26 | - return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
|
| 27 | - }); |
|
| 26 | + return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
|
| 27 | + }); |
|
| 28 | 28 | |
| 29 | - return \App::make($class); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - } |
|
| 29 | + return \App::make($class); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Abstract methods that return the necessary |
|
| 36 | - * information (repositories namespaces) |
|
| 37 | - * needed to preform the previous actions. |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - abstract protected function getRepoNameSpace(); |
|
| 34 | + /** |
|
| 35 | + * Abstract methods that return the necessary |
|
| 36 | + * information (repositories namespaces) |
|
| 37 | + * needed to preform the previous actions. |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + abstract protected function getRepoNameSpace(); |
|
| 42 | 42 | } |
| 43 | 43 | \ No newline at end of file |
@@ -18,10 +18,10 @@ |
||
| 18 | 18 | { |
| 19 | 19 | foreach ($this->getRepoNameSpace() as $repoNameSpace) |
| 20 | 20 | { |
| 21 | - $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
| 21 | + $class = rtrim($repoNameSpace, '\\').'\\'.ucfirst(str_singular($name)).'Repository'; |
|
| 22 | 22 | if (class_exists($class)) |
| 23 | 23 | { |
| 24 | - \App::singleton($class, function ($app) use ($class) { |
|
| 24 | + \App::singleton($class, function($app) use ($class) { |
|
| 25 | 25 | |
| 26 | 26 | return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
| 27 | 27 | }); |
@@ -13,13 +13,13 @@ |
||
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | 15 | Schema::create('settings', function (Blueprint $table) { |
| 16 | - $table->increments('id'); |
|
| 17 | - $table->string('name',100); |
|
| 18 | - $table->string('key',100)->unique(); |
|
| 19 | - $table->text('value')->nullable(); |
|
| 20 | - $table->softDeletes(); |
|
| 21 | - $table->timestamps(); |
|
| 22 | - }); |
|
| 16 | + $table->increments('id'); |
|
| 17 | + $table->string('name',100); |
|
| 18 | + $table->string('key',100)->unique(); |
|
| 19 | + $table->text('value')->nullable(); |
|
| 20 | + $table->softDeletes(); |
|
| 21 | + $table->timestamps(); |
|
| 22 | + }); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
@@ -12,10 +12,10 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | - Schema::create('settings', function (Blueprint $table) { |
|
| 15 | + Schema::create('settings', function(Blueprint $table) { |
|
| 16 | 16 | $table->increments('id'); |
| 17 | - $table->string('name',100); |
|
| 18 | - $table->string('key',100)->unique(); |
|
| 17 | + $table->string('name', 100); |
|
| 18 | + $table->string('key', 100)->unique(); |
|
| 19 | 19 | $table->text('value')->nullable(); |
| 20 | 20 | $table->softDeletes(); |
| 21 | 21 | $table->timestamps(); |
@@ -8,20 +8,20 @@ |
||
| 8 | 8 | |
| 9 | 9 | class SettingsController extends BaseApiController |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The name of the model that is used by the base api controller |
|
| 13 | - * to preform actions like (add, edit ... etc). |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $model = 'settings'; |
|
| 11 | + /** |
|
| 12 | + * The name of the model that is used by the base api controller |
|
| 13 | + * to preform actions like (add, edit ... etc). |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $model = 'settings'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The validations rules used by the base api controller |
|
| 20 | - * to check before add. |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $validationRules = [ |
|
| 24 | - 'name' => 'required|string|max:100', |
|
| 25 | - 'value' => 'required|string' |
|
| 26 | - ]; |
|
| 18 | + /** |
|
| 19 | + * The validations rules used by the base api controller |
|
| 20 | + * to check before add. |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $validationRules = [ |
|
| 24 | + 'name' => 'required|string|max:100', |
|
| 25 | + 'value' => 'required|string' |
|
| 26 | + ]; |
|
| 27 | 27 | } |
@@ -13,14 +13,14 @@ |
||
| 13 | 13 | * to preform actions like (add, edit ... etc). |
| 14 | 14 | * @var string |
| 15 | 15 | */ |
| 16 | - protected $model = 'settings'; |
|
| 16 | + protected $model = 'settings'; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * The validations rules used by the base api controller |
| 20 | 20 | * to check before add. |
| 21 | 21 | * @var array |
| 22 | 22 | */ |
| 23 | - protected $validationRules = [ |
|
| 23 | + protected $validationRules = [ |
|
| 24 | 24 | 'name' => 'required|string|max:100', |
| 25 | 25 | 'value' => 'required|string' |
| 26 | 26 | ]; |
@@ -432,8 +432,7 @@ discard block |
||
| 432 | 432 | $model = $this->model->lockForUpdate()->find($value); |
| 433 | 433 | $model ? $model->update($data) : 0; |
| 434 | 434 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
| 435 | - } |
|
| 436 | - else |
|
| 435 | + } else |
|
| 437 | 436 | { |
| 438 | 437 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
| 439 | 438 | $model->update($data); |
@@ -464,8 +463,7 @@ discard block |
||
| 464 | 463 | $model->delete(); |
| 465 | 464 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
| 466 | 465 | }); |
| 467 | - } |
|
| 468 | - else |
|
| 466 | + } else |
|
| 469 | 467 | { |
| 470 | 468 | \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
| 471 | 469 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
@@ -582,14 +580,12 @@ discard block |
||
| 582 | 580 | $conditions = $this->constructConditions($value, $model); |
| 583 | 581 | $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
| 584 | 582 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
| 585 | - } |
|
| 586 | - else if ($key == 'or') |
|
| 583 | + } else if ($key == 'or') |
|
| 587 | 584 | { |
| 588 | 585 | $conditions = $this->constructConditions($value, $model); |
| 589 | 586 | $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
| 590 | 587 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
| 591 | - } |
|
| 592 | - else |
|
| 588 | + } else |
|
| 593 | 589 | { |
| 594 | 590 | if (is_array($value)) |
| 595 | 591 | { |
@@ -598,13 +594,11 @@ discard block |
||
| 598 | 594 | { |
| 599 | 595 | $value1 = $value['val1']; |
| 600 | 596 | $value2 = $value['val2']; |
| 601 | - } |
|
| 602 | - else |
|
| 597 | + } else |
|
| 603 | 598 | { |
| 604 | 599 | $value = array_key_exists('val', $value) ? $value['val'] : ''; |
| 605 | 600 | } |
| 606 | - } |
|
| 607 | - else |
|
| 601 | + } else |
|
| 608 | 602 | { |
| 609 | 603 | $operator = '='; |
| 610 | 604 | } |
@@ -616,29 +610,24 @@ discard block |
||
| 616 | 610 | |
| 617 | 611 | $conditionString .= $key . ' <= ? {op} '; |
| 618 | 612 | $conditionValues[] = $value2; |
| 619 | - } |
|
| 620 | - elseif (strtolower($operator) == 'in') |
|
| 613 | + } elseif (strtolower($operator) == 'in') |
|
| 621 | 614 | { |
| 622 | 615 | $conditionValues = array_merge($conditionValues, $value); |
| 623 | 616 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
| 624 | 617 | $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
| 625 | - } |
|
| 626 | - elseif (strtolower($operator) == 'null') |
|
| 618 | + } elseif (strtolower($operator) == 'null') |
|
| 627 | 619 | { |
| 628 | 620 | $conditionString .= $key . ' is null {op} '; |
| 629 | - } |
|
| 630 | - elseif (strtolower($operator) == 'not null') |
|
| 621 | + } elseif (strtolower($operator) == 'not null') |
|
| 631 | 622 | { |
| 632 | 623 | $conditionString .= $key . ' is not null {op} '; |
| 633 | - } |
|
| 634 | - elseif (strtolower($operator) == 'has') |
|
| 624 | + } elseif (strtolower($operator) == 'has') |
|
| 635 | 625 | { |
| 636 | 626 | $sql = $model->withTrashed()->has($key)->toSql(); |
| 637 | 627 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
| 638 | 628 | $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
| 639 | 629 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
| 640 | - } |
|
| 641 | - else |
|
| 630 | + } else |
|
| 642 | 631 | { |
| 643 | 632 | $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
| 644 | 633 | $conditionValues[] = $value; |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * |
| 25 | 25 | * @param array $relations |
| 26 | 26 | * @param string $sortBy |
| 27 | - * @param boolean $desc |
|
| 27 | + * @param integer $desc |
|
| 28 | 28 | * @param array $columns |
| 29 | 29 | * @return collection |
| 30 | 30 | */ |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | * @param integer $perPage |
| 43 | 43 | * @param array $relations |
| 44 | 44 | * @param string $sortBy |
| 45 | - * @param boolean $desc |
|
| 45 | + * @param integer $desc |
|
| 46 | 46 | * @param array $columns |
| 47 | 47 | * @return collection |
| 48 | 48 | */ |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | * @param integer $perPage |
| 132 | 132 | * @param array $relations |
| 133 | 133 | * @param string $sortBy |
| 134 | - * @param boolean $desc |
|
| 134 | + * @param integer $desc |
|
| 135 | 135 | * @param array $columns |
| 136 | 136 | * @return collection |
| 137 | 137 | */ |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | * @param integer $perPage |
| 150 | 150 | * @param array $relations |
| 151 | 151 | * @param string $sortBy |
| 152 | - * @param boolean $desc |
|
| 152 | + * @param integer $desc |
|
| 153 | 153 | * @param array $columns |
| 154 | 154 | * @return collection |
| 155 | 155 | */ |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | * Update record in the storage based on the given |
| 413 | 413 | * condition. |
| 414 | 414 | * |
| 415 | - * @param var $value condition value |
|
| 415 | + * @param boolean $value condition value |
|
| 416 | 416 | * @param array $data |
| 417 | 417 | * @param string $attribute condition column name |
| 418 | 418 | * @return void |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | * @param array $conditions array of conditions |
| 490 | 490 | * @param array $relations |
| 491 | 491 | * @param string $sortBy |
| 492 | - * @param boolean $desc |
|
| 492 | + * @param integer $desc |
|
| 493 | 493 | * @param array $columns |
| 494 | 494 | * @return collection |
| 495 | 495 | */ |
@@ -521,7 +521,7 @@ discard block |
||
| 521 | 521 | * @param array $conditions array of conditions |
| 522 | 522 | * @param integer $perPage |
| 523 | 523 | * @param string $sortBy |
| 524 | - * @param boolean $desc |
|
| 524 | + * @param integer $desc |
|
| 525 | 525 | * @param array $columns |
| 526 | 526 | * @return collection |
| 527 | 527 | */ |
@@ -4,649 +4,649 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * The model implementation. |
|
| 9 | - * |
|
| 10 | - * @var model |
|
| 11 | - */ |
|
| 12 | - public $model; |
|
| 7 | + /** |
|
| 8 | + * The model implementation. |
|
| 9 | + * |
|
| 10 | + * @var model |
|
| 11 | + */ |
|
| 12 | + public $model; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Create new AbstractRepository instance. |
|
| 16 | - */ |
|
| 17 | - public function __construct() |
|
| 18 | - { |
|
| 19 | - $this->model = \App::make($this->getModel()); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Fetch all records with relations from the storage. |
|
| 24 | - * |
|
| 25 | - * @param array $relations |
|
| 26 | - * @param string $sortBy |
|
| 27 | - * @param boolean $desc |
|
| 28 | - * @param array $columns |
|
| 29 | - * @return collection |
|
| 30 | - */ |
|
| 31 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 32 | - { |
|
| 33 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 34 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Fetch all records with relations from storage in pages |
|
| 39 | - * that matche the given query. |
|
| 40 | - * |
|
| 41 | - * @param string $query |
|
| 42 | - * @param integer $perPage |
|
| 43 | - * @param array $relations |
|
| 44 | - * @param string $sortBy |
|
| 45 | - * @param boolean $desc |
|
| 46 | - * @param array $columns |
|
| 47 | - * @return collection |
|
| 48 | - */ |
|
| 49 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 50 | - { |
|
| 51 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 52 | - $conditionColumns = $this->model->searchable; |
|
| 53 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Construct the select conditions for the model. |
|
| 57 | - */ |
|
| 58 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 59 | - |
|
| 60 | - if (count($conditionColumns)) |
|
| 61 | - { |
|
| 62 | - /** |
|
| 63 | - * Use the first element in the model columns to construct the first condition. |
|
| 64 | - */ |
|
| 65 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 70 | - */ |
|
| 71 | - foreach ($conditionColumns as $column) |
|
| 72 | - { |
|
| 73 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Loop through the model relations. |
|
| 78 | - */ |
|
| 79 | - foreach ($relations as $relation) |
|
| 80 | - { |
|
| 81 | - /** |
|
| 82 | - * Remove the sub relation if exists. |
|
| 83 | - */ |
|
| 84 | - $relation = explode('.', $relation)[0]; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Try to fetch the relation repository from the core. |
|
| 88 | - */ |
|
| 89 | - if (\Core::$relation()) |
|
| 90 | - { |
|
| 91 | - /** |
|
| 92 | - * Construct the relation condition. |
|
| 93 | - */ |
|
| 94 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 95 | - |
|
| 96 | - $subModel->where(function ($q) use ($query, $relation){ |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get columns of the relation. |
|
| 100 | - */ |
|
| 101 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 102 | - |
|
| 103 | - if (count($subConditionColumns)) |
|
| 104 | - { |
|
| 105 | - /** |
|
| 106 | - * Use the first element in the relation model columns to construct the first condition. |
|
| 107 | - */ |
|
| 108 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 113 | - */ |
|
| 114 | - foreach ($subConditionColumns as $subConditionColumn) |
|
| 115 | - { |
|
| 116 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 117 | - } |
|
| 118 | - }); |
|
| 119 | - |
|
| 120 | - }); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - }); |
|
| 14 | + /** |
|
| 15 | + * Create new AbstractRepository instance. |
|
| 16 | + */ |
|
| 17 | + public function __construct() |
|
| 18 | + { |
|
| 19 | + $this->model = \App::make($this->getModel()); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Fetch all records with relations from the storage. |
|
| 24 | + * |
|
| 25 | + * @param array $relations |
|
| 26 | + * @param string $sortBy |
|
| 27 | + * @param boolean $desc |
|
| 28 | + * @param array $columns |
|
| 29 | + * @return collection |
|
| 30 | + */ |
|
| 31 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 32 | + { |
|
| 33 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 34 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Fetch all records with relations from storage in pages |
|
| 39 | + * that matche the given query. |
|
| 40 | + * |
|
| 41 | + * @param string $query |
|
| 42 | + * @param integer $perPage |
|
| 43 | + * @param array $relations |
|
| 44 | + * @param string $sortBy |
|
| 45 | + * @param boolean $desc |
|
| 46 | + * @param array $columns |
|
| 47 | + * @return collection |
|
| 48 | + */ |
|
| 49 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 50 | + { |
|
| 51 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 52 | + $conditionColumns = $this->model->searchable; |
|
| 53 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Construct the select conditions for the model. |
|
| 57 | + */ |
|
| 58 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 59 | + |
|
| 60 | + if (count($conditionColumns)) |
|
| 61 | + { |
|
| 62 | + /** |
|
| 63 | + * Use the first element in the model columns to construct the first condition. |
|
| 64 | + */ |
|
| 65 | + $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 70 | + */ |
|
| 71 | + foreach ($conditionColumns as $column) |
|
| 72 | + { |
|
| 73 | + $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Loop through the model relations. |
|
| 78 | + */ |
|
| 79 | + foreach ($relations as $relation) |
|
| 80 | + { |
|
| 81 | + /** |
|
| 82 | + * Remove the sub relation if exists. |
|
| 83 | + */ |
|
| 84 | + $relation = explode('.', $relation)[0]; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Try to fetch the relation repository from the core. |
|
| 88 | + */ |
|
| 89 | + if (\Core::$relation()) |
|
| 90 | + { |
|
| 91 | + /** |
|
| 92 | + * Construct the relation condition. |
|
| 93 | + */ |
|
| 94 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 95 | + |
|
| 96 | + $subModel->where(function ($q) use ($query, $relation){ |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get columns of the relation. |
|
| 100 | + */ |
|
| 101 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 102 | + |
|
| 103 | + if (count($subConditionColumns)) |
|
| 104 | + { |
|
| 105 | + /** |
|
| 106 | + * Use the first element in the relation model columns to construct the first condition. |
|
| 107 | + */ |
|
| 108 | + $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 113 | + */ |
|
| 114 | + foreach ($subConditionColumns as $subConditionColumn) |
|
| 115 | + { |
|
| 116 | + $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 117 | + } |
|
| 118 | + }); |
|
| 119 | + |
|
| 120 | + }); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + }); |
|
| 124 | 124 | |
| 125 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 126 | - } |
|
| 125 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Fetch all records with relations from storage in pages. |
|
| 130 | - * |
|
| 131 | - * @param integer $perPage |
|
| 132 | - * @param array $relations |
|
| 133 | - * @param string $sortBy |
|
| 134 | - * @param boolean $desc |
|
| 135 | - * @param array $columns |
|
| 136 | - * @return collection |
|
| 137 | - */ |
|
| 138 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 139 | - { |
|
| 140 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 141 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Fetch all records with relations based on |
|
| 146 | - * the given condition from storage in pages. |
|
| 147 | - * |
|
| 148 | - * @param array $conditions array of conditions |
|
| 149 | - * @param integer $perPage |
|
| 150 | - * @param array $relations |
|
| 151 | - * @param string $sortBy |
|
| 152 | - * @param boolean $desc |
|
| 153 | - * @param array $columns |
|
| 154 | - * @return collection |
|
| 155 | - */ |
|
| 156 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 157 | - { |
|
| 158 | - unset($conditions['page']); |
|
| 159 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 160 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 161 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 162 | - } |
|
| 128 | + /** |
|
| 129 | + * Fetch all records with relations from storage in pages. |
|
| 130 | + * |
|
| 131 | + * @param integer $perPage |
|
| 132 | + * @param array $relations |
|
| 133 | + * @param string $sortBy |
|
| 134 | + * @param boolean $desc |
|
| 135 | + * @param array $columns |
|
| 136 | + * @return collection |
|
| 137 | + */ |
|
| 138 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 139 | + { |
|
| 140 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 141 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Fetch all records with relations based on |
|
| 146 | + * the given condition from storage in pages. |
|
| 147 | + * |
|
| 148 | + * @param array $conditions array of conditions |
|
| 149 | + * @param integer $perPage |
|
| 150 | + * @param array $relations |
|
| 151 | + * @param string $sortBy |
|
| 152 | + * @param boolean $desc |
|
| 153 | + * @param array $columns |
|
| 154 | + * @return collection |
|
| 155 | + */ |
|
| 156 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 157 | + { |
|
| 158 | + unset($conditions['page']); |
|
| 159 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 160 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 161 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Save the given model to the storage. |
|
| 166 | - * |
|
| 167 | - * @param array $data |
|
| 168 | - * @param boolean $saveLog |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - public function save(array $data, $saveLog = true) |
|
| 172 | - { |
|
| 173 | - $model = false; |
|
| 174 | - $modelClass = $this->model; |
|
| 175 | - $relations = []; |
|
| 176 | - |
|
| 177 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 178 | - /** |
|
| 179 | - * If the id is present in the data then select the model for updating, |
|
| 180 | - * else create new model. |
|
| 181 | - * @var array |
|
| 182 | - */ |
|
| 183 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 184 | - if ( ! $model) |
|
| 185 | - { |
|
| 186 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Construct the model object with the given data, |
|
| 191 | - * and if there is a relation add it to relations array, |
|
| 192 | - * then save the model. |
|
| 193 | - */ |
|
| 194 | - foreach ($data as $key => $value) |
|
| 195 | - { |
|
| 196 | - /** |
|
| 197 | - * If the attribute is a relation. |
|
| 198 | - */ |
|
| 199 | - $relation = camel_case($key); |
|
| 200 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
| 201 | - { |
|
| 202 | - /** |
|
| 203 | - * Check if the relation is a collection. |
|
| 204 | - */ |
|
| 205 | - if (class_basename($model->$relation) == 'Collection') |
|
| 206 | - { |
|
| 207 | - /** |
|
| 208 | - * If the relation has no value then marke the relation data |
|
| 209 | - * related to the model to be deleted. |
|
| 210 | - */ |
|
| 211 | - if ( ! $value || ! count($value)) |
|
| 212 | - { |
|
| 213 | - $relations[$relation] = 'delete'; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - if (is_array($value)) |
|
| 217 | - { |
|
| 218 | - /** |
|
| 219 | - * Loop through the relation data. |
|
| 220 | - */ |
|
| 221 | - foreach ($value as $attr => $val) |
|
| 222 | - { |
|
| 223 | - /** |
|
| 224 | - * Get the relation model. |
|
| 225 | - */ |
|
| 226 | - $relationBaseModel = \Core::$relation()->model; |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Check if the relation is a collection. |
|
| 230 | - */ |
|
| 231 | - if (class_basename($model->$relation) == 'Collection') |
|
| 232 | - { |
|
| 233 | - /** |
|
| 234 | - * If the id is present in the data then select the relation model for updating, |
|
| 235 | - * else create new model. |
|
| 236 | - */ |
|
| 237 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * If model doesn't exists. |
|
| 241 | - */ |
|
| 242 | - if ( ! $relationModel) |
|
| 243 | - { |
|
| 244 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Loop through the relation attributes. |
|
| 249 | - */ |
|
| 250 | - foreach ($val as $attr => $val) |
|
| 251 | - { |
|
| 252 | - /** |
|
| 253 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 254 | - */ |
|
| 255 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 256 | - { |
|
| 257 | - $relationModel->$attr = $val; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - $relations[$relation][] = $relationModel; |
|
| 262 | - } |
|
| 263 | - /** |
|
| 264 | - * If not collection. |
|
| 265 | - */ |
|
| 266 | - else |
|
| 267 | - { |
|
| 268 | - /** |
|
| 269 | - * Prevent the sub relations. |
|
| 270 | - */ |
|
| 271 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 272 | - { |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * If the id is present in the data then select the relation model for updating, |
|
| 276 | - * else create new model. |
|
| 277 | - */ |
|
| 278 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * If model doesn't exists. |
|
| 282 | - */ |
|
| 283 | - if ( ! $relationModel) |
|
| 284 | - { |
|
| 285 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - foreach ($value as $relationAttribute => $relationValue) |
|
| 289 | - { |
|
| 290 | - /** |
|
| 291 | - * Prevent attributes not in the fillable. |
|
| 292 | - */ |
|
| 293 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 294 | - { |
|
| 295 | - $relationModel->$relationAttribute = $relationValue; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - $relations[$relation] = $relationModel; |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - /** |
|
| 306 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 307 | - */ |
|
| 308 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 309 | - { |
|
| 310 | - $model->$key = $value; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - /** |
|
| 314 | - * Save the model. |
|
| 315 | - */ |
|
| 316 | - $model->save(); |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Loop through the relations array. |
|
| 320 | - */ |
|
| 321 | - foreach ($relations as $key => $value) |
|
| 322 | - { |
|
| 323 | - /** |
|
| 324 | - * If the relation is marked for delete then delete it. |
|
| 325 | - */ |
|
| 326 | - if ($value == 'delete' && $model->$key()->count()) |
|
| 327 | - { |
|
| 328 | - $model->$key()->delete(); |
|
| 329 | - } |
|
| 330 | - /** |
|
| 331 | - * If the relation is an array. |
|
| 332 | - */ |
|
| 333 | - else if (gettype($value) == 'array') |
|
| 334 | - { |
|
| 335 | - $ids = []; |
|
| 336 | - /** |
|
| 337 | - * Loop through the relations. |
|
| 338 | - */ |
|
| 339 | - foreach ($value as $val) |
|
| 340 | - { |
|
| 341 | - switch (class_basename($model->$key())) |
|
| 342 | - { |
|
| 343 | - /** |
|
| 344 | - * If the relation is one to many then update it's foreign key with |
|
| 345 | - * the model id and save it then add its id to ids array to delete all |
|
| 346 | - * relations who's id isn't in the ids array. |
|
| 347 | - */ |
|
| 348 | - case 'HasMany': |
|
| 349 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 350 | - $val->$foreignKeyName = $model->id; |
|
| 351 | - $val->save(); |
|
| 352 | - $ids[] = $val->id; |
|
| 353 | - break; |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * If the relation is many to many then add it's id to the ids array to |
|
| 357 | - * attache these ids to the model. |
|
| 358 | - */ |
|
| 359 | - case 'BelongsToMany': |
|
| 360 | - $val->save(); |
|
| 361 | - $ids[] = $val->id; |
|
| 362 | - break; |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - switch (class_basename($model->$key())) |
|
| 366 | - { |
|
| 367 | - /** |
|
| 368 | - * If the relation is one to many then delete all |
|
| 369 | - * relations who's id isn't in the ids array. |
|
| 370 | - */ |
|
| 371 | - case 'HasMany': |
|
| 372 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 373 | - break; |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * If the relation is many to many then |
|
| 377 | - * detach the previous data and attach |
|
| 378 | - * the ids array to the model. |
|
| 379 | - */ |
|
| 380 | - case 'BelongsToMany': |
|
| 381 | - $model->$key()->detach(); |
|
| 382 | - $model->$key()->attach($ids); |
|
| 383 | - break; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - /** |
|
| 387 | - * If the relation isn't array. |
|
| 388 | - */ |
|
| 389 | - else |
|
| 390 | - { |
|
| 391 | - switch (class_basename($model->$key())) |
|
| 392 | - { |
|
| 393 | - /** |
|
| 394 | - * If the relation is one to many or one to one. |
|
| 395 | - */ |
|
| 396 | - case 'HasOne': |
|
| 397 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 398 | - $value->$foreignKeyName = $model->id; |
|
| 399 | - $value->save(); |
|
| 400 | - break; |
|
| 401 | - } |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
| 406 | - }); |
|
| 164 | + /** |
|
| 165 | + * Save the given model to the storage. |
|
| 166 | + * |
|
| 167 | + * @param array $data |
|
| 168 | + * @param boolean $saveLog |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + public function save(array $data, $saveLog = true) |
|
| 172 | + { |
|
| 173 | + $model = false; |
|
| 174 | + $modelClass = $this->model; |
|
| 175 | + $relations = []; |
|
| 176 | + |
|
| 177 | + \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 178 | + /** |
|
| 179 | + * If the id is present in the data then select the model for updating, |
|
| 180 | + * else create new model. |
|
| 181 | + * @var array |
|
| 182 | + */ |
|
| 183 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 184 | + if ( ! $model) |
|
| 185 | + { |
|
| 186 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Construct the model object with the given data, |
|
| 191 | + * and if there is a relation add it to relations array, |
|
| 192 | + * then save the model. |
|
| 193 | + */ |
|
| 194 | + foreach ($data as $key => $value) |
|
| 195 | + { |
|
| 196 | + /** |
|
| 197 | + * If the attribute is a relation. |
|
| 198 | + */ |
|
| 199 | + $relation = camel_case($key); |
|
| 200 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
| 201 | + { |
|
| 202 | + /** |
|
| 203 | + * Check if the relation is a collection. |
|
| 204 | + */ |
|
| 205 | + if (class_basename($model->$relation) == 'Collection') |
|
| 206 | + { |
|
| 207 | + /** |
|
| 208 | + * If the relation has no value then marke the relation data |
|
| 209 | + * related to the model to be deleted. |
|
| 210 | + */ |
|
| 211 | + if ( ! $value || ! count($value)) |
|
| 212 | + { |
|
| 213 | + $relations[$relation] = 'delete'; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + if (is_array($value)) |
|
| 217 | + { |
|
| 218 | + /** |
|
| 219 | + * Loop through the relation data. |
|
| 220 | + */ |
|
| 221 | + foreach ($value as $attr => $val) |
|
| 222 | + { |
|
| 223 | + /** |
|
| 224 | + * Get the relation model. |
|
| 225 | + */ |
|
| 226 | + $relationBaseModel = \Core::$relation()->model; |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Check if the relation is a collection. |
|
| 230 | + */ |
|
| 231 | + if (class_basename($model->$relation) == 'Collection') |
|
| 232 | + { |
|
| 233 | + /** |
|
| 234 | + * If the id is present in the data then select the relation model for updating, |
|
| 235 | + * else create new model. |
|
| 236 | + */ |
|
| 237 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * If model doesn't exists. |
|
| 241 | + */ |
|
| 242 | + if ( ! $relationModel) |
|
| 243 | + { |
|
| 244 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Loop through the relation attributes. |
|
| 249 | + */ |
|
| 250 | + foreach ($val as $attr => $val) |
|
| 251 | + { |
|
| 252 | + /** |
|
| 253 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 254 | + */ |
|
| 255 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 256 | + { |
|
| 257 | + $relationModel->$attr = $val; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + $relations[$relation][] = $relationModel; |
|
| 262 | + } |
|
| 263 | + /** |
|
| 264 | + * If not collection. |
|
| 265 | + */ |
|
| 266 | + else |
|
| 267 | + { |
|
| 268 | + /** |
|
| 269 | + * Prevent the sub relations. |
|
| 270 | + */ |
|
| 271 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 272 | + { |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * If the id is present in the data then select the relation model for updating, |
|
| 276 | + * else create new model. |
|
| 277 | + */ |
|
| 278 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * If model doesn't exists. |
|
| 282 | + */ |
|
| 283 | + if ( ! $relationModel) |
|
| 284 | + { |
|
| 285 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + foreach ($value as $relationAttribute => $relationValue) |
|
| 289 | + { |
|
| 290 | + /** |
|
| 291 | + * Prevent attributes not in the fillable. |
|
| 292 | + */ |
|
| 293 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 294 | + { |
|
| 295 | + $relationModel->$relationAttribute = $relationValue; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + $relations[$relation] = $relationModel; |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + /** |
|
| 306 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 307 | + */ |
|
| 308 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 309 | + { |
|
| 310 | + $model->$key = $value; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + /** |
|
| 314 | + * Save the model. |
|
| 315 | + */ |
|
| 316 | + $model->save(); |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Loop through the relations array. |
|
| 320 | + */ |
|
| 321 | + foreach ($relations as $key => $value) |
|
| 322 | + { |
|
| 323 | + /** |
|
| 324 | + * If the relation is marked for delete then delete it. |
|
| 325 | + */ |
|
| 326 | + if ($value == 'delete' && $model->$key()->count()) |
|
| 327 | + { |
|
| 328 | + $model->$key()->delete(); |
|
| 329 | + } |
|
| 330 | + /** |
|
| 331 | + * If the relation is an array. |
|
| 332 | + */ |
|
| 333 | + else if (gettype($value) == 'array') |
|
| 334 | + { |
|
| 335 | + $ids = []; |
|
| 336 | + /** |
|
| 337 | + * Loop through the relations. |
|
| 338 | + */ |
|
| 339 | + foreach ($value as $val) |
|
| 340 | + { |
|
| 341 | + switch (class_basename($model->$key())) |
|
| 342 | + { |
|
| 343 | + /** |
|
| 344 | + * If the relation is one to many then update it's foreign key with |
|
| 345 | + * the model id and save it then add its id to ids array to delete all |
|
| 346 | + * relations who's id isn't in the ids array. |
|
| 347 | + */ |
|
| 348 | + case 'HasMany': |
|
| 349 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 350 | + $val->$foreignKeyName = $model->id; |
|
| 351 | + $val->save(); |
|
| 352 | + $ids[] = $val->id; |
|
| 353 | + break; |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * If the relation is many to many then add it's id to the ids array to |
|
| 357 | + * attache these ids to the model. |
|
| 358 | + */ |
|
| 359 | + case 'BelongsToMany': |
|
| 360 | + $val->save(); |
|
| 361 | + $ids[] = $val->id; |
|
| 362 | + break; |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + switch (class_basename($model->$key())) |
|
| 366 | + { |
|
| 367 | + /** |
|
| 368 | + * If the relation is one to many then delete all |
|
| 369 | + * relations who's id isn't in the ids array. |
|
| 370 | + */ |
|
| 371 | + case 'HasMany': |
|
| 372 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 373 | + break; |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * If the relation is many to many then |
|
| 377 | + * detach the previous data and attach |
|
| 378 | + * the ids array to the model. |
|
| 379 | + */ |
|
| 380 | + case 'BelongsToMany': |
|
| 381 | + $model->$key()->detach(); |
|
| 382 | + $model->$key()->attach($ids); |
|
| 383 | + break; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + /** |
|
| 387 | + * If the relation isn't array. |
|
| 388 | + */ |
|
| 389 | + else |
|
| 390 | + { |
|
| 391 | + switch (class_basename($model->$key())) |
|
| 392 | + { |
|
| 393 | + /** |
|
| 394 | + * If the relation is one to many or one to one. |
|
| 395 | + */ |
|
| 396 | + case 'HasOne': |
|
| 397 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 398 | + $value->$foreignKeyName = $model->id; |
|
| 399 | + $value->save(); |
|
| 400 | + break; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
| 406 | + }); |
|
| 407 | 407 | |
| 408 | - return $model->id; |
|
| 409 | - } |
|
| 408 | + return $model->id; |
|
| 409 | + } |
|
| 410 | 410 | |
| 411 | - /** |
|
| 412 | - * Update record in the storage based on the given |
|
| 413 | - * condition. |
|
| 414 | - * |
|
| 415 | - * @param var $value condition value |
|
| 416 | - * @param array $data |
|
| 417 | - * @param string $attribute condition column name |
|
| 418 | - * @return void |
|
| 419 | - */ |
|
| 420 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
| 421 | - { |
|
| 422 | - if ($attribute == 'id') |
|
| 423 | - { |
|
| 424 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 425 | - $model ? $model->update($data) : 0; |
|
| 426 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 427 | - } |
|
| 428 | - else |
|
| 429 | - { |
|
| 430 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
| 431 | - $model->update($data); |
|
| 432 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 433 | - }); |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Delete record from the storage based on the given |
|
| 439 | - * condition. |
|
| 440 | - * |
|
| 441 | - * @param var $value condition value |
|
| 442 | - * @param string $attribute condition column name |
|
| 443 | - * @return void |
|
| 444 | - */ |
|
| 445 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
| 446 | - { |
|
| 447 | - if ($attribute == 'id') |
|
| 448 | - { |
|
| 449 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 450 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 451 | - if ( ! $model) |
|
| 452 | - { |
|
| 453 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 454 | - } |
|
| 411 | + /** |
|
| 412 | + * Update record in the storage based on the given |
|
| 413 | + * condition. |
|
| 414 | + * |
|
| 415 | + * @param var $value condition value |
|
| 416 | + * @param array $data |
|
| 417 | + * @param string $attribute condition column name |
|
| 418 | + * @return void |
|
| 419 | + */ |
|
| 420 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
| 421 | + { |
|
| 422 | + if ($attribute == 'id') |
|
| 423 | + { |
|
| 424 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 425 | + $model ? $model->update($data) : 0; |
|
| 426 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 427 | + } |
|
| 428 | + else |
|
| 429 | + { |
|
| 430 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
| 431 | + $model->update($data); |
|
| 432 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 433 | + }); |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Delete record from the storage based on the given |
|
| 439 | + * condition. |
|
| 440 | + * |
|
| 441 | + * @param var $value condition value |
|
| 442 | + * @param string $attribute condition column name |
|
| 443 | + * @return void |
|
| 444 | + */ |
|
| 445 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
| 446 | + { |
|
| 447 | + if ($attribute == 'id') |
|
| 448 | + { |
|
| 449 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 450 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 451 | + if ( ! $model) |
|
| 452 | + { |
|
| 453 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 454 | + } |
|
| 455 | 455 | |
| 456 | - $model->delete(); |
|
| 457 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 458 | - }); |
|
| 459 | - } |
|
| 460 | - else |
|
| 461 | - { |
|
| 462 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 463 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
| 464 | - $model->delete(); |
|
| 465 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 466 | - }); |
|
| 467 | - }); |
|
| 468 | - } |
|
| 469 | - } |
|
| 456 | + $model->delete(); |
|
| 457 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 458 | + }); |
|
| 459 | + } |
|
| 460 | + else |
|
| 461 | + { |
|
| 462 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 463 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
| 464 | + $model->delete(); |
|
| 465 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 466 | + }); |
|
| 467 | + }); |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | - /** |
|
| 472 | - * Fetch records from the storage based on the given |
|
| 473 | - * id. |
|
| 474 | - * |
|
| 475 | - * @param integer $id |
|
| 476 | - * @param array $relations |
|
| 477 | - * @param array $columns |
|
| 478 | - * @return object |
|
| 479 | - */ |
|
| 480 | - public function find($id, $relations = [], $columns = array('*')) |
|
| 481 | - { |
|
| 482 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 483 | - } |
|
| 471 | + /** |
|
| 472 | + * Fetch records from the storage based on the given |
|
| 473 | + * id. |
|
| 474 | + * |
|
| 475 | + * @param integer $id |
|
| 476 | + * @param array $relations |
|
| 477 | + * @param array $columns |
|
| 478 | + * @return object |
|
| 479 | + */ |
|
| 480 | + public function find($id, $relations = [], $columns = array('*')) |
|
| 481 | + { |
|
| 482 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 483 | + } |
|
| 484 | 484 | |
| 485 | - /** |
|
| 486 | - * Fetch records from the storage based on the given |
|
| 487 | - * condition. |
|
| 488 | - * |
|
| 489 | - * @param array $conditions array of conditions |
|
| 490 | - * @param array $relations |
|
| 491 | - * @param string $sortBy |
|
| 492 | - * @param boolean $desc |
|
| 493 | - * @param array $columns |
|
| 494 | - * @return collection |
|
| 495 | - */ |
|
| 496 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 497 | - { |
|
| 498 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 499 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 500 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * Fetch the first record from the storage based on the given |
|
| 505 | - * condition. |
|
| 506 | - * |
|
| 507 | - * @param array $conditions array of conditions |
|
| 508 | - * @param array $relations |
|
| 509 | - * @param array $columns |
|
| 510 | - * @return object |
|
| 511 | - */ |
|
| 512 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
| 513 | - { |
|
| 514 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 515 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Return the deleted models in pages based on the given conditions. |
|
| 520 | - * |
|
| 521 | - * @param array $conditions array of conditions |
|
| 522 | - * @param integer $perPage |
|
| 523 | - * @param string $sortBy |
|
| 524 | - * @param boolean $desc |
|
| 525 | - * @param array $columns |
|
| 526 | - * @return collection |
|
| 527 | - */ |
|
| 528 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 529 | - { |
|
| 530 | - unset($conditions['page']); |
|
| 531 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 532 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 533 | - $model = $this->model->onlyTrashed(); |
|
| 534 | - |
|
| 535 | - if (count($conditions['conditionValues'])) |
|
| 536 | - { |
|
| 537 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * Restore the deleted model. |
|
| 545 | - * |
|
| 546 | - * @param integer $id |
|
| 547 | - * @return void |
|
| 548 | - */ |
|
| 549 | - public function restore($id) |
|
| 550 | - { |
|
| 551 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 552 | - |
|
| 553 | - if ( ! $model) |
|
| 554 | - { |
|
| 555 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - $model->restore(); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Build the conditions recursively for the retrieving methods. |
|
| 563 | - * @param array $conditions |
|
| 564 | - * @return array |
|
| 565 | - */ |
|
| 566 | - protected function constructConditions($conditions, $model) |
|
| 567 | - { |
|
| 568 | - $conditionString = ''; |
|
| 569 | - $conditionValues = []; |
|
| 570 | - foreach ($conditions as $key => $value) |
|
| 571 | - { |
|
| 572 | - if ($key == 'and') |
|
| 573 | - { |
|
| 574 | - $conditions = $this->constructConditions($value, $model); |
|
| 575 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 576 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 577 | - } |
|
| 578 | - else if ($key == 'or') |
|
| 579 | - { |
|
| 580 | - $conditions = $this->constructConditions($value, $model); |
|
| 581 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 582 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 583 | - } |
|
| 584 | - else |
|
| 585 | - { |
|
| 586 | - if (is_array($value)) |
|
| 587 | - { |
|
| 588 | - $operator = $value['op']; |
|
| 589 | - if (strtolower($operator) == 'between') |
|
| 590 | - { |
|
| 591 | - $value1 = $value['val1']; |
|
| 592 | - $value2 = $value['val2']; |
|
| 593 | - } |
|
| 594 | - else |
|
| 595 | - { |
|
| 596 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - else |
|
| 600 | - { |
|
| 601 | - $operator = '='; |
|
| 602 | - } |
|
| 485 | + /** |
|
| 486 | + * Fetch records from the storage based on the given |
|
| 487 | + * condition. |
|
| 488 | + * |
|
| 489 | + * @param array $conditions array of conditions |
|
| 490 | + * @param array $relations |
|
| 491 | + * @param string $sortBy |
|
| 492 | + * @param boolean $desc |
|
| 493 | + * @param array $columns |
|
| 494 | + * @return collection |
|
| 495 | + */ |
|
| 496 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 497 | + { |
|
| 498 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 499 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 500 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * Fetch the first record from the storage based on the given |
|
| 505 | + * condition. |
|
| 506 | + * |
|
| 507 | + * @param array $conditions array of conditions |
|
| 508 | + * @param array $relations |
|
| 509 | + * @param array $columns |
|
| 510 | + * @return object |
|
| 511 | + */ |
|
| 512 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
| 513 | + { |
|
| 514 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 515 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Return the deleted models in pages based on the given conditions. |
|
| 520 | + * |
|
| 521 | + * @param array $conditions array of conditions |
|
| 522 | + * @param integer $perPage |
|
| 523 | + * @param string $sortBy |
|
| 524 | + * @param boolean $desc |
|
| 525 | + * @param array $columns |
|
| 526 | + * @return collection |
|
| 527 | + */ |
|
| 528 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 529 | + { |
|
| 530 | + unset($conditions['page']); |
|
| 531 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 532 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 533 | + $model = $this->model->onlyTrashed(); |
|
| 534 | + |
|
| 535 | + if (count($conditions['conditionValues'])) |
|
| 536 | + { |
|
| 537 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * Restore the deleted model. |
|
| 545 | + * |
|
| 546 | + * @param integer $id |
|
| 547 | + * @return void |
|
| 548 | + */ |
|
| 549 | + public function restore($id) |
|
| 550 | + { |
|
| 551 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 552 | + |
|
| 553 | + if ( ! $model) |
|
| 554 | + { |
|
| 555 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + $model->restore(); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Build the conditions recursively for the retrieving methods. |
|
| 563 | + * @param array $conditions |
|
| 564 | + * @return array |
|
| 565 | + */ |
|
| 566 | + protected function constructConditions($conditions, $model) |
|
| 567 | + { |
|
| 568 | + $conditionString = ''; |
|
| 569 | + $conditionValues = []; |
|
| 570 | + foreach ($conditions as $key => $value) |
|
| 571 | + { |
|
| 572 | + if ($key == 'and') |
|
| 573 | + { |
|
| 574 | + $conditions = $this->constructConditions($value, $model); |
|
| 575 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 576 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 577 | + } |
|
| 578 | + else if ($key == 'or') |
|
| 579 | + { |
|
| 580 | + $conditions = $this->constructConditions($value, $model); |
|
| 581 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 582 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 583 | + } |
|
| 584 | + else |
|
| 585 | + { |
|
| 586 | + if (is_array($value)) |
|
| 587 | + { |
|
| 588 | + $operator = $value['op']; |
|
| 589 | + if (strtolower($operator) == 'between') |
|
| 590 | + { |
|
| 591 | + $value1 = $value['val1']; |
|
| 592 | + $value2 = $value['val2']; |
|
| 593 | + } |
|
| 594 | + else |
|
| 595 | + { |
|
| 596 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + else |
|
| 600 | + { |
|
| 601 | + $operator = '='; |
|
| 602 | + } |
|
| 603 | 603 | |
| 604 | - if (strtolower($operator) == 'between') |
|
| 605 | - { |
|
| 606 | - $conditionString .= $key . ' >= ? and '; |
|
| 607 | - $conditionValues[] = $value1; |
|
| 608 | - |
|
| 609 | - $conditionString .= $key . ' <= ? {op} '; |
|
| 610 | - $conditionValues[] = $value2; |
|
| 611 | - } |
|
| 612 | - elseif (strtolower($operator) == 'in') |
|
| 613 | - { |
|
| 614 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 615 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 616 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 617 | - } |
|
| 618 | - elseif (strtolower($operator) == 'null') |
|
| 619 | - { |
|
| 620 | - $conditionString .= $key . ' is null {op} '; |
|
| 621 | - } |
|
| 622 | - elseif (strtolower($operator) == 'not null') |
|
| 623 | - { |
|
| 624 | - $conditionString .= $key . ' is not null {op} '; |
|
| 625 | - } |
|
| 626 | - elseif (strtolower($operator) == 'has') |
|
| 627 | - { |
|
| 628 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 629 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 630 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 631 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 632 | - } |
|
| 633 | - else |
|
| 634 | - { |
|
| 635 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 636 | - $conditionValues[] = $value; |
|
| 637 | - } |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 641 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Abstract method that return the necessary |
|
| 646 | - * information (full model namespace) |
|
| 647 | - * needed to preform the previous actions. |
|
| 648 | - * |
|
| 649 | - * @return string |
|
| 650 | - */ |
|
| 651 | - abstract protected function getModel(); |
|
| 604 | + if (strtolower($operator) == 'between') |
|
| 605 | + { |
|
| 606 | + $conditionString .= $key . ' >= ? and '; |
|
| 607 | + $conditionValues[] = $value1; |
|
| 608 | + |
|
| 609 | + $conditionString .= $key . ' <= ? {op} '; |
|
| 610 | + $conditionValues[] = $value2; |
|
| 611 | + } |
|
| 612 | + elseif (strtolower($operator) == 'in') |
|
| 613 | + { |
|
| 614 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 615 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 616 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 617 | + } |
|
| 618 | + elseif (strtolower($operator) == 'null') |
|
| 619 | + { |
|
| 620 | + $conditionString .= $key . ' is null {op} '; |
|
| 621 | + } |
|
| 622 | + elseif (strtolower($operator) == 'not null') |
|
| 623 | + { |
|
| 624 | + $conditionString .= $key . ' is not null {op} '; |
|
| 625 | + } |
|
| 626 | + elseif (strtolower($operator) == 'has') |
|
| 627 | + { |
|
| 628 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 629 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 630 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 631 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 632 | + } |
|
| 633 | + else |
|
| 634 | + { |
|
| 635 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 636 | + $conditionValues[] = $value; |
|
| 637 | + } |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 641 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Abstract method that return the necessary |
|
| 646 | + * information (full model namespace) |
|
| 647 | + * needed to preform the previous actions. |
|
| 648 | + * |
|
| 649 | + * @return string |
|
| 650 | + */ |
|
| 651 | + abstract protected function getModel(); |
|
| 652 | 652 | } |
| 653 | 653 | \ No newline at end of file |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | public function __construct() |
| 18 | 18 | { |
| 19 | - $this->model = \App::make($this->getModel()); |
|
| 19 | + $this->model = \App::make($this->getModel()); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -55,14 +55,14 @@ discard block |
||
| 55 | 55 | /** |
| 56 | 56 | * Construct the select conditions for the model. |
| 57 | 57 | */ |
| 58 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 58 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
| 59 | 59 | |
| 60 | 60 | if (count($conditionColumns)) |
| 61 | 61 | { |
| 62 | 62 | /** |
| 63 | 63 | * Use the first element in the model columns to construct the first condition. |
| 64 | 64 | */ |
| 65 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 65 | + $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | foreach ($conditionColumns as $column) |
| 72 | 72 | { |
| 73 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 73 | + $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -91,9 +91,9 @@ discard block |
||
| 91 | 91 | /** |
| 92 | 92 | * Construct the relation condition. |
| 93 | 93 | */ |
| 94 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 94 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
| 95 | 95 | |
| 96 | - $subModel->where(function ($q) use ($query, $relation){ |
|
| 96 | + $subModel->where(function($q) use ($query, $relation){ |
|
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | 99 | * Get columns of the relation. |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | /** |
| 106 | 106 | * Use the first element in the relation model columns to construct the first condition. |
| 107 | 107 | */ |
| 108 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 108 | + $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | foreach ($subConditionColumns as $subConditionColumn) |
| 115 | 115 | { |
| 116 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 116 | + $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 117 | 117 | } |
| 118 | 118 | }); |
| 119 | 119 | |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | $modelClass = $this->model; |
| 175 | 175 | $relations = []; |
| 176 | 176 | |
| 177 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 177 | + \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 178 | 178 | /** |
| 179 | 179 | * If the id is present in the data then select the model for updating, |
| 180 | 180 | * else create new model. |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 184 | 184 | if ( ! $model) |
| 185 | 185 | { |
| 186 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 186 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /** |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | */ |
| 242 | 242 | if ( ! $relationModel) |
| 243 | 243 | { |
| 244 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 244 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | /** |
| 253 | 253 | * Prevent the sub relations or attributes not in the fillable. |
| 254 | 254 | */ |
| 255 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 255 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 256 | 256 | { |
| 257 | 257 | $relationModel->$attr = $val; |
| 258 | 258 | } |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | */ |
| 283 | 283 | if ( ! $relationModel) |
| 284 | 284 | { |
| 285 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 285 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | foreach ($value as $relationAttribute => $relationValue) |
@@ -427,7 +427,7 @@ discard block |
||
| 427 | 427 | } |
| 428 | 428 | else |
| 429 | 429 | { |
| 430 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
| 430 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ |
|
| 431 | 431 | $model->update($data); |
| 432 | 432 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
| 433 | 433 | }); |
@@ -446,11 +446,11 @@ discard block |
||
| 446 | 446 | { |
| 447 | 447 | if ($attribute == 'id') |
| 448 | 448 | { |
| 449 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 449 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
| 450 | 450 | $model = $this->model->lockForUpdate()->find($value); |
| 451 | 451 | if ( ! $model) |
| 452 | 452 | { |
| 453 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 453 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | $model->delete(); |
@@ -459,8 +459,8 @@ discard block |
||
| 459 | 459 | } |
| 460 | 460 | else |
| 461 | 461 | { |
| 462 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 463 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
| 462 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
| 463 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($saveLog){ |
|
| 464 | 464 | $model->delete(); |
| 465 | 465 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
| 466 | 466 | }); |
@@ -497,7 +497,7 @@ discard block |
||
| 497 | 497 | { |
| 498 | 498 | $conditions = $this->constructConditions($conditions, $this->model); |
| 499 | 499 | $sort = $desc ? 'desc' : 'asc'; |
| 500 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 500 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 501 | 501 | } |
| 502 | 502 | |
| 503 | 503 | /** |
@@ -537,7 +537,7 @@ discard block |
||
| 537 | 537 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 540 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | /** |
@@ -552,7 +552,7 @@ discard block |
||
| 552 | 552 | |
| 553 | 553 | if ( ! $model) |
| 554 | 554 | { |
| 555 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 555 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | $model->restore(); |
@@ -572,13 +572,13 @@ discard block |
||
| 572 | 572 | if ($key == 'and') |
| 573 | 573 | { |
| 574 | 574 | $conditions = $this->constructConditions($value, $model); |
| 575 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 575 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 576 | 576 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
| 577 | 577 | } |
| 578 | 578 | else if ($key == 'or') |
| 579 | 579 | { |
| 580 | 580 | $conditions = $this->constructConditions($value, $model); |
| 581 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 581 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 582 | 582 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
| 583 | 583 | } |
| 584 | 584 | else |
@@ -603,41 +603,41 @@ discard block |
||
| 603 | 603 | |
| 604 | 604 | if (strtolower($operator) == 'between') |
| 605 | 605 | { |
| 606 | - $conditionString .= $key . ' >= ? and '; |
|
| 606 | + $conditionString .= $key.' >= ? and '; |
|
| 607 | 607 | $conditionValues[] = $value1; |
| 608 | 608 | |
| 609 | - $conditionString .= $key . ' <= ? {op} '; |
|
| 609 | + $conditionString .= $key.' <= ? {op} '; |
|
| 610 | 610 | $conditionValues[] = $value2; |
| 611 | 611 | } |
| 612 | 612 | elseif (strtolower($operator) == 'in') |
| 613 | 613 | { |
| 614 | 614 | $conditionValues = array_merge($conditionValues, $value); |
| 615 | 615 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
| 616 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 616 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 617 | 617 | } |
| 618 | 618 | elseif (strtolower($operator) == 'null') |
| 619 | 619 | { |
| 620 | - $conditionString .= $key . ' is null {op} '; |
|
| 620 | + $conditionString .= $key.' is null {op} '; |
|
| 621 | 621 | } |
| 622 | 622 | elseif (strtolower($operator) == 'not null') |
| 623 | 623 | { |
| 624 | - $conditionString .= $key . ' is not null {op} '; |
|
| 624 | + $conditionString .= $key.' is not null {op} '; |
|
| 625 | 625 | } |
| 626 | 626 | elseif (strtolower($operator) == 'has') |
| 627 | 627 | { |
| 628 | 628 | $sql = $model->withTrashed()->has($key)->toSql(); |
| 629 | 629 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
| 630 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 630 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 631 | 631 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
| 632 | 632 | } |
| 633 | 633 | else |
| 634 | 634 | { |
| 635 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 635 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 636 | 636 | $conditionValues[] = $value; |
| 637 | 637 | } |
| 638 | 638 | } |
| 639 | 639 | } |
| 640 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 640 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 641 | 641 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
| 642 | 642 | } |
| 643 | 643 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * Generate headers for the given route. |
| 92 | 92 | * |
| 93 | 93 | * @param array &$route |
| 94 | - * @param object $reflectionClass |
|
| 94 | + * @param \ReflectionClass $reflectionClass |
|
| 95 | 95 | * @param string $method |
| 96 | 96 | * @param array $skipLoginCheck |
| 97 | 97 | * @return void |
@@ -118,6 +118,7 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @param array &$route |
| 120 | 120 | * @param object $reflectionMethod] |
| 121 | + * @param \ReflectionMethod $reflectionMethod |
|
| 121 | 122 | * @return void |
| 122 | 123 | */ |
| 123 | 124 | protected function processDocBlock(&$route, $reflectionMethod) |
@@ -140,7 +141,7 @@ discard block |
||
| 140 | 141 | * Generate post body for the given route. |
| 141 | 142 | * |
| 142 | 143 | * @param array &$route |
| 143 | - * @param object $reflectionMethod |
|
| 144 | + * @param \ReflectionMethod $reflectionMethod |
|
| 144 | 145 | * @param array $validationRules |
| 145 | 146 | * @return void |
| 146 | 147 | */ |
@@ -156,8 +156,7 @@ discard block |
||
| 156 | 156 | if ($match[1] == '$this->validationRules') |
| 157 | 157 | { |
| 158 | 158 | $route['body'] = $validationRules; |
| 159 | - } |
|
| 160 | - else |
|
| 159 | + } else |
|
| 161 | 160 | { |
| 162 | 161 | $route['body'] = eval('return ' . $match[1] . ';'); |
| 163 | 162 | } |
@@ -167,14 +166,12 @@ discard block |
||
| 167 | 166 | if(strpos($rule, 'unique')) |
| 168 | 167 | { |
| 169 | 168 | $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
| 170 | - } |
|
| 171 | - elseif(strpos($rule, 'exists')) |
|
| 169 | + } elseif(strpos($rule, 'exists')) |
|
| 172 | 170 | { |
| 173 | 171 | $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
| 174 | 172 | } |
| 175 | 173 | } |
| 176 | - } |
|
| 177 | - else |
|
| 174 | + } else |
|
| 178 | 175 | { |
| 179 | 176 | $route['body'] = 'conditions'; |
| 180 | 177 | } |
@@ -6,270 +6,270 @@ |
||
| 6 | 6 | |
| 7 | 7 | class GenerateDoc extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The name and signature of the console command. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $signature = 'doc:generate'; |
|
| 9 | + /** |
|
| 10 | + * The name and signature of the console command. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $signature = 'doc:generate'; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The console command description. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected $description = 'Generate api documentation'; |
|
| 16 | + /** |
|
| 17 | + * The console command description. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected $description = 'Generate api documentation'; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Create a new command instance. |
|
| 25 | - * |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function __construct() |
|
| 29 | - { |
|
| 30 | - parent::__construct(); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Create a new command instance. |
|
| 25 | + * |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function __construct() |
|
| 29 | + { |
|
| 30 | + parent::__construct(); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Execute the console command. |
|
| 35 | - * |
|
| 36 | - * @return mixed |
|
| 37 | - */ |
|
| 38 | - public function handle() |
|
| 39 | - { |
|
| 40 | - $docData = []; |
|
| 41 | - $docData['models'] = []; |
|
| 42 | - $routes = $this->getRoutes(); |
|
| 43 | - foreach ($routes as $route) |
|
| 44 | - { |
|
| 45 | - if ($route) |
|
| 46 | - { |
|
| 47 | - $actoinArray = explode('@', $route['action']); |
|
| 48 | - $controller = $actoinArray[0]; |
|
| 49 | - $method = $actoinArray[1]; |
|
| 50 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 33 | + /** |
|
| 34 | + * Execute the console command. |
|
| 35 | + * |
|
| 36 | + * @return mixed |
|
| 37 | + */ |
|
| 38 | + public function handle() |
|
| 39 | + { |
|
| 40 | + $docData = []; |
|
| 41 | + $docData['models'] = []; |
|
| 42 | + $routes = $this->getRoutes(); |
|
| 43 | + foreach ($routes as $route) |
|
| 44 | + { |
|
| 45 | + if ($route) |
|
| 46 | + { |
|
| 47 | + $actoinArray = explode('@', $route['action']); |
|
| 48 | + $controller = $actoinArray[0]; |
|
| 49 | + $method = $actoinArray[1]; |
|
| 50 | + $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 51 | 51 | |
| 52 | - $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | - $skipLoginCheck = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false; |
|
| 56 | - $validationRules = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false; |
|
| 52 | + $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | + $skipLoginCheck = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false; |
|
| 56 | + $validationRules = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false; |
|
| 57 | 57 | |
| 58 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name']); |
|
| 58 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name']); |
|
| 59 | 59 | |
| 60 | - $this->processDocBlock($route, $reflectionMethod); |
|
| 61 | - $this->getHeaders($route, $reflectionClass, $method, $skipLoginCheck); |
|
| 62 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 60 | + $this->processDocBlock($route, $reflectionMethod); |
|
| 61 | + $this->getHeaders($route, $reflectionClass, $method, $skipLoginCheck); |
|
| 62 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 63 | 63 | |
| 64 | - preg_match('/api\/v1\/([^#]+)\//iU', $route['uri'], $module); |
|
| 65 | - preg_match('/api\/v1\/' . $module[1] . '\/([^#]+)\//iU', $route['uri'], $model); |
|
| 66 | - $docData['modules'][$module[1]][$model[1]][] = $route; |
|
| 64 | + preg_match('/api\/v1\/([^#]+)\//iU', $route['uri'], $module); |
|
| 65 | + preg_match('/api\/v1\/' . $module[1] . '\/([^#]+)\//iU', $route['uri'], $model); |
|
| 66 | + $docData['modules'][$module[1]][$model[1]][] = $route; |
|
| 67 | 67 | |
| 68 | - $this->getModels($classProperties['model'], $docData); |
|
| 69 | - } |
|
| 70 | - } |
|
| 68 | + $this->getModels($classProperties['model'], $docData); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $docData['errors'] = $this->getErrors(); |
|
| 73 | - \File::put(app_path('Modules/V1/Core/Resources/api.json'), json_encode($docData)); |
|
| 74 | - } |
|
| 72 | + $docData['errors'] = $this->getErrors(); |
|
| 73 | + \File::put(app_path('Modules/V1/Core/Resources/api.json'), json_encode($docData)); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Get list of all registered routes. |
|
| 78 | - * |
|
| 79 | - * @return collection |
|
| 80 | - */ |
|
| 81 | - protected function getRoutes() |
|
| 82 | - { |
|
| 83 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 84 | - if (strpos($route->uri(), 'api/v') !== false) |
|
| 85 | - { |
|
| 86 | - return [ |
|
| 87 | - 'method' => $route->methods()[0], |
|
| 88 | - 'uri' => $route->uri(), |
|
| 89 | - 'action' => $route->getActionName() |
|
| 90 | - ]; |
|
| 91 | - } |
|
| 92 | - return false; |
|
| 93 | - })->all(); |
|
| 94 | - } |
|
| 76 | + /** |
|
| 77 | + * Get list of all registered routes. |
|
| 78 | + * |
|
| 79 | + * @return collection |
|
| 80 | + */ |
|
| 81 | + protected function getRoutes() |
|
| 82 | + { |
|
| 83 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
| 84 | + if (strpos($route->uri(), 'api/v') !== false) |
|
| 85 | + { |
|
| 86 | + return [ |
|
| 87 | + 'method' => $route->methods()[0], |
|
| 88 | + 'uri' => $route->uri(), |
|
| 89 | + 'action' => $route->getActionName() |
|
| 90 | + ]; |
|
| 91 | + } |
|
| 92 | + return false; |
|
| 93 | + })->all(); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Generate headers for the given route. |
|
| 98 | - * |
|
| 99 | - * @param array &$route |
|
| 100 | - * @param object $reflectionClass |
|
| 101 | - * @param string $method |
|
| 102 | - * @param array $skipLoginCheck |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - protected function getHeaders(&$route, $reflectionClass, $method, $skipLoginCheck) |
|
| 106 | - { |
|
| 107 | - $route['headers'] = [ |
|
| 108 | - 'Accept' => 'application/json', |
|
| 109 | - 'Content-Type' => 'application/json', |
|
| 110 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 111 | - 'time-zone-diff' => 'Timezone difference between UTC and Local Time', |
|
| 112 | - ]; |
|
| 96 | + /** |
|
| 97 | + * Generate headers for the given route. |
|
| 98 | + * |
|
| 99 | + * @param array &$route |
|
| 100 | + * @param object $reflectionClass |
|
| 101 | + * @param string $method |
|
| 102 | + * @param array $skipLoginCheck |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + protected function getHeaders(&$route, $reflectionClass, $method, $skipLoginCheck) |
|
| 106 | + { |
|
| 107 | + $route['headers'] = [ |
|
| 108 | + 'Accept' => 'application/json', |
|
| 109 | + 'Content-Type' => 'application/json', |
|
| 110 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 111 | + 'time-zone-diff' => 'Timezone difference between UTC and Local Time', |
|
| 112 | + ]; |
|
| 113 | 113 | |
| 114 | 114 | |
| 115 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 116 | - { |
|
| 117 | - $route['headers']['Authrization'] = 'bearer {token}'; |
|
| 118 | - } |
|
| 119 | - } |
|
| 115 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 116 | + { |
|
| 117 | + $route['headers']['Authrization'] = 'bearer {token}'; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Generate description and params for the given route |
|
| 123 | - * based on the docblock. |
|
| 124 | - * |
|
| 125 | - * @param array &$route |
|
| 126 | - * @param object $reflectionMethod] |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
| 130 | - { |
|
| 131 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 132 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 133 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 134 | - $params = $docblock->getTagsByName('param'); |
|
| 135 | - foreach ($params as $param) |
|
| 136 | - { |
|
| 137 | - $name = $param->getVariableName(); |
|
| 138 | - if ($name !== 'request') |
|
| 139 | - { |
|
| 140 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 121 | + /** |
|
| 122 | + * Generate description and params for the given route |
|
| 123 | + * based on the docblock. |
|
| 124 | + * |
|
| 125 | + * @param array &$route |
|
| 126 | + * @param object $reflectionMethod] |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
| 130 | + { |
|
| 131 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 132 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 133 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 134 | + $params = $docblock->getTagsByName('param'); |
|
| 135 | + foreach ($params as $param) |
|
| 136 | + { |
|
| 137 | + $name = $param->getVariableName(); |
|
| 138 | + if ($name !== 'request') |
|
| 139 | + { |
|
| 140 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Generate post body for the given route. |
|
| 147 | - * |
|
| 148 | - * @param array &$route |
|
| 149 | - * @param object $reflectionMethod |
|
| 150 | - * @param array $validationRules |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 154 | - { |
|
| 155 | - if ($route['method'] == 'POST') |
|
| 156 | - { |
|
| 157 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 145 | + /** |
|
| 146 | + * Generate post body for the given route. |
|
| 147 | + * |
|
| 148 | + * @param array &$route |
|
| 149 | + * @param object $reflectionMethod |
|
| 150 | + * @param array $validationRules |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 154 | + { |
|
| 155 | + if ($route['method'] == 'POST') |
|
| 156 | + { |
|
| 157 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 158 | 158 | |
| 159 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 160 | - if (count($match)) |
|
| 161 | - { |
|
| 162 | - if ($match[1] == '$this->validationRules') |
|
| 163 | - { |
|
| 164 | - $route['body'] = $validationRules; |
|
| 165 | - } |
|
| 166 | - else |
|
| 167 | - { |
|
| 168 | - $route['body'] = eval('return ' . $match[1] . ';'); |
|
| 169 | - } |
|
| 159 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 160 | + if (count($match)) |
|
| 161 | + { |
|
| 162 | + if ($match[1] == '$this->validationRules') |
|
| 163 | + { |
|
| 164 | + $route['body'] = $validationRules; |
|
| 165 | + } |
|
| 166 | + else |
|
| 167 | + { |
|
| 168 | + $route['body'] = eval('return ' . $match[1] . ';'); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - foreach ($route['body'] as &$rule) |
|
| 172 | - { |
|
| 173 | - if(strpos($rule, 'unique')) |
|
| 174 | - { |
|
| 175 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 176 | - } |
|
| 177 | - elseif(strpos($rule, 'exists')) |
|
| 178 | - { |
|
| 179 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - else |
|
| 184 | - { |
|
| 185 | - $route['body'] = 'conditions'; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } |
|
| 171 | + foreach ($route['body'] as &$rule) |
|
| 172 | + { |
|
| 173 | + if(strpos($rule, 'unique')) |
|
| 174 | + { |
|
| 175 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 176 | + } |
|
| 177 | + elseif(strpos($rule, 'exists')) |
|
| 178 | + { |
|
| 179 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + else |
|
| 184 | + { |
|
| 185 | + $route['body'] = 'conditions'; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Generate application errors. |
|
| 192 | - * |
|
| 193 | - * @return array |
|
| 194 | - */ |
|
| 195 | - protected function getErrors() |
|
| 196 | - { |
|
| 197 | - $errors = []; |
|
| 198 | - $reflectionClass = new \ReflectionClass('App\Modules\V1\Core\Utl\ErrorHandler'); |
|
| 199 | - foreach ($reflectionClass->getMethods() as $method) |
|
| 200 | - { |
|
| 201 | - $methodName = $method->getName(); |
|
| 202 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 203 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 190 | + /** |
|
| 191 | + * Generate application errors. |
|
| 192 | + * |
|
| 193 | + * @return array |
|
| 194 | + */ |
|
| 195 | + protected function getErrors() |
|
| 196 | + { |
|
| 197 | + $errors = []; |
|
| 198 | + $reflectionClass = new \ReflectionClass('App\Modules\V1\Core\Utl\ErrorHandler'); |
|
| 199 | + foreach ($reflectionClass->getMethods() as $method) |
|
| 200 | + { |
|
| 201 | + $methodName = $method->getName(); |
|
| 202 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 203 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 204 | 204 | |
| 205 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 205 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 206 | 206 | |
| 207 | - if (count($match)) |
|
| 208 | - { |
|
| 209 | - $errors[$match[1]][] = $methodName; |
|
| 210 | - } |
|
| 211 | - } |
|
| 207 | + if (count($match)) |
|
| 208 | + { |
|
| 209 | + $errors[$match[1]][] = $methodName; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - return $errors; |
|
| 214 | - } |
|
| 213 | + return $errors; |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - /** |
|
| 217 | - * Get the given method body code. |
|
| 218 | - * |
|
| 219 | - * @param object $reflectionMethod |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - protected function getMethodBody($reflectionMethod) |
|
| 223 | - { |
|
| 224 | - $filename = $reflectionMethod->getFileName(); |
|
| 225 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 226 | - $end_line = $reflectionMethod->getEndLine(); |
|
| 227 | - $length = $end_line - $start_line; |
|
| 228 | - $source = file($filename); |
|
| 229 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
| 230 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 216 | + /** |
|
| 217 | + * Get the given method body code. |
|
| 218 | + * |
|
| 219 | + * @param object $reflectionMethod |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + protected function getMethodBody($reflectionMethod) |
|
| 223 | + { |
|
| 224 | + $filename = $reflectionMethod->getFileName(); |
|
| 225 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 226 | + $end_line = $reflectionMethod->getEndLine(); |
|
| 227 | + $length = $end_line - $start_line; |
|
| 228 | + $source = file($filename); |
|
| 229 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
| 230 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 231 | 231 | |
| 232 | - return $body; |
|
| 233 | - } |
|
| 232 | + return $body; |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * Get example object of all availble models. |
|
| 237 | - * |
|
| 238 | - * @param string $modelName |
|
| 239 | - * @param object $docData |
|
| 240 | - * @return string |
|
| 241 | - */ |
|
| 242 | - protected function getModels($modelName, &$docData) |
|
| 243 | - { |
|
| 244 | - if ($modelName && ! array_key_exists($modelName, $docData['models'])) |
|
| 245 | - { |
|
| 246 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 247 | - $model = factory($modelClass)->make(); |
|
| 248 | - $modelArr = $model->toArray(); |
|
| 235 | + /** |
|
| 236 | + * Get example object of all availble models. |
|
| 237 | + * |
|
| 238 | + * @param string $modelName |
|
| 239 | + * @param object $docData |
|
| 240 | + * @return string |
|
| 241 | + */ |
|
| 242 | + protected function getModels($modelName, &$docData) |
|
| 243 | + { |
|
| 244 | + if ($modelName && ! array_key_exists($modelName, $docData['models'])) |
|
| 245 | + { |
|
| 246 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 247 | + $model = factory($modelClass)->make(); |
|
| 248 | + $modelArr = $model->toArray(); |
|
| 249 | 249 | |
| 250 | - if ( $model->trans && ! $model->trans->count()) |
|
| 251 | - { |
|
| 252 | - $modelArr['trans'] = [ |
|
| 253 | - 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
| 254 | - ]; |
|
| 255 | - } |
|
| 250 | + if ( $model->trans && ! $model->trans->count()) |
|
| 251 | + { |
|
| 252 | + $modelArr['trans'] = [ |
|
| 253 | + 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
| 254 | + ]; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 258 | - } |
|
| 259 | - } |
|
| 257 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Get the route response object type. |
|
| 263 | - * |
|
| 264 | - * @param string $modelName |
|
| 265 | - * @param string $method |
|
| 266 | - * @return array |
|
| 267 | - */ |
|
| 268 | - protected function getResponseObject($modelName, $method) |
|
| 269 | - { |
|
| 270 | - $config = \CoreConfig::getConfig(); |
|
| 271 | - $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false; |
|
| 261 | + /** |
|
| 262 | + * Get the route response object type. |
|
| 263 | + * |
|
| 264 | + * @param string $modelName |
|
| 265 | + * @param string $method |
|
| 266 | + * @return array |
|
| 267 | + */ |
|
| 268 | + protected function getResponseObject($modelName, $method) |
|
| 269 | + { |
|
| 270 | + $config = \CoreConfig::getConfig(); |
|
| 271 | + $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false; |
|
| 272 | 272 | |
| 273 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 274 | - } |
|
| 273 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 274 | + } |
|
| 275 | 275 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | $this->getPostData($route, $reflectionMethod, $validationRules); |
| 63 | 63 | |
| 64 | 64 | preg_match('/api\/v1\/([^#]+)\//iU', $route['uri'], $module); |
| 65 | - preg_match('/api\/v1\/' . $module[1] . '\/([^#]+)\//iU', $route['uri'], $model); |
|
| 65 | + preg_match('/api\/v1\/'.$module[1].'\/([^#]+)\//iU', $route['uri'], $model); |
|
| 66 | 66 | $docData['modules'][$module[1]][$model[1]][] = $route; |
| 67 | 67 | |
| 68 | 68 | $this->getModels($classProperties['model'], $docData); |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | */ |
| 81 | 81 | protected function getRoutes() |
| 82 | 82 | { |
| 83 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 83 | + return collect(\Route::getRoutes())->map(function($route) { |
|
| 84 | 84 | if (strpos($route->uri(), 'api/v') !== false) |
| 85 | 85 | { |
| 86 | 86 | return [ |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | ]; |
| 113 | 113 | |
| 114 | 114 | |
| 115 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 115 | + if ( ! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 116 | 116 | { |
| 117 | 117 | $route['headers']['Authrization'] = 'bearer {token}'; |
| 118 | 118 | } |
@@ -165,16 +165,16 @@ discard block |
||
| 165 | 165 | } |
| 166 | 166 | else |
| 167 | 167 | { |
| 168 | - $route['body'] = eval('return ' . $match[1] . ';'); |
|
| 168 | + $route['body'] = eval('return '.$match[1].';'); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | foreach ($route['body'] as &$rule) |
| 172 | 172 | { |
| 173 | - if(strpos($rule, 'unique')) |
|
| 173 | + if (strpos($rule, 'unique')) |
|
| 174 | 174 | { |
| 175 | 175 | $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
| 176 | 176 | } |
| 177 | - elseif(strpos($rule, 'exists')) |
|
| 177 | + elseif (strpos($rule, 'exists')) |
|
| 178 | 178 | { |
| 179 | 179 | $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
| 180 | 180 | } |
@@ -247,10 +247,10 @@ discard block |
||
| 247 | 247 | $model = factory($modelClass)->make(); |
| 248 | 248 | $modelArr = $model->toArray(); |
| 249 | 249 | |
| 250 | - if ( $model->trans && ! $model->trans->count()) |
|
| 250 | + if ($model->trans && ! $model->trans->count()) |
|
| 251 | 251 | { |
| 252 | 252 | $modelArr['trans'] = [ |
| 253 | - 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
| 253 | + 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 254 | 254 | ]; |
| 255 | 255 | } |
| 256 | 256 | |
@@ -2,105 +2,105 @@ |
||
| 2 | 2 | |
| 3 | 3 | class ErrorHandler |
| 4 | 4 | { |
| 5 | - public function unAuthorized() |
|
| 6 | - { |
|
| 7 | - $error = ['status' => 401, 'message' => trans('errors.unAuthorized')]; |
|
| 8 | - abort($error['status'], $error['message']); |
|
| 9 | - } |
|
| 10 | - |
|
| 11 | - public function tokenExpired() |
|
| 12 | - { |
|
| 13 | - $error = ['status' => 403, 'message' => trans('errors.tokenExpired')]; |
|
| 14 | - abort($error['status'], $error['message']); |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - public function noPermissions() |
|
| 18 | - { |
|
| 19 | - $error = ['status' => 403, 'message' => trans('errors.noPermissions')]; |
|
| 20 | - abort($error['status'], $error['message']); |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function loginFailed() |
|
| 24 | - { |
|
| 25 | - $error = ['status' => 400, 'message' => trans('errors.loginFailed')]; |
|
| 26 | - abort($error['status'], $error['message']); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function noSocialEmail() |
|
| 30 | - { |
|
| 31 | - $error = ['status' => 400, 'message' => trans('errors.noSocialEmail')]; |
|
| 32 | - abort($error['status'], $error['message']); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function userAlreadyRegistered() |
|
| 36 | - { |
|
| 37 | - $error = ['status' => 400, 'message' => trans('errors.userAlreadyRegistered')]; |
|
| 38 | - abort($error['status'], $error['message']); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function connectionError() |
|
| 42 | - { |
|
| 43 | - $error = ['status' => 400, 'message' => trans('errors.connectionError')]; |
|
| 44 | - abort($error['status'], $error['message']); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function redisNotRunning() |
|
| 48 | - { |
|
| 49 | - $error = ['status' => 400, 'message' => trans('errors.redisNotRunning')]; |
|
| 50 | - abort($error['status'], $error['message']); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function dbQueryError() |
|
| 54 | - { |
|
| 55 | - $error = ['status' => 400, 'message' => trans('errors.dbQueryError')]; |
|
| 56 | - abort($error['status'], $error['message']); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function cannotCreateSetting() |
|
| 60 | - { |
|
| 61 | - $error = ['status' => 400, 'message' => trans('errors.cannotCreateSetting')]; |
|
| 62 | - abort($error['status'], $error['message']); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public function cannotUpdateSettingKey() |
|
| 66 | - { |
|
| 67 | - $error = ['status' => 400, 'message' => trans('errors.cannotUpdateSettingKey')]; |
|
| 68 | - abort($error['status'], $error['message']); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function userIsBlocked() |
|
| 72 | - { |
|
| 73 | - $error = ['status' => 403, 'message' => trans('errors.userIsBlocked')]; |
|
| 74 | - abort($error['status'], $error['message']); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function invalidResetToken() |
|
| 78 | - { |
|
| 79 | - $error = ['status' => 400, 'message' => trans('errors.invalidResetToken')]; |
|
| 80 | - abort($error['status'], $error['message']); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function invalidResetPassword() |
|
| 84 | - { |
|
| 85 | - $error = ['status' => 400, 'message' => trans('errors.invalidResetPassword')]; |
|
| 86 | - abort($error['status'], $error['message']); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function invalidOldPassword() |
|
| 90 | - { |
|
| 91 | - $error = ['status' => 400, 'message' => trans('errors.invalidOldPassword')]; |
|
| 92 | - abort($error['status'], $error['message']); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function notFound($text) |
|
| 96 | - { |
|
| 97 | - $error = ['status' => 404, 'message' => trans('errors.notFound', ['replace' => $text])]; |
|
| 98 | - abort($error['status'], $error['message']); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function generalError() |
|
| 102 | - { |
|
| 103 | - $error = ['status' => 400, 'message' => trans('errors.generalError')]; |
|
| 104 | - abort($error['status'], $error['message']); |
|
| 105 | - } |
|
| 5 | + public function unAuthorized() |
|
| 6 | + { |
|
| 7 | + $error = ['status' => 401, 'message' => trans('errors.unAuthorized')]; |
|
| 8 | + abort($error['status'], $error['message']); |
|
| 9 | + } |
|
| 10 | + |
|
| 11 | + public function tokenExpired() |
|
| 12 | + { |
|
| 13 | + $error = ['status' => 403, 'message' => trans('errors.tokenExpired')]; |
|
| 14 | + abort($error['status'], $error['message']); |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + public function noPermissions() |
|
| 18 | + { |
|
| 19 | + $error = ['status' => 403, 'message' => trans('errors.noPermissions')]; |
|
| 20 | + abort($error['status'], $error['message']); |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public function loginFailed() |
|
| 24 | + { |
|
| 25 | + $error = ['status' => 400, 'message' => trans('errors.loginFailed')]; |
|
| 26 | + abort($error['status'], $error['message']); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function noSocialEmail() |
|
| 30 | + { |
|
| 31 | + $error = ['status' => 400, 'message' => trans('errors.noSocialEmail')]; |
|
| 32 | + abort($error['status'], $error['message']); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function userAlreadyRegistered() |
|
| 36 | + { |
|
| 37 | + $error = ['status' => 400, 'message' => trans('errors.userAlreadyRegistered')]; |
|
| 38 | + abort($error['status'], $error['message']); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function connectionError() |
|
| 42 | + { |
|
| 43 | + $error = ['status' => 400, 'message' => trans('errors.connectionError')]; |
|
| 44 | + abort($error['status'], $error['message']); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function redisNotRunning() |
|
| 48 | + { |
|
| 49 | + $error = ['status' => 400, 'message' => trans('errors.redisNotRunning')]; |
|
| 50 | + abort($error['status'], $error['message']); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function dbQueryError() |
|
| 54 | + { |
|
| 55 | + $error = ['status' => 400, 'message' => trans('errors.dbQueryError')]; |
|
| 56 | + abort($error['status'], $error['message']); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function cannotCreateSetting() |
|
| 60 | + { |
|
| 61 | + $error = ['status' => 400, 'message' => trans('errors.cannotCreateSetting')]; |
|
| 62 | + abort($error['status'], $error['message']); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public function cannotUpdateSettingKey() |
|
| 66 | + { |
|
| 67 | + $error = ['status' => 400, 'message' => trans('errors.cannotUpdateSettingKey')]; |
|
| 68 | + abort($error['status'], $error['message']); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function userIsBlocked() |
|
| 72 | + { |
|
| 73 | + $error = ['status' => 403, 'message' => trans('errors.userIsBlocked')]; |
|
| 74 | + abort($error['status'], $error['message']); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function invalidResetToken() |
|
| 78 | + { |
|
| 79 | + $error = ['status' => 400, 'message' => trans('errors.invalidResetToken')]; |
|
| 80 | + abort($error['status'], $error['message']); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function invalidResetPassword() |
|
| 84 | + { |
|
| 85 | + $error = ['status' => 400, 'message' => trans('errors.invalidResetPassword')]; |
|
| 86 | + abort($error['status'], $error['message']); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function invalidOldPassword() |
|
| 90 | + { |
|
| 91 | + $error = ['status' => 400, 'message' => trans('errors.invalidOldPassword')]; |
|
| 92 | + abort($error['status'], $error['message']); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function notFound($text) |
|
| 96 | + { |
|
| 97 | + $error = ['status' => 404, 'message' => trans('errors.notFound', ['replace' => $text])]; |
|
| 98 | + abort($error['status'], $error['message']); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function generalError() |
|
| 102 | + { |
|
| 103 | + $error = ['status' => 400, 'message' => trans('errors.generalError')]; |
|
| 104 | + abort($error['status'], $error['message']); |
|
| 105 | + } |
|
| 106 | 106 | } |
| 107 | 107 | \ No newline at end of file |
@@ -7,50 +7,50 @@ |
||
| 7 | 7 | class ModuleServiceProvider extends ServiceProvider |
| 8 | 8 | { |
| 9 | 9 | /** |
| 10 | - * Bootstrap the module services. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function boot() |
|
| 15 | - { |
|
| 16 | - $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'core'); |
|
| 17 | - $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'core'); |
|
| 18 | - |
|
| 19 | - $factory = app('Illuminate\Database\Eloquent\Factory'); |
|
| 20 | - $factory->load(__DIR__.'/../Database/Factories'); |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Register the module services. |
|
| 25 | - * |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function register() |
|
| 29 | - { |
|
| 30 | - //Bind Core Facade to the IoC Container |
|
| 31 | - \App::bind('Core', function() |
|
| 32 | - { |
|
| 33 | - return new \App\Modules\V1\Core\Core; |
|
| 34 | - }); |
|
| 35 | - |
|
| 36 | - //Bind ErrorHandler Facade to the IoC Container |
|
| 37 | - \App::bind('ErrorHandler', function() |
|
| 38 | - { |
|
| 39 | - return new \App\Modules\V1\Core\Utl\ErrorHandler; |
|
| 40 | - }); |
|
| 41 | - |
|
| 42 | - //Bind CoreConfig Facade to the IoC Container |
|
| 43 | - \App::bind('CoreConfig', function() |
|
| 44 | - { |
|
| 45 | - return new \App\Modules\V1\Core\Utl\CoreConfig; |
|
| 46 | - }); |
|
| 47 | - |
|
| 48 | - //Bind Logging Facade to the IoC Container |
|
| 49 | - \App::bind('Logging', function() |
|
| 50 | - { |
|
| 51 | - return new \App\Modules\V1\Core\Utl\Logging; |
|
| 52 | - }); |
|
| 10 | + * Bootstrap the module services. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function boot() |
|
| 15 | + { |
|
| 16 | + $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'core'); |
|
| 17 | + $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'core'); |
|
| 18 | + |
|
| 19 | + $factory = app('Illuminate\Database\Eloquent\Factory'); |
|
| 20 | + $factory->load(__DIR__.'/../Database/Factories'); |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Register the module services. |
|
| 25 | + * |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function register() |
|
| 29 | + { |
|
| 30 | + //Bind Core Facade to the IoC Container |
|
| 31 | + \App::bind('Core', function() |
|
| 32 | + { |
|
| 33 | + return new \App\Modules\V1\Core\Core; |
|
| 34 | + }); |
|
| 35 | + |
|
| 36 | + //Bind ErrorHandler Facade to the IoC Container |
|
| 37 | + \App::bind('ErrorHandler', function() |
|
| 38 | + { |
|
| 39 | + return new \App\Modules\V1\Core\Utl\ErrorHandler; |
|
| 40 | + }); |
|
| 41 | + |
|
| 42 | + //Bind CoreConfig Facade to the IoC Container |
|
| 43 | + \App::bind('CoreConfig', function() |
|
| 44 | + { |
|
| 45 | + return new \App\Modules\V1\Core\Utl\CoreConfig; |
|
| 46 | + }); |
|
| 47 | + |
|
| 48 | + //Bind Logging Facade to the IoC Container |
|
| 49 | + \App::bind('Logging', function() |
|
| 50 | + { |
|
| 51 | + return new \App\Modules\V1\Core\Utl\Logging; |
|
| 52 | + }); |
|
| 53 | 53 | |
| 54 | - $this->app->register(RouteServiceProvider::class); |
|
| 55 | - } |
|
| 54 | + $this->app->register(RouteServiceProvider::class); |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -7,26 +7,26 @@ |
||
| 7 | 7 | class ModuleServiceProvider extends ServiceProvider |
| 8 | 8 | { |
| 9 | 9 | /** |
| 10 | - * Bootstrap the module services. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function boot() |
|
| 15 | - { |
|
| 16 | - $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'reporting'); |
|
| 17 | - $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'reporting'); |
|
| 10 | + * Bootstrap the module services. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function boot() |
|
| 15 | + { |
|
| 16 | + $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'reporting'); |
|
| 17 | + $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'reporting'); |
|
| 18 | 18 | |
| 19 | - $factory = app('Illuminate\Database\Eloquent\Factory'); |
|
| 20 | - $factory->load(__DIR__.'/../Database/Factories'); |
|
| 21 | - } |
|
| 19 | + $factory = app('Illuminate\Database\Eloquent\Factory'); |
|
| 20 | + $factory->load(__DIR__.'/../Database/Factories'); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Register the module services. |
|
| 25 | - * |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function register() |
|
| 29 | - { |
|
| 30 | - $this->app->register(RouteServiceProvider::class); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Register the module services. |
|
| 25 | + * |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function register() |
|
| 29 | + { |
|
| 30 | + $this->app->register(RouteServiceProvider::class); |
|
| 31 | + } |
|
| 32 | 32 | } |