@@ -12,5 +12,5 @@ |
||
12 | 12 | */ |
13 | 13 | |
14 | 14 | Broadcast::channel('users.{id}', function ($user, $id) { |
15 | - return (int) $user->id === (int) $id; |
|
15 | + return (int) $user->id === (int) $id; |
|
16 | 16 | }); |
@@ -11,6 +11,6 @@ |
||
11 | 11 | | |
12 | 12 | */ |
13 | 13 | |
14 | -Broadcast::channel('users.{id}', function ($user, $id) { |
|
14 | +Broadcast::channel('users.{id}', function($user, $id) { |
|
15 | 15 | return (int) $user->id === (int) $id; |
16 | 16 | }); |
@@ -2,7 +2,6 @@ |
||
2 | 2 | |
3 | 3 | use App\User; |
4 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; |
5 | -use Illuminate\Notifications\Notifiable; |
|
6 | 5 | use Laravel\Passport\HasApiTokens; |
7 | 6 | |
8 | 7 | class AclUser extends User |
@@ -8,133 +8,133 @@ |
||
8 | 8 | class AclUser extends User |
9 | 9 | { |
10 | 10 | |
11 | - use SoftDeletes, HasApiTokens; |
|
12 | - protected $table = 'users'; |
|
13 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
14 | - protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
15 | - protected $guarded = ['id']; |
|
16 | - protected $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'time_zone']; |
|
17 | - public $searchable = ['name', 'email']; |
|
11 | + use SoftDeletes, HasApiTokens; |
|
12 | + protected $table = 'users'; |
|
13 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
14 | + protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
15 | + protected $guarded = ['id']; |
|
16 | + protected $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'time_zone']; |
|
17 | + public $searchable = ['name', 'email']; |
|
18 | 18 | |
19 | - public function getCreatedAtAttribute($value) |
|
20 | - { |
|
21 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
22 | - } |
|
23 | - |
|
24 | - public function getUpdatedAtAttribute($value) |
|
25 | - { |
|
26 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
27 | - } |
|
28 | - |
|
29 | - public function getDeletedAtAttribute($value) |
|
30 | - { |
|
31 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Get the profile picture url. |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function getProfilePictureAttribute($value) |
|
39 | - { |
|
40 | - return url(\Storage::url($value)); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Encrypt the password attribute before |
|
45 | - * saving it in the storage. |
|
46 | - * |
|
47 | - * @param string $value |
|
48 | - */ |
|
49 | - public function setPasswordAttribute($value) |
|
50 | - { |
|
51 | - $this->attributes['password'] = bcrypt($value); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Get the entity's notifications. |
|
56 | - */ |
|
57 | - public function notifications() |
|
58 | - { |
|
59 | - return $this->morphMany('\App\Modules\Notifications\Notification', 'notifiable')->orderBy('created_at', 'desc'); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Get the entity's read notifications. |
|
64 | - */ |
|
65 | - public function readNotifications() |
|
66 | - { |
|
67 | - return $this->notifications()->whereNotNull('read_at'); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Get the entity's unread notifications. |
|
72 | - */ |
|
73 | - public function unreadNotifications() |
|
74 | - { |
|
75 | - return $this->notifications()->whereNull('read_at'); |
|
76 | - } |
|
77 | - |
|
78 | - public function groups() |
|
79 | - { |
|
80 | - return $this->belongsToMany('\App\Modules\Acl\AclGroup', 'users_groups', 'user_id', 'group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
81 | - } |
|
82 | - |
|
83 | - public function oauthClients() |
|
84 | - { |
|
85 | - return $this->hasMany('App\Modules\Acl\OauthClient', 'user_id'); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Return fcm device tokens that will be used in sending fcm notifications. |
|
90 | - * |
|
91 | - * @return array |
|
92 | - */ |
|
93 | - public function routeNotificationForFCM() |
|
94 | - { |
|
95 | - $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
96 | - $tokens = []; |
|
97 | - |
|
98 | - foreach ($devices as $device) { |
|
99 | - if (\Core::users()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
100 | - $device->forceDelete(); |
|
101 | - continue; |
|
102 | - } |
|
103 | - |
|
104 | - $tokens[] = $device->device_token; |
|
105 | - } |
|
106 | - |
|
107 | - return $tokens; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * The channels the user receives notification broadcasts on. |
|
112 | - * |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function receivesBroadcastNotificationsOn() |
|
116 | - { |
|
117 | - return 'users.'.$this->id; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Custom password validation. |
|
122 | - * |
|
123 | - * @param string $password |
|
124 | - * @return boolean |
|
125 | - */ |
|
126 | - public function validateForPassportPasswordGrant($password) |
|
127 | - { |
|
128 | - if ($password == config('skeleton.social_pass')) { |
|
129 | - return true; |
|
130 | - } |
|
131 | - |
|
132 | - return \Hash::check($password, $this->password); |
|
133 | - } |
|
19 | + public function getCreatedAtAttribute($value) |
|
20 | + { |
|
21 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
22 | + } |
|
23 | + |
|
24 | + public function getUpdatedAtAttribute($value) |
|
25 | + { |
|
26 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
27 | + } |
|
28 | + |
|
29 | + public function getDeletedAtAttribute($value) |
|
30 | + { |
|
31 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Get the profile picture url. |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function getProfilePictureAttribute($value) |
|
39 | + { |
|
40 | + return url(\Storage::url($value)); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Encrypt the password attribute before |
|
45 | + * saving it in the storage. |
|
46 | + * |
|
47 | + * @param string $value |
|
48 | + */ |
|
49 | + public function setPasswordAttribute($value) |
|
50 | + { |
|
51 | + $this->attributes['password'] = bcrypt($value); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Get the entity's notifications. |
|
56 | + */ |
|
57 | + public function notifications() |
|
58 | + { |
|
59 | + return $this->morphMany('\App\Modules\Notifications\Notification', 'notifiable')->orderBy('created_at', 'desc'); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Get the entity's read notifications. |
|
64 | + */ |
|
65 | + public function readNotifications() |
|
66 | + { |
|
67 | + return $this->notifications()->whereNotNull('read_at'); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Get the entity's unread notifications. |
|
72 | + */ |
|
73 | + public function unreadNotifications() |
|
74 | + { |
|
75 | + return $this->notifications()->whereNull('read_at'); |
|
76 | + } |
|
77 | + |
|
78 | + public function groups() |
|
79 | + { |
|
80 | + return $this->belongsToMany('\App\Modules\Acl\AclGroup', 'users_groups', 'user_id', 'group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
81 | + } |
|
82 | + |
|
83 | + public function oauthClients() |
|
84 | + { |
|
85 | + return $this->hasMany('App\Modules\Acl\OauthClient', 'user_id'); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Return fcm device tokens that will be used in sending fcm notifications. |
|
90 | + * |
|
91 | + * @return array |
|
92 | + */ |
|
93 | + public function routeNotificationForFCM() |
|
94 | + { |
|
95 | + $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
96 | + $tokens = []; |
|
97 | + |
|
98 | + foreach ($devices as $device) { |
|
99 | + if (\Core::users()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
100 | + $device->forceDelete(); |
|
101 | + continue; |
|
102 | + } |
|
103 | + |
|
104 | + $tokens[] = $device->device_token; |
|
105 | + } |
|
106 | + |
|
107 | + return $tokens; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * The channels the user receives notification broadcasts on. |
|
112 | + * |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function receivesBroadcastNotificationsOn() |
|
116 | + { |
|
117 | + return 'users.'.$this->id; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Custom password validation. |
|
122 | + * |
|
123 | + * @param string $password |
|
124 | + * @return boolean |
|
125 | + */ |
|
126 | + public function validateForPassportPasswordGrant($password) |
|
127 | + { |
|
128 | + if ($password == config('skeleton.social_pass')) { |
|
129 | + return true; |
|
130 | + } |
|
131 | + |
|
132 | + return \Hash::check($password, $this->password); |
|
133 | + } |
|
134 | 134 | |
135 | - public static function boot() |
|
136 | - { |
|
137 | - parent::boot(); |
|
138 | - AclUser::observe(\App::make('App\Modules\Acl\ModelObservers\AclUserObserver')); |
|
139 | - } |
|
135 | + public static function boot() |
|
136 | + { |
|
137 | + parent::boot(); |
|
138 | + AclUser::observe(\App::make('App\Modules\Acl\ModelObservers\AclUserObserver')); |
|
139 | + } |
|
140 | 140 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @param array $relations |
36 | 36 | * @param string $sortBy |
37 | - * @param boolean $desc |
|
37 | + * @param integer $desc |
|
38 | 38 | * @param array $columns |
39 | 39 | * @return collection |
40 | 40 | */ |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * @param integer $perPage |
53 | 53 | * @param array $relations |
54 | 54 | * @param string $sortBy |
55 | - * @param boolean $desc |
|
55 | + * @param integer $desc |
|
56 | 56 | * @param array $columns |
57 | 57 | * @return collection |
58 | 58 | */ |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | * @param integer $perPage |
155 | 155 | * @param array $relations |
156 | 156 | * @param string $sortBy |
157 | - * @param boolean $desc |
|
157 | + * @param integer $desc |
|
158 | 158 | * @param array $columns |
159 | 159 | * @return collection |
160 | 160 | */ |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | * @param integer $perPage |
173 | 173 | * @param array $relations |
174 | 174 | * @param string $sortBy |
175 | - * @param boolean $desc |
|
175 | + * @param integer $desc |
|
176 | 176 | * @param array $columns |
177 | 177 | * @return collection |
178 | 178 | */ |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | * Save the given model to the storage. |
189 | 189 | * |
190 | 190 | * @param array $data |
191 | - * @return mixed |
|
191 | + * @return boolean |
|
192 | 192 | */ |
193 | 193 | public function save(array $data) |
194 | 194 | { |
@@ -477,7 +477,7 @@ discard block |
||
477 | 477 | * @param array $conditions array of conditions |
478 | 478 | * @param array $relations |
479 | 479 | * @param string $sortBy |
480 | - * @param boolean $desc |
|
480 | + * @param integer $desc |
|
481 | 481 | * @param array $columns |
482 | 482 | * @return collection |
483 | 483 | */ |
@@ -509,7 +509,7 @@ discard block |
||
509 | 509 | * @param array $conditions array of conditions |
510 | 510 | * @param integer $perPage |
511 | 511 | * @param string $sortBy |
512 | - * @param boolean $desc |
|
512 | + * @param integer $desc |
|
513 | 513 | * @param array $columns |
514 | 514 | * @return collection |
515 | 515 | */ |
@@ -6,634 +6,634 @@ |
||
6 | 6 | |
7 | 7 | abstract class AbstractRepository implements RepositoryInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * The model implementation. |
|
11 | - * |
|
12 | - * @var object |
|
13 | - */ |
|
14 | - public $model; |
|
9 | + /** |
|
10 | + * The model implementation. |
|
11 | + * |
|
12 | + * @var object |
|
13 | + */ |
|
14 | + public $model; |
|
15 | 15 | |
16 | - /** |
|
17 | - * The config implementation. |
|
18 | - * |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected $config; |
|
16 | + /** |
|
17 | + * The config implementation. |
|
18 | + * |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected $config; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Create new AbstractRepository instance. |
|
25 | - */ |
|
26 | - public function __construct() |
|
27 | - { |
|
28 | - $this->config = \CoreConfig::getConfig(); |
|
29 | - $this->model = \App::make($this->getModel()); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Fetch all records with relations from the storage. |
|
34 | - * |
|
35 | - * @param array $relations |
|
36 | - * @param string $sortBy |
|
37 | - * @param boolean $desc |
|
38 | - * @param array $columns |
|
39 | - * @return collection |
|
40 | - */ |
|
41 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
42 | - { |
|
43 | - $sort = $desc ? 'desc' : 'asc'; |
|
44 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Fetch all records with relations from storage in pages |
|
49 | - * that matche the given query. |
|
50 | - * |
|
51 | - * @param string $query |
|
52 | - * @param integer $perPage |
|
53 | - * @param array $relations |
|
54 | - * @param string $sortBy |
|
55 | - * @param boolean $desc |
|
56 | - * @param array $columns |
|
57 | - * @return collection |
|
58 | - */ |
|
59 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
60 | - { |
|
61 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
62 | - $conditionColumns = $this->model->searchable; |
|
63 | - $sort = $desc ? 'desc' : 'asc'; |
|
64 | - |
|
65 | - /** |
|
66 | - * Construct the select conditions for the model. |
|
67 | - */ |
|
68 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
69 | - |
|
70 | - if (count($conditionColumns)) { |
|
71 | - $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
72 | - if (Str::contains($column, '->')) { |
|
73 | - $column = $this->wrapJsonSelector($column); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Use the first element in the model columns to construct the first condition. |
|
78 | - */ |
|
79 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Loop through the rest of the columns to construct or where conditions. |
|
84 | - */ |
|
85 | - foreach ($conditionColumns as $column) { |
|
86 | - $column = 'LOWER('.$column.')'; |
|
87 | - if (Str::contains($column, '->')) { |
|
88 | - $column = $this->wrapJsonSelector($column); |
|
89 | - } |
|
90 | - |
|
91 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Loop through the model relations. |
|
96 | - */ |
|
97 | - foreach ($relations as $relation) { |
|
98 | - /** |
|
99 | - * Remove the sub relation if exists. |
|
100 | - */ |
|
101 | - $relation = explode('.', $relation)[0]; |
|
102 | - |
|
103 | - /** |
|
104 | - * Try to fetch the relation repository from the core. |
|
105 | - */ |
|
106 | - if (\Core::$relation()) { |
|
107 | - /** |
|
108 | - * Construct the relation condition. |
|
109 | - */ |
|
110 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
111 | - |
|
112 | - $subModel->where(function ($q) use ($query, $relation) { |
|
113 | - |
|
114 | - /** |
|
115 | - * Get columns of the relation. |
|
116 | - */ |
|
117 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
118 | - |
|
119 | - if (count($subConditionColumns)) { |
|
120 | - $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
121 | - if (Str::contains($column, '->')) { |
|
122 | - $column = $this->wrapJsonSelector($column); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Use the first element in the relation model columns to construct the first condition. |
|
127 | - */ |
|
128 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Loop through the rest of the columns to construct or where conditions. |
|
133 | - */ |
|
134 | - foreach ($subConditionColumns as $subConditionColumn) { |
|
135 | - $column = 'LOWER('.$subConditionColumn.')'; |
|
136 | - if (Str::contains($column, '->')) { |
|
137 | - $column = $this->wrapJsonSelector($column); |
|
138 | - } |
|
23 | + /** |
|
24 | + * Create new AbstractRepository instance. |
|
25 | + */ |
|
26 | + public function __construct() |
|
27 | + { |
|
28 | + $this->config = \CoreConfig::getConfig(); |
|
29 | + $this->model = \App::make($this->getModel()); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Fetch all records with relations from the storage. |
|
34 | + * |
|
35 | + * @param array $relations |
|
36 | + * @param string $sortBy |
|
37 | + * @param boolean $desc |
|
38 | + * @param array $columns |
|
39 | + * @return collection |
|
40 | + */ |
|
41 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
42 | + { |
|
43 | + $sort = $desc ? 'desc' : 'asc'; |
|
44 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Fetch all records with relations from storage in pages |
|
49 | + * that matche the given query. |
|
50 | + * |
|
51 | + * @param string $query |
|
52 | + * @param integer $perPage |
|
53 | + * @param array $relations |
|
54 | + * @param string $sortBy |
|
55 | + * @param boolean $desc |
|
56 | + * @param array $columns |
|
57 | + * @return collection |
|
58 | + */ |
|
59 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
60 | + { |
|
61 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
62 | + $conditionColumns = $this->model->searchable; |
|
63 | + $sort = $desc ? 'desc' : 'asc'; |
|
64 | + |
|
65 | + /** |
|
66 | + * Construct the select conditions for the model. |
|
67 | + */ |
|
68 | + $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
69 | + |
|
70 | + if (count($conditionColumns)) { |
|
71 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
72 | + if (Str::contains($column, '->')) { |
|
73 | + $column = $this->wrapJsonSelector($column); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Use the first element in the model columns to construct the first condition. |
|
78 | + */ |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Loop through the rest of the columns to construct or where conditions. |
|
84 | + */ |
|
85 | + foreach ($conditionColumns as $column) { |
|
86 | + $column = 'LOWER('.$column.')'; |
|
87 | + if (Str::contains($column, '->')) { |
|
88 | + $column = $this->wrapJsonSelector($column); |
|
89 | + } |
|
90 | + |
|
91 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Loop through the model relations. |
|
96 | + */ |
|
97 | + foreach ($relations as $relation) { |
|
98 | + /** |
|
99 | + * Remove the sub relation if exists. |
|
100 | + */ |
|
101 | + $relation = explode('.', $relation)[0]; |
|
102 | + |
|
103 | + /** |
|
104 | + * Try to fetch the relation repository from the core. |
|
105 | + */ |
|
106 | + if (\Core::$relation()) { |
|
107 | + /** |
|
108 | + * Construct the relation condition. |
|
109 | + */ |
|
110 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
111 | + |
|
112 | + $subModel->where(function ($q) use ($query, $relation) { |
|
113 | + |
|
114 | + /** |
|
115 | + * Get columns of the relation. |
|
116 | + */ |
|
117 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
118 | + |
|
119 | + if (count($subConditionColumns)) { |
|
120 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
121 | + if (Str::contains($column, '->')) { |
|
122 | + $column = $this->wrapJsonSelector($column); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Use the first element in the relation model columns to construct the first condition. |
|
127 | + */ |
|
128 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Loop through the rest of the columns to construct or where conditions. |
|
133 | + */ |
|
134 | + foreach ($subConditionColumns as $subConditionColumn) { |
|
135 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
136 | + if (Str::contains($column, '->')) { |
|
137 | + $column = $this->wrapJsonSelector($column); |
|
138 | + } |
|
139 | 139 | |
140 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
141 | - } |
|
142 | - }); |
|
143 | - }); |
|
144 | - } |
|
145 | - } |
|
146 | - }); |
|
140 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
141 | + } |
|
142 | + }); |
|
143 | + }); |
|
144 | + } |
|
145 | + } |
|
146 | + }); |
|
147 | 147 | |
148 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
149 | - } |
|
148 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Fetch all records with relations from storage in pages. |
|
153 | - * |
|
154 | - * @param integer $perPage |
|
155 | - * @param array $relations |
|
156 | - * @param string $sortBy |
|
157 | - * @param boolean $desc |
|
158 | - * @param array $columns |
|
159 | - * @return collection |
|
160 | - */ |
|
161 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
162 | - { |
|
163 | - $sort = $desc ? 'desc' : 'asc'; |
|
164 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Fetch all records with relations based on |
|
169 | - * the given condition from storage in pages. |
|
170 | - * |
|
171 | - * @param array $conditions array of conditions |
|
172 | - * @param integer $perPage |
|
173 | - * @param array $relations |
|
174 | - * @param string $sortBy |
|
175 | - * @param boolean $desc |
|
176 | - * @param array $columns |
|
177 | - * @return collection |
|
178 | - */ |
|
179 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
180 | - { |
|
181 | - unset($conditions['page']); |
|
182 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
183 | - $sort = $desc ? 'desc' : 'asc'; |
|
184 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
185 | - } |
|
151 | + /** |
|
152 | + * Fetch all records with relations from storage in pages. |
|
153 | + * |
|
154 | + * @param integer $perPage |
|
155 | + * @param array $relations |
|
156 | + * @param string $sortBy |
|
157 | + * @param boolean $desc |
|
158 | + * @param array $columns |
|
159 | + * @return collection |
|
160 | + */ |
|
161 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
162 | + { |
|
163 | + $sort = $desc ? 'desc' : 'asc'; |
|
164 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Fetch all records with relations based on |
|
169 | + * the given condition from storage in pages. |
|
170 | + * |
|
171 | + * @param array $conditions array of conditions |
|
172 | + * @param integer $perPage |
|
173 | + * @param array $relations |
|
174 | + * @param string $sortBy |
|
175 | + * @param boolean $desc |
|
176 | + * @param array $columns |
|
177 | + * @return collection |
|
178 | + */ |
|
179 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
180 | + { |
|
181 | + unset($conditions['page']); |
|
182 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
183 | + $sort = $desc ? 'desc' : 'asc'; |
|
184 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Save the given model to the storage. |
|
189 | - * |
|
190 | - * @param array $data |
|
191 | - * @return mixed |
|
192 | - */ |
|
193 | - public function save(array $data) |
|
194 | - { |
|
195 | - \Session::put('locale', 'all'); |
|
196 | - $model = false; |
|
197 | - $modelClass = $this->model; |
|
198 | - $relations = []; |
|
199 | - |
|
200 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
201 | - /** |
|
202 | - * If the id is present in the data then select the model for updating, |
|
203 | - * else create new model. |
|
204 | - * @var array |
|
205 | - */ |
|
206 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
207 | - if (! $model) { |
|
208 | - \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Construct the model object with the given data, |
|
213 | - * and if there is a relation add it to relations array, |
|
214 | - * then save the model. |
|
215 | - */ |
|
216 | - foreach ($data as $key => $value) { |
|
217 | - /** |
|
218 | - * If the attribute is a relation. |
|
219 | - */ |
|
220 | - $relation = camel_case($key); |
|
221 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
222 | - /** |
|
223 | - * Check if the relation is a collection. |
|
224 | - */ |
|
225 | - if (class_basename($model->$relation) == 'Collection') { |
|
226 | - /** |
|
227 | - * If the relation has no value then marke the relation data |
|
228 | - * related to the model to be deleted. |
|
229 | - */ |
|
230 | - if (! $value || ! count($value)) { |
|
231 | - $relations[$relation] = 'delete'; |
|
232 | - } |
|
233 | - } |
|
234 | - if (is_array($value)) { |
|
235 | - /** |
|
236 | - * Loop through the relation data. |
|
237 | - */ |
|
238 | - foreach ($value as $attr => $val) { |
|
239 | - /** |
|
240 | - * Get the relation model. |
|
241 | - */ |
|
242 | - $relationBaseModel = \Core::$relation()->model; |
|
243 | - |
|
244 | - /** |
|
245 | - * Check if the relation is a collection. |
|
246 | - */ |
|
247 | - if (class_basename($model->$relation) == 'Collection') { |
|
248 | - /** |
|
249 | - * If the id is present in the data then select the relation model for updating, |
|
250 | - * else create new model. |
|
251 | - */ |
|
252 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
253 | - |
|
254 | - /** |
|
255 | - * If model doesn't exists. |
|
256 | - */ |
|
257 | - if (! $relationModel) { |
|
258 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Loop through the relation attributes. |
|
263 | - */ |
|
264 | - foreach ($val as $attr => $val) { |
|
265 | - /** |
|
266 | - * Prevent the sub relations or attributes not in the fillable. |
|
267 | - */ |
|
268 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
269 | - $relationModel->$attr = $val; |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - $relations[$relation][] = $relationModel; |
|
274 | - } else { |
|
275 | - /** |
|
276 | - * Prevent the sub relations. |
|
277 | - */ |
|
278 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
279 | - /** |
|
280 | - * If the id is present in the data then select the relation model for updating, |
|
281 | - * else create new model. |
|
282 | - */ |
|
283 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
284 | - |
|
285 | - /** |
|
286 | - * If model doesn't exists. |
|
287 | - */ |
|
288 | - if (! $relationModel) { |
|
289 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
290 | - } |
|
291 | - |
|
292 | - foreach ($value as $relationAttribute => $relationValue) { |
|
293 | - /** |
|
294 | - * Prevent attributes not in the fillable. |
|
295 | - */ |
|
296 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
297 | - $relationModel->$relationAttribute = $relationValue; |
|
298 | - } |
|
299 | - } |
|
300 | - |
|
301 | - $relations[$relation] = $relationModel; |
|
302 | - } |
|
303 | - } |
|
304 | - } |
|
305 | - } |
|
306 | - } elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
307 | - /** |
|
308 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
309 | - */ |
|
310 | - $model->$key = $value; |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Loop through the relations array. |
|
316 | - */ |
|
317 | - foreach ($relations as $key => $value) { |
|
318 | - /** |
|
319 | - * If the relation is marked for delete then delete it. |
|
320 | - */ |
|
321 | - if ($value == 'delete' && $model->$key()->count()) { |
|
322 | - $model->$key()->delete(); |
|
323 | - } elseif (gettype($value) == 'array') { |
|
324 | - /** |
|
325 | - * Save the model. |
|
326 | - */ |
|
327 | - $model->save(); |
|
328 | - $ids = []; |
|
329 | - |
|
330 | - /** |
|
331 | - * Loop through the relations. |
|
332 | - */ |
|
333 | - foreach ($value as $val) { |
|
334 | - switch (class_basename($model->$key())) { |
|
335 | - /** |
|
336 | - * If the relation is one to many then update it's foreign key with |
|
337 | - * the model id and save it then add its id to ids array to delete all |
|
338 | - * relations who's id isn't in the ids array. |
|
339 | - */ |
|
340 | - case 'HasMany': |
|
341 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
342 | - $val->$foreignKeyName = $model->id; |
|
343 | - $val->save(); |
|
344 | - $ids[] = $val->id; |
|
345 | - break; |
|
346 | - |
|
347 | - /** |
|
348 | - * If the relation is many to many then add it's id to the ids array to |
|
349 | - * attache these ids to the model. |
|
350 | - */ |
|
351 | - case 'BelongsToMany': |
|
352 | - $val->save(); |
|
353 | - $ids[] = $val->id; |
|
354 | - break; |
|
355 | - } |
|
356 | - } |
|
357 | - switch (class_basename($model->$key())) { |
|
358 | - /** |
|
359 | - * If the relation is one to many then delete all |
|
360 | - * relations who's id isn't in the ids array. |
|
361 | - */ |
|
362 | - case 'HasMany': |
|
363 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
364 | - break; |
|
365 | - |
|
366 | - /** |
|
367 | - * If the relation is many to many then |
|
368 | - * detach the previous data and attach |
|
369 | - * the ids array to the model. |
|
370 | - */ |
|
371 | - case 'BelongsToMany': |
|
372 | - $model->$key()->detach(); |
|
373 | - $model->$key()->attach($ids); |
|
374 | - break; |
|
375 | - } |
|
376 | - } else { |
|
377 | - switch (class_basename($model->$key())) { |
|
378 | - /** |
|
379 | - * If the relation is one to one. |
|
380 | - */ |
|
381 | - case 'HasOne': |
|
382 | - /** |
|
383 | - * Save the model. |
|
384 | - */ |
|
385 | - $model->save(); |
|
386 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
387 | - $value->$foreignKeyName = $model->id; |
|
388 | - $value->save(); |
|
389 | - break; |
|
390 | - case 'BelongsTo': |
|
391 | - /** |
|
392 | - * Save the model. |
|
393 | - */ |
|
394 | - $value->save(); |
|
395 | - $model->$key()->associate($value); |
|
396 | - break; |
|
397 | - } |
|
398 | - } |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Save the model. |
|
403 | - */ |
|
404 | - $model->save(); |
|
405 | - }); |
|
187 | + /** |
|
188 | + * Save the given model to the storage. |
|
189 | + * |
|
190 | + * @param array $data |
|
191 | + * @return mixed |
|
192 | + */ |
|
193 | + public function save(array $data) |
|
194 | + { |
|
195 | + \Session::put('locale', 'all'); |
|
196 | + $model = false; |
|
197 | + $modelClass = $this->model; |
|
198 | + $relations = []; |
|
199 | + |
|
200 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
201 | + /** |
|
202 | + * If the id is present in the data then select the model for updating, |
|
203 | + * else create new model. |
|
204 | + * @var array |
|
205 | + */ |
|
206 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
207 | + if (! $model) { |
|
208 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Construct the model object with the given data, |
|
213 | + * and if there is a relation add it to relations array, |
|
214 | + * then save the model. |
|
215 | + */ |
|
216 | + foreach ($data as $key => $value) { |
|
217 | + /** |
|
218 | + * If the attribute is a relation. |
|
219 | + */ |
|
220 | + $relation = camel_case($key); |
|
221 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
222 | + /** |
|
223 | + * Check if the relation is a collection. |
|
224 | + */ |
|
225 | + if (class_basename($model->$relation) == 'Collection') { |
|
226 | + /** |
|
227 | + * If the relation has no value then marke the relation data |
|
228 | + * related to the model to be deleted. |
|
229 | + */ |
|
230 | + if (! $value || ! count($value)) { |
|
231 | + $relations[$relation] = 'delete'; |
|
232 | + } |
|
233 | + } |
|
234 | + if (is_array($value)) { |
|
235 | + /** |
|
236 | + * Loop through the relation data. |
|
237 | + */ |
|
238 | + foreach ($value as $attr => $val) { |
|
239 | + /** |
|
240 | + * Get the relation model. |
|
241 | + */ |
|
242 | + $relationBaseModel = \Core::$relation()->model; |
|
243 | + |
|
244 | + /** |
|
245 | + * Check if the relation is a collection. |
|
246 | + */ |
|
247 | + if (class_basename($model->$relation) == 'Collection') { |
|
248 | + /** |
|
249 | + * If the id is present in the data then select the relation model for updating, |
|
250 | + * else create new model. |
|
251 | + */ |
|
252 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
253 | + |
|
254 | + /** |
|
255 | + * If model doesn't exists. |
|
256 | + */ |
|
257 | + if (! $relationModel) { |
|
258 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Loop through the relation attributes. |
|
263 | + */ |
|
264 | + foreach ($val as $attr => $val) { |
|
265 | + /** |
|
266 | + * Prevent the sub relations or attributes not in the fillable. |
|
267 | + */ |
|
268 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
269 | + $relationModel->$attr = $val; |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + $relations[$relation][] = $relationModel; |
|
274 | + } else { |
|
275 | + /** |
|
276 | + * Prevent the sub relations. |
|
277 | + */ |
|
278 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
279 | + /** |
|
280 | + * If the id is present in the data then select the relation model for updating, |
|
281 | + * else create new model. |
|
282 | + */ |
|
283 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
284 | + |
|
285 | + /** |
|
286 | + * If model doesn't exists. |
|
287 | + */ |
|
288 | + if (! $relationModel) { |
|
289 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
290 | + } |
|
291 | + |
|
292 | + foreach ($value as $relationAttribute => $relationValue) { |
|
293 | + /** |
|
294 | + * Prevent attributes not in the fillable. |
|
295 | + */ |
|
296 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
297 | + $relationModel->$relationAttribute = $relationValue; |
|
298 | + } |
|
299 | + } |
|
300 | + |
|
301 | + $relations[$relation] = $relationModel; |
|
302 | + } |
|
303 | + } |
|
304 | + } |
|
305 | + } |
|
306 | + } elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
307 | + /** |
|
308 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
309 | + */ |
|
310 | + $model->$key = $value; |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Loop through the relations array. |
|
316 | + */ |
|
317 | + foreach ($relations as $key => $value) { |
|
318 | + /** |
|
319 | + * If the relation is marked for delete then delete it. |
|
320 | + */ |
|
321 | + if ($value == 'delete' && $model->$key()->count()) { |
|
322 | + $model->$key()->delete(); |
|
323 | + } elseif (gettype($value) == 'array') { |
|
324 | + /** |
|
325 | + * Save the model. |
|
326 | + */ |
|
327 | + $model->save(); |
|
328 | + $ids = []; |
|
329 | + |
|
330 | + /** |
|
331 | + * Loop through the relations. |
|
332 | + */ |
|
333 | + foreach ($value as $val) { |
|
334 | + switch (class_basename($model->$key())) { |
|
335 | + /** |
|
336 | + * If the relation is one to many then update it's foreign key with |
|
337 | + * the model id and save it then add its id to ids array to delete all |
|
338 | + * relations who's id isn't in the ids array. |
|
339 | + */ |
|
340 | + case 'HasMany': |
|
341 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
342 | + $val->$foreignKeyName = $model->id; |
|
343 | + $val->save(); |
|
344 | + $ids[] = $val->id; |
|
345 | + break; |
|
346 | + |
|
347 | + /** |
|
348 | + * If the relation is many to many then add it's id to the ids array to |
|
349 | + * attache these ids to the model. |
|
350 | + */ |
|
351 | + case 'BelongsToMany': |
|
352 | + $val->save(); |
|
353 | + $ids[] = $val->id; |
|
354 | + break; |
|
355 | + } |
|
356 | + } |
|
357 | + switch (class_basename($model->$key())) { |
|
358 | + /** |
|
359 | + * If the relation is one to many then delete all |
|
360 | + * relations who's id isn't in the ids array. |
|
361 | + */ |
|
362 | + case 'HasMany': |
|
363 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
364 | + break; |
|
365 | + |
|
366 | + /** |
|
367 | + * If the relation is many to many then |
|
368 | + * detach the previous data and attach |
|
369 | + * the ids array to the model. |
|
370 | + */ |
|
371 | + case 'BelongsToMany': |
|
372 | + $model->$key()->detach(); |
|
373 | + $model->$key()->attach($ids); |
|
374 | + break; |
|
375 | + } |
|
376 | + } else { |
|
377 | + switch (class_basename($model->$key())) { |
|
378 | + /** |
|
379 | + * If the relation is one to one. |
|
380 | + */ |
|
381 | + case 'HasOne': |
|
382 | + /** |
|
383 | + * Save the model. |
|
384 | + */ |
|
385 | + $model->save(); |
|
386 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
387 | + $value->$foreignKeyName = $model->id; |
|
388 | + $value->save(); |
|
389 | + break; |
|
390 | + case 'BelongsTo': |
|
391 | + /** |
|
392 | + * Save the model. |
|
393 | + */ |
|
394 | + $value->save(); |
|
395 | + $model->$key()->associate($value); |
|
396 | + break; |
|
397 | + } |
|
398 | + } |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Save the model. |
|
403 | + */ |
|
404 | + $model->save(); |
|
405 | + }); |
|
406 | 406 | |
407 | - return $model; |
|
408 | - } |
|
407 | + return $model; |
|
408 | + } |
|
409 | 409 | |
410 | - /** |
|
411 | - * Update record in the storage based on the given |
|
412 | - * condition. |
|
413 | - * |
|
414 | - * @param var $value condition value |
|
415 | - * @param array $data |
|
416 | - * @param string $attribute condition column name |
|
417 | - * @return void |
|
418 | - */ |
|
419 | - public function update($value, array $data, $attribute = 'id') |
|
420 | - { |
|
421 | - if ($attribute == 'id') { |
|
422 | - $model = $this->model->lockForUpdate()->find($value); |
|
423 | - $model ? $model->update($data) : 0; |
|
424 | - } else { |
|
425 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
426 | - $model->update($data); |
|
427 | - }); |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Delete record from the storage based on the given |
|
433 | - * condition. |
|
434 | - * |
|
435 | - * @param var $value condition value |
|
436 | - * @param string $attribute condition column name |
|
437 | - * @return void |
|
438 | - */ |
|
439 | - public function delete($value, $attribute = 'id') |
|
440 | - { |
|
441 | - if ($attribute == 'id') { |
|
442 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
443 | - $model = $this->model->lockForUpdate()->find($value); |
|
444 | - if (! $model) { |
|
445 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
446 | - } |
|
410 | + /** |
|
411 | + * Update record in the storage based on the given |
|
412 | + * condition. |
|
413 | + * |
|
414 | + * @param var $value condition value |
|
415 | + * @param array $data |
|
416 | + * @param string $attribute condition column name |
|
417 | + * @return void |
|
418 | + */ |
|
419 | + public function update($value, array $data, $attribute = 'id') |
|
420 | + { |
|
421 | + if ($attribute == 'id') { |
|
422 | + $model = $this->model->lockForUpdate()->find($value); |
|
423 | + $model ? $model->update($data) : 0; |
|
424 | + } else { |
|
425 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
426 | + $model->update($data); |
|
427 | + }); |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Delete record from the storage based on the given |
|
433 | + * condition. |
|
434 | + * |
|
435 | + * @param var $value condition value |
|
436 | + * @param string $attribute condition column name |
|
437 | + * @return void |
|
438 | + */ |
|
439 | + public function delete($value, $attribute = 'id') |
|
440 | + { |
|
441 | + if ($attribute == 'id') { |
|
442 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
443 | + $model = $this->model->lockForUpdate()->find($value); |
|
444 | + if (! $model) { |
|
445 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
446 | + } |
|
447 | 447 | |
448 | - $model->delete(); |
|
449 | - }); |
|
450 | - } else { |
|
451 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
452 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) { |
|
453 | - $model->delete(); |
|
454 | - }); |
|
455 | - }); |
|
456 | - } |
|
457 | - } |
|
448 | + $model->delete(); |
|
449 | + }); |
|
450 | + } else { |
|
451 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
452 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) { |
|
453 | + $model->delete(); |
|
454 | + }); |
|
455 | + }); |
|
456 | + } |
|
457 | + } |
|
458 | 458 | |
459 | - /** |
|
460 | - * Fetch records from the storage based on the given |
|
461 | - * id. |
|
462 | - * |
|
463 | - * @param integer $id |
|
464 | - * @param string[] $relations |
|
465 | - * @param array $columns |
|
466 | - * @return object |
|
467 | - */ |
|
468 | - public function find($id, $relations = [], $columns = ['*']) |
|
469 | - { |
|
470 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
471 | - } |
|
459 | + /** |
|
460 | + * Fetch records from the storage based on the given |
|
461 | + * id. |
|
462 | + * |
|
463 | + * @param integer $id |
|
464 | + * @param string[] $relations |
|
465 | + * @param array $columns |
|
466 | + * @return object |
|
467 | + */ |
|
468 | + public function find($id, $relations = [], $columns = ['*']) |
|
469 | + { |
|
470 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
471 | + } |
|
472 | 472 | |
473 | - /** |
|
474 | - * Fetch records from the storage based on the given |
|
475 | - * condition. |
|
476 | - * |
|
477 | - * @param array $conditions array of conditions |
|
478 | - * @param array $relations |
|
479 | - * @param string $sortBy |
|
480 | - * @param boolean $desc |
|
481 | - * @param array $columns |
|
482 | - * @return collection |
|
483 | - */ |
|
484 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
485 | - { |
|
486 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
487 | - $sort = $desc ? 'desc' : 'asc'; |
|
488 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Fetch the first record from the storage based on the given |
|
493 | - * condition. |
|
494 | - * |
|
495 | - * @param array $conditions array of conditions |
|
496 | - * @param array $relations |
|
497 | - * @param array $columns |
|
498 | - * @return object |
|
499 | - */ |
|
500 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
501 | - { |
|
502 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
503 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Return the deleted models in pages based on the given conditions. |
|
508 | - * |
|
509 | - * @param array $conditions array of conditions |
|
510 | - * @param integer $perPage |
|
511 | - * @param string $sortBy |
|
512 | - * @param boolean $desc |
|
513 | - * @param array $columns |
|
514 | - * @return collection |
|
515 | - */ |
|
516 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
517 | - { |
|
518 | - unset($conditions['page']); |
|
519 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
520 | - $sort = $desc ? 'desc' : 'asc'; |
|
521 | - $model = $this->model->onlyTrashed(); |
|
522 | - |
|
523 | - if (count($conditions['conditionValues'])) { |
|
524 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
525 | - } |
|
526 | - |
|
527 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
528 | - ; |
|
529 | - } |
|
530 | - |
|
531 | - /** |
|
532 | - * Restore the deleted model. |
|
533 | - * |
|
534 | - * @param integer $id |
|
535 | - * @return void |
|
536 | - */ |
|
537 | - public function restore($id) |
|
538 | - { |
|
539 | - $model = $this->model->onlyTrashed()->find($id); |
|
540 | - |
|
541 | - if (! $model) { |
|
542 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
543 | - } |
|
544 | - |
|
545 | - $model->restore(); |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Build the conditions recursively for the retrieving methods. |
|
550 | - * @param array $conditions |
|
551 | - * @return array |
|
552 | - */ |
|
553 | - protected function constructConditions($conditions, $model) |
|
554 | - { |
|
555 | - $conditionString = ''; |
|
556 | - $conditionValues = []; |
|
557 | - foreach ($conditions as $key => $value) { |
|
558 | - if (Str::contains($key, '->')) { |
|
559 | - $key = $this->wrapJsonSelector($key); |
|
560 | - } |
|
561 | - |
|
562 | - if ($key == 'and') { |
|
563 | - $conditions = $this->constructConditions($value, $model); |
|
564 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
565 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
566 | - } elseif ($key == 'or') { |
|
567 | - $conditions = $this->constructConditions($value, $model); |
|
568 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
569 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
570 | - } else { |
|
571 | - if (is_array($value)) { |
|
572 | - $operator = $value['op']; |
|
573 | - if (strtolower($operator) == 'between') { |
|
574 | - $value1 = $value['val1']; |
|
575 | - $value2 = $value['val2']; |
|
576 | - } else { |
|
577 | - $value = Arr::get($value, 'val', ''); |
|
578 | - } |
|
579 | - } else { |
|
580 | - $operator = '='; |
|
581 | - } |
|
473 | + /** |
|
474 | + * Fetch records from the storage based on the given |
|
475 | + * condition. |
|
476 | + * |
|
477 | + * @param array $conditions array of conditions |
|
478 | + * @param array $relations |
|
479 | + * @param string $sortBy |
|
480 | + * @param boolean $desc |
|
481 | + * @param array $columns |
|
482 | + * @return collection |
|
483 | + */ |
|
484 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
485 | + { |
|
486 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
487 | + $sort = $desc ? 'desc' : 'asc'; |
|
488 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Fetch the first record from the storage based on the given |
|
493 | + * condition. |
|
494 | + * |
|
495 | + * @param array $conditions array of conditions |
|
496 | + * @param array $relations |
|
497 | + * @param array $columns |
|
498 | + * @return object |
|
499 | + */ |
|
500 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
501 | + { |
|
502 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
503 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Return the deleted models in pages based on the given conditions. |
|
508 | + * |
|
509 | + * @param array $conditions array of conditions |
|
510 | + * @param integer $perPage |
|
511 | + * @param string $sortBy |
|
512 | + * @param boolean $desc |
|
513 | + * @param array $columns |
|
514 | + * @return collection |
|
515 | + */ |
|
516 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
517 | + { |
|
518 | + unset($conditions['page']); |
|
519 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
520 | + $sort = $desc ? 'desc' : 'asc'; |
|
521 | + $model = $this->model->onlyTrashed(); |
|
522 | + |
|
523 | + if (count($conditions['conditionValues'])) { |
|
524 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
525 | + } |
|
526 | + |
|
527 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
528 | + ; |
|
529 | + } |
|
530 | + |
|
531 | + /** |
|
532 | + * Restore the deleted model. |
|
533 | + * |
|
534 | + * @param integer $id |
|
535 | + * @return void |
|
536 | + */ |
|
537 | + public function restore($id) |
|
538 | + { |
|
539 | + $model = $this->model->onlyTrashed()->find($id); |
|
540 | + |
|
541 | + if (! $model) { |
|
542 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
543 | + } |
|
544 | + |
|
545 | + $model->restore(); |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Build the conditions recursively for the retrieving methods. |
|
550 | + * @param array $conditions |
|
551 | + * @return array |
|
552 | + */ |
|
553 | + protected function constructConditions($conditions, $model) |
|
554 | + { |
|
555 | + $conditionString = ''; |
|
556 | + $conditionValues = []; |
|
557 | + foreach ($conditions as $key => $value) { |
|
558 | + if (Str::contains($key, '->')) { |
|
559 | + $key = $this->wrapJsonSelector($key); |
|
560 | + } |
|
561 | + |
|
562 | + if ($key == 'and') { |
|
563 | + $conditions = $this->constructConditions($value, $model); |
|
564 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
565 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
566 | + } elseif ($key == 'or') { |
|
567 | + $conditions = $this->constructConditions($value, $model); |
|
568 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
569 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
570 | + } else { |
|
571 | + if (is_array($value)) { |
|
572 | + $operator = $value['op']; |
|
573 | + if (strtolower($operator) == 'between') { |
|
574 | + $value1 = $value['val1']; |
|
575 | + $value2 = $value['val2']; |
|
576 | + } else { |
|
577 | + $value = Arr::get($value, 'val', ''); |
|
578 | + } |
|
579 | + } else { |
|
580 | + $operator = '='; |
|
581 | + } |
|
582 | 582 | |
583 | - if (strtolower($operator) == 'between') { |
|
584 | - $conditionString .= $key.' >= ? and '; |
|
585 | - $conditionValues[] = $value1; |
|
586 | - |
|
587 | - $conditionString .= $key.' <= ? {op} '; |
|
588 | - $conditionValues[] = $value2; |
|
589 | - } elseif (strtolower($operator) == 'in') { |
|
590 | - $conditionValues = array_merge($conditionValues, $value); |
|
591 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
592 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
593 | - } elseif (strtolower($operator) == 'null') { |
|
594 | - $conditionString .= $key.' is null {op} '; |
|
595 | - } elseif (strtolower($operator) == 'not null') { |
|
596 | - $conditionString .= $key.' is not null {op} '; |
|
597 | - } elseif (strtolower($operator) == 'has') { |
|
598 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
599 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
600 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
601 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
602 | - } else { |
|
603 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
604 | - $conditionValues[] = $value; |
|
605 | - } |
|
606 | - } |
|
607 | - } |
|
608 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
609 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
610 | - } |
|
611 | - |
|
612 | - /** |
|
613 | - * Wrap the given JSON selector. |
|
614 | - * |
|
615 | - * @param string $value |
|
616 | - * @return string |
|
617 | - */ |
|
618 | - protected function wrapJsonSelector($value) |
|
619 | - { |
|
620 | - $removeLast = strpos($value, ')'); |
|
621 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
622 | - $path = explode('->', $value); |
|
623 | - $field = array_shift($path); |
|
624 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
625 | - return '"'.$part.'"'; |
|
626 | - })->implode('.')); |
|
583 | + if (strtolower($operator) == 'between') { |
|
584 | + $conditionString .= $key.' >= ? and '; |
|
585 | + $conditionValues[] = $value1; |
|
586 | + |
|
587 | + $conditionString .= $key.' <= ? {op} '; |
|
588 | + $conditionValues[] = $value2; |
|
589 | + } elseif (strtolower($operator) == 'in') { |
|
590 | + $conditionValues = array_merge($conditionValues, $value); |
|
591 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
592 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
593 | + } elseif (strtolower($operator) == 'null') { |
|
594 | + $conditionString .= $key.' is null {op} '; |
|
595 | + } elseif (strtolower($operator) == 'not null') { |
|
596 | + $conditionString .= $key.' is not null {op} '; |
|
597 | + } elseif (strtolower($operator) == 'has') { |
|
598 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
599 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
600 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
601 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
602 | + } else { |
|
603 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
604 | + $conditionValues[] = $value; |
|
605 | + } |
|
606 | + } |
|
607 | + } |
|
608 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
609 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
610 | + } |
|
611 | + |
|
612 | + /** |
|
613 | + * Wrap the given JSON selector. |
|
614 | + * |
|
615 | + * @param string $value |
|
616 | + * @return string |
|
617 | + */ |
|
618 | + protected function wrapJsonSelector($value) |
|
619 | + { |
|
620 | + $removeLast = strpos($value, ')'); |
|
621 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
622 | + $path = explode('->', $value); |
|
623 | + $field = array_shift($path); |
|
624 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
625 | + return '"'.$part.'"'; |
|
626 | + })->implode('.')); |
|
627 | 627 | |
628 | - return $removeLast === false ? $result : $result.')'; |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Abstract method that return the necessary |
|
633 | - * information (full model namespace) |
|
634 | - * needed to preform the previous actions. |
|
635 | - * |
|
636 | - * @return string |
|
637 | - */ |
|
638 | - abstract protected function getModel(); |
|
628 | + return $removeLast === false ? $result : $result.')'; |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Abstract method that return the necessary |
|
633 | + * information (full model namespace) |
|
634 | + * needed to preform the previous actions. |
|
635 | + * |
|
636 | + * @return string |
|
637 | + */ |
|
638 | + abstract protected function getModel(); |
|
639 | 639 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | /** |
66 | 66 | * Construct the select conditions for the model. |
67 | 67 | */ |
68 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
68 | + $model->where(function($q) use ($query, $conditionColumns, $relations) { |
|
69 | 69 | |
70 | 70 | if (count($conditionColumns)) { |
71 | 71 | $column = 'LOWER('.array_shift($conditionColumns).')'; |
@@ -107,9 +107,9 @@ discard block |
||
107 | 107 | /** |
108 | 108 | * Construct the relation condition. |
109 | 109 | */ |
110 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
110 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation) { |
|
111 | 111 | |
112 | - $subModel->where(function ($q) use ($query, $relation) { |
|
112 | + $subModel->where(function($q) use ($query, $relation) { |
|
113 | 113 | |
114 | 114 | /** |
115 | 115 | * Get columns of the relation. |
@@ -197,14 +197,14 @@ discard block |
||
197 | 197 | $modelClass = $this->model; |
198 | 198 | $relations = []; |
199 | 199 | |
200 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
200 | + \DB::transaction(function() use (&$model, &$relations, $data, $modelClass) { |
|
201 | 201 | /** |
202 | 202 | * If the id is present in the data then select the model for updating, |
203 | 203 | * else create new model. |
204 | 204 | * @var array |
205 | 205 | */ |
206 | 206 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
207 | - if (! $model) { |
|
207 | + if ( ! $model) { |
|
208 | 208 | \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
209 | 209 | } |
210 | 210 | |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | * If the relation has no value then marke the relation data |
228 | 228 | * related to the model to be deleted. |
229 | 229 | */ |
230 | - if (! $value || ! count($value)) { |
|
230 | + if ( ! $value || ! count($value)) { |
|
231 | 231 | $relations[$relation] = 'delete'; |
232 | 232 | } |
233 | 233 | } |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | /** |
255 | 255 | * If model doesn't exists. |
256 | 256 | */ |
257 | - if (! $relationModel) { |
|
257 | + if ( ! $relationModel) { |
|
258 | 258 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
259 | 259 | } |
260 | 260 | |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | /** |
286 | 286 | * If model doesn't exists. |
287 | 287 | */ |
288 | - if (! $relationModel) { |
|
288 | + if ( ! $relationModel) { |
|
289 | 289 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
290 | 290 | } |
291 | 291 | |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | $model = $this->model->lockForUpdate()->find($value); |
423 | 423 | $model ? $model->update($data) : 0; |
424 | 424 | } else { |
425 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
425 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data) { |
|
426 | 426 | $model->update($data); |
427 | 427 | }); |
428 | 428 | } |
@@ -439,17 +439,17 @@ discard block |
||
439 | 439 | public function delete($value, $attribute = 'id') |
440 | 440 | { |
441 | 441 | if ($attribute == 'id') { |
442 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
442 | + \DB::transaction(function() use ($value, $attribute, &$result) { |
|
443 | 443 | $model = $this->model->lockForUpdate()->find($value); |
444 | - if (! $model) { |
|
444 | + if ( ! $model) { |
|
445 | 445 | \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
446 | 446 | } |
447 | 447 | |
448 | 448 | $model->delete(); |
449 | 449 | }); |
450 | 450 | } else { |
451 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
452 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) { |
|
451 | + \DB::transaction(function() use ($value, $attribute, &$result) { |
|
452 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { |
|
453 | 453 | $model->delete(); |
454 | 454 | }); |
455 | 455 | }); |
@@ -538,7 +538,7 @@ discard block |
||
538 | 538 | { |
539 | 539 | $model = $this->model->onlyTrashed()->find($id); |
540 | 540 | |
541 | - if (! $model) { |
|
541 | + if ( ! $model) { |
|
542 | 542 | \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
543 | 543 | } |
544 | 544 | |
@@ -621,7 +621,7 @@ discard block |
||
621 | 621 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
622 | 622 | $path = explode('->', $value); |
623 | 623 | $field = array_shift($path); |
624 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
624 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
625 | 625 | return '"'.$part.'"'; |
626 | 626 | })->implode('.')); |
627 | 627 |
@@ -199,7 +199,7 @@ |
||
199 | 199 | /** |
200 | 200 | * Get the given method body code. |
201 | 201 | * |
202 | - * @param object $reflectionMethod |
|
202 | + * @param \ReflectionMethod $reflectionMethod |
|
203 | 203 | * @return string |
204 | 204 | */ |
205 | 205 | protected function getMethodBody($reflectionMethod) |
@@ -7,252 +7,252 @@ |
||
7 | 7 | |
8 | 8 | class GenerateDoc extends Command |
9 | 9 | { |
10 | - /** |
|
11 | - * The name and signature of the console command. |
|
12 | - * |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $signature = 'doc:generate'; |
|
10 | + /** |
|
11 | + * The name and signature of the console command. |
|
12 | + * |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $signature = 'doc:generate'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * The console command description. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $description = 'Generate api documentation'; |
|
17 | + /** |
|
18 | + * The console command description. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $description = 'Generate api documentation'; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Create a new command instance. |
|
26 | - */ |
|
27 | - public function __construct() |
|
28 | - { |
|
29 | - parent::__construct(); |
|
30 | - } |
|
24 | + /** |
|
25 | + * Create a new command instance. |
|
26 | + */ |
|
27 | + public function __construct() |
|
28 | + { |
|
29 | + parent::__construct(); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Execute the console command. |
|
34 | - * |
|
35 | - * @return mixed |
|
36 | - */ |
|
37 | - public function handle() |
|
38 | - { |
|
39 | - $docData = []; |
|
40 | - $docData['models'] = []; |
|
41 | - $routes = $this->getRoutes(); |
|
42 | - foreach ($routes as $route) { |
|
43 | - if ($route) { |
|
44 | - $actoinArray = explode('@', $route['action']); |
|
45 | - if (Arr::get($actoinArray, 1, false)) { |
|
46 | - $controller = $actoinArray[0]; |
|
47 | - $method = $actoinArray[1]; |
|
48 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
32 | + /** |
|
33 | + * Execute the console command. |
|
34 | + * |
|
35 | + * @return mixed |
|
36 | + */ |
|
37 | + public function handle() |
|
38 | + { |
|
39 | + $docData = []; |
|
40 | + $docData['models'] = []; |
|
41 | + $routes = $this->getRoutes(); |
|
42 | + foreach ($routes as $route) { |
|
43 | + if ($route) { |
|
44 | + $actoinArray = explode('@', $route['action']); |
|
45 | + if (Arr::get($actoinArray, 1, false)) { |
|
46 | + $controller = $actoinArray[0]; |
|
47 | + $method = $actoinArray[1]; |
|
48 | + $route['name'] = $method !== 'index' ? $method : 'list'; |
|
49 | 49 | |
50 | - $reflectionClass = new \ReflectionClass($controller); |
|
51 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
52 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
53 | - $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
54 | - $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
50 | + $reflectionClass = new \ReflectionClass($controller); |
|
51 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
52 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
53 | + $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
54 | + $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
55 | 55 | |
56 | - $this->processDocBlock($route, $reflectionMethod); |
|
57 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
58 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
56 | + $this->processDocBlock($route, $reflectionMethod); |
|
57 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
58 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
59 | 59 | |
60 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
60 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
61 | 61 | |
62 | - preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
63 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
62 | + preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
63 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
64 | 64 | |
65 | - $this->getModels($classProperties['model'], $docData); |
|
66 | - } |
|
67 | - } |
|
68 | - } |
|
65 | + $this->getModels($classProperties['model'], $docData); |
|
66 | + } |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - $docData['errors'] = $this->getErrors(); |
|
71 | - $docData['reports'] = \Core::reports()->all(); |
|
72 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
73 | - } |
|
70 | + $docData['errors'] = $this->getErrors(); |
|
71 | + $docData['reports'] = \Core::reports()->all(); |
|
72 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get list of all registered routes. |
|
77 | - * |
|
78 | - * @return collection |
|
79 | - */ |
|
80 | - protected function getRoutes() |
|
81 | - { |
|
82 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
83 | - if (strpos($route->uri(), 'api/') !== false) { |
|
84 | - return [ |
|
85 | - 'method' => $route->methods()[0], |
|
86 | - 'uri' => $route->uri(), |
|
87 | - 'action' => $route->getActionName(), |
|
88 | - 'prefix' => $route->getPrefix() |
|
89 | - ]; |
|
90 | - } |
|
91 | - return false; |
|
92 | - })->all(); |
|
93 | - } |
|
75 | + /** |
|
76 | + * Get list of all registered routes. |
|
77 | + * |
|
78 | + * @return collection |
|
79 | + */ |
|
80 | + protected function getRoutes() |
|
81 | + { |
|
82 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
83 | + if (strpos($route->uri(), 'api/') !== false) { |
|
84 | + return [ |
|
85 | + 'method' => $route->methods()[0], |
|
86 | + 'uri' => $route->uri(), |
|
87 | + 'action' => $route->getActionName(), |
|
88 | + 'prefix' => $route->getPrefix() |
|
89 | + ]; |
|
90 | + } |
|
91 | + return false; |
|
92 | + })->all(); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Generate headers for the given route. |
|
97 | - * |
|
98 | - * @param array &$route |
|
99 | - * @param string $method |
|
100 | - * @param array $skipLoginCheck |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
104 | - { |
|
105 | - $route['headers'] = [ |
|
106 | - 'Accept' => 'application/json', |
|
107 | - 'Content-Type' => 'application/json', |
|
108 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
109 | - 'time-zone' => 'Your locale time zone', |
|
110 | - ]; |
|
95 | + /** |
|
96 | + * Generate headers for the given route. |
|
97 | + * |
|
98 | + * @param array &$route |
|
99 | + * @param string $method |
|
100 | + * @param array $skipLoginCheck |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
104 | + { |
|
105 | + $route['headers'] = [ |
|
106 | + 'Accept' => 'application/json', |
|
107 | + 'Content-Type' => 'application/json', |
|
108 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
109 | + 'time-zone' => 'Your locale time zone', |
|
110 | + ]; |
|
111 | 111 | |
112 | 112 | |
113 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
114 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
115 | - } |
|
116 | - } |
|
113 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
114 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
115 | + } |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Generate description and params for the given route |
|
120 | - * based on the docblock. |
|
121 | - * |
|
122 | - * @param array &$route |
|
123 | - * @param \ReflectionMethod $reflectionMethod |
|
124 | - * @return void |
|
125 | - */ |
|
126 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
127 | - { |
|
128 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
129 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
130 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
131 | - $params = $docblock->getTagsByName('param'); |
|
132 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
133 | - foreach ($params as $param) { |
|
134 | - $name = $param->getVariableName(); |
|
135 | - if ($name !== 'request') { |
|
136 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
137 | - } |
|
138 | - } |
|
139 | - } |
|
118 | + /** |
|
119 | + * Generate description and params for the given route |
|
120 | + * based on the docblock. |
|
121 | + * |
|
122 | + * @param array &$route |
|
123 | + * @param \ReflectionMethod $reflectionMethod |
|
124 | + * @return void |
|
125 | + */ |
|
126 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
127 | + { |
|
128 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
129 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
130 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
131 | + $params = $docblock->getTagsByName('param'); |
|
132 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
133 | + foreach ($params as $param) { |
|
134 | + $name = $param->getVariableName(); |
|
135 | + if ($name !== 'request') { |
|
136 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
137 | + } |
|
138 | + } |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * Generate post body for the given route. |
|
143 | - * |
|
144 | - * @param array &$route |
|
145 | - * @param \ReflectionMethod $reflectionMethod |
|
146 | - * @param array $validationRules |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
150 | - { |
|
151 | - if ($route['method'] == 'POST') { |
|
152 | - $body = $this->getMethodBody($reflectionMethod); |
|
141 | + /** |
|
142 | + * Generate post body for the given route. |
|
143 | + * |
|
144 | + * @param array &$route |
|
145 | + * @param \ReflectionMethod $reflectionMethod |
|
146 | + * @param array $validationRules |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
150 | + { |
|
151 | + if ($route['method'] == 'POST') { |
|
152 | + $body = $this->getMethodBody($reflectionMethod); |
|
153 | 153 | |
154 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
155 | - if (count($match)) { |
|
156 | - if ($match[1] == '$this->validationRules') { |
|
157 | - $route['body'] = $validationRules; |
|
158 | - } else { |
|
159 | - $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
160 | - } |
|
154 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
155 | + if (count($match)) { |
|
156 | + if ($match[1] == '$this->validationRules') { |
|
157 | + $route['body'] = $validationRules; |
|
158 | + } else { |
|
159 | + $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
160 | + } |
|
161 | 161 | |
162 | - foreach ($route['body'] as &$rule) { |
|
163 | - if (strpos($rule, 'unique')) { |
|
164 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
165 | - } elseif (strpos($rule, 'exists')) { |
|
166 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
167 | - } |
|
168 | - } |
|
169 | - } else { |
|
170 | - $route['body'] = 'conditions'; |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
162 | + foreach ($route['body'] as &$rule) { |
|
163 | + if (strpos($rule, 'unique')) { |
|
164 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
165 | + } elseif (strpos($rule, 'exists')) { |
|
166 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
167 | + } |
|
168 | + } |
|
169 | + } else { |
|
170 | + $route['body'] = 'conditions'; |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Generate application errors. |
|
177 | - * |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - protected function getErrors() |
|
181 | - { |
|
182 | - $errors = []; |
|
183 | - $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
184 | - foreach ($reflectionClass->getMethods() as $method) { |
|
185 | - $methodName = $method->name; |
|
186 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
187 | - $body = $this->getMethodBody($reflectionMethod); |
|
175 | + /** |
|
176 | + * Generate application errors. |
|
177 | + * |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + protected function getErrors() |
|
181 | + { |
|
182 | + $errors = []; |
|
183 | + $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
184 | + foreach ($reflectionClass->getMethods() as $method) { |
|
185 | + $methodName = $method->name; |
|
186 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
187 | + $body = $this->getMethodBody($reflectionMethod); |
|
188 | 188 | |
189 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
189 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
190 | 190 | |
191 | - if (count($match)) { |
|
192 | - $errors[$match[1]][] = $methodName; |
|
193 | - } |
|
194 | - } |
|
191 | + if (count($match)) { |
|
192 | + $errors[$match[1]][] = $methodName; |
|
193 | + } |
|
194 | + } |
|
195 | 195 | |
196 | - return $errors; |
|
197 | - } |
|
196 | + return $errors; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get the given method body code. |
|
201 | - * |
|
202 | - * @param object $reflectionMethod |
|
203 | - * @return string |
|
204 | - */ |
|
205 | - protected function getMethodBody($reflectionMethod) |
|
206 | - { |
|
207 | - $filename = $reflectionMethod->getFileName(); |
|
208 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
209 | - $end_line = $reflectionMethod->getEndLine(); |
|
210 | - $length = $end_line - $start_line; |
|
211 | - $source = file($filename); |
|
212 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
213 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
199 | + /** |
|
200 | + * Get the given method body code. |
|
201 | + * |
|
202 | + * @param object $reflectionMethod |
|
203 | + * @return string |
|
204 | + */ |
|
205 | + protected function getMethodBody($reflectionMethod) |
|
206 | + { |
|
207 | + $filename = $reflectionMethod->getFileName(); |
|
208 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
209 | + $end_line = $reflectionMethod->getEndLine(); |
|
210 | + $length = $end_line - $start_line; |
|
211 | + $source = file($filename); |
|
212 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
213 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
214 | 214 | |
215 | - return $body; |
|
216 | - } |
|
215 | + return $body; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Get example object of all availble models. |
|
220 | - * |
|
221 | - * @param string $modelName |
|
222 | - * @param array $docData |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - protected function getModels($modelName, &$docData) |
|
226 | - { |
|
227 | - if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
228 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
229 | - $model = factory($modelClass)->make(); |
|
230 | - $modelArr = $model->toArray(); |
|
218 | + /** |
|
219 | + * Get example object of all availble models. |
|
220 | + * |
|
221 | + * @param string $modelName |
|
222 | + * @param array $docData |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + protected function getModels($modelName, &$docData) |
|
226 | + { |
|
227 | + if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
228 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
229 | + $model = factory($modelClass)->make(); |
|
230 | + $modelArr = $model->toArray(); |
|
231 | 231 | |
232 | - if ($model->trans && ! $model->trans->count()) { |
|
233 | - $modelArr['trans'] = [ |
|
234 | - 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
235 | - ]; |
|
236 | - } |
|
232 | + if ($model->trans && ! $model->trans->count()) { |
|
233 | + $modelArr['trans'] = [ |
|
234 | + 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
235 | + ]; |
|
236 | + } |
|
237 | 237 | |
238 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
239 | - } |
|
240 | - } |
|
238 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
239 | + } |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * Get the route response object type. |
|
244 | - * |
|
245 | - * @param string $modelName |
|
246 | - * @param string $method |
|
247 | - * @param string $returnDocBlock |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
251 | - { |
|
252 | - $config = \CoreConfig::getConfig(); |
|
253 | - $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
254 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
242 | + /** |
|
243 | + * Get the route response object type. |
|
244 | + * |
|
245 | + * @param string $modelName |
|
246 | + * @param string $method |
|
247 | + * @param string $returnDocBlock |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
251 | + { |
|
252 | + $config = \CoreConfig::getConfig(); |
|
253 | + $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
254 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
255 | 255 | |
256 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
257 | - } |
|
256 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
257 | + } |
|
258 | 258 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | */ |
80 | 80 | protected function getRoutes() |
81 | 81 | { |
82 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
82 | + return collect(\Route::getRoutes())->map(function($route) { |
|
83 | 83 | if (strpos($route->uri(), 'api/') !== false) { |
84 | 84 | return [ |
85 | 85 | 'method' => $route->methods()[0], |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | ]; |
111 | 111 | |
112 | 112 | |
113 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
113 | + if ( ! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
114 | 114 | $route['headers']['Authorization'] = 'Bearer {token}'; |
115 | 115 | } |
116 | 116 | } |
@@ -28,10 +28,6 @@ discard block |
||
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
31 | - * @param string $uri |
|
32 | - * @param array $data |
|
33 | - * @param array $headers |
|
34 | - * @param string $content |
|
35 | 31 | * @return \Illuminate\Http\Response |
36 | 32 | */ |
37 | 33 | public function get() |
@@ -40,10 +36,6 @@ discard block |
||
40 | 36 | } |
41 | 37 | |
42 | 38 | /** |
43 | - * @param string $uri |
|
44 | - * @param array $data |
|
45 | - * @param array $headers |
|
46 | - * @param string $content |
|
47 | 39 | * @return \Illuminate\Http\Response |
48 | 40 | */ |
49 | 41 | public function post() |
@@ -52,10 +44,6 @@ discard block |
||
52 | 44 | } |
53 | 45 | |
54 | 46 | /** |
55 | - * @param string $uri |
|
56 | - * @param array $data |
|
57 | - * @param array $headers |
|
58 | - * @param string $content |
|
59 | 47 | * @return \Illuminate\Http\Response |
60 | 48 | */ |
61 | 49 | public function put() |
@@ -64,10 +52,6 @@ discard block |
||
64 | 52 | } |
65 | 53 | |
66 | 54 | /** |
67 | - * @param string $uri |
|
68 | - * @param array $data |
|
69 | - * @param array $headers |
|
70 | - * @param string $content |
|
71 | 55 | * @return \Illuminate\Http\Response |
72 | 56 | */ |
73 | 57 | public function delete() |
@@ -7,218 +7,218 @@ |
||
7 | 7 | class ApiConsumer |
8 | 8 | { |
9 | 9 | |
10 | - private $app; |
|
11 | - |
|
12 | - private $router; |
|
13 | - |
|
14 | - private $request; |
|
15 | - |
|
16 | - private $disableMiddleware = false; |
|
17 | - |
|
18 | - /** |
|
19 | - * @param \Illuminate\Foundation\Application $app |
|
20 | - * @param \Illuminate\Http\Request $request, |
|
21 | - * @param \Illuminate\Routing\Router $router |
|
22 | - */ |
|
23 | - public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
24 | - { |
|
25 | - $this->app = $app; |
|
26 | - $this->request = $request; |
|
27 | - $this->router = $router; |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * @param string $uri |
|
32 | - * @param array $data |
|
33 | - * @param array $headers |
|
34 | - * @param string $content |
|
35 | - * @return \Illuminate\Http\Response |
|
36 | - */ |
|
37 | - public function get() |
|
38 | - { |
|
39 | - return $this->quickCall('GET', func_get_args()); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @param string $uri |
|
44 | - * @param array $data |
|
45 | - * @param array $headers |
|
46 | - * @param string $content |
|
47 | - * @return \Illuminate\Http\Response |
|
48 | - */ |
|
49 | - public function post() |
|
50 | - { |
|
51 | - return $this->quickCall('POST', func_get_args()); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param string $uri |
|
56 | - * @param array $data |
|
57 | - * @param array $headers |
|
58 | - * @param string $content |
|
59 | - * @return \Illuminate\Http\Response |
|
60 | - */ |
|
61 | - public function put() |
|
62 | - { |
|
63 | - return $this->quickCall('PUT', func_get_args()); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @param string $uri |
|
68 | - * @param array $data |
|
69 | - * @param array $headers |
|
70 | - * @param string $content |
|
71 | - * @return \Illuminate\Http\Response |
|
72 | - */ |
|
73 | - public function delete() |
|
74 | - { |
|
75 | - return $this->quickCall('DELETE', func_get_args()); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param array $requests An array of requests |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - public function batchRequest(array $requests) |
|
83 | - { |
|
84 | - foreach ($requests as $i => $request) { |
|
85 | - $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
86 | - } |
|
87 | - |
|
88 | - return $requests; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $method |
|
93 | - * @param array $args |
|
94 | - * @return \Illuminate\Http\Response |
|
95 | - */ |
|
96 | - public function quickCall($method, array $args) |
|
97 | - { |
|
98 | - array_unshift($args, $method); |
|
99 | - return call_user_func_array([$this, "singleRequest"], $args); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $method |
|
104 | - * @param string $uri |
|
105 | - * @param array $data |
|
106 | - * @param array $headers |
|
107 | - * @param string $content |
|
108 | - * @return \Illuminate\Http\Response |
|
109 | - */ |
|
110 | - public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
111 | - { |
|
112 | - // Save the current request so we can reset the router back to it |
|
113 | - // after we've completed our internal request. |
|
114 | - $currentRequest = $this->request->instance()->duplicate(); |
|
115 | - |
|
116 | - $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
117 | - |
|
118 | - if ($this->disableMiddleware) { |
|
119 | - $this->app->instance('middleware.disable', true); |
|
120 | - } |
|
121 | - |
|
122 | - $response = $this->request($method, $uri, $data, $headers, $content); |
|
123 | - |
|
124 | - if ($this->disableMiddleware) { |
|
125 | - $this->app->instance('middleware.disable', false); |
|
126 | - } |
|
127 | - |
|
128 | - // Once the request has completed we reset the currentRequest of the router |
|
129 | - // to match the original request. |
|
130 | - $this->request->instance()->initialize( |
|
131 | - $currentRequest->query->all(), |
|
132 | - $currentRequest->request->all(), |
|
133 | - $currentRequest->attributes->all(), |
|
134 | - $currentRequest->cookies->all(), |
|
135 | - $currentRequest->files->all(), |
|
136 | - $currentRequest->server->all(), |
|
137 | - $currentRequest->content |
|
138 | - ); |
|
139 | - |
|
140 | - return $response; |
|
141 | - } |
|
142 | - |
|
143 | - private function overrideHeaders(array $default, array $headers) |
|
144 | - { |
|
145 | - $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
146 | - return array_merge($default, $headers); |
|
147 | - } |
|
148 | - |
|
149 | - public function enableMiddleware() |
|
150 | - { |
|
151 | - $this->disableMiddleware = false; |
|
152 | - } |
|
153 | - |
|
154 | - public function disableMiddleware() |
|
155 | - { |
|
156 | - $this->disableMiddleware = true; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param string $method |
|
161 | - * @param string $uri |
|
162 | - * @param array $data |
|
163 | - * @param array $headers |
|
164 | - * @param string $content |
|
165 | - * @return \Illuminate\Http\Response |
|
166 | - */ |
|
167 | - private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
168 | - { |
|
169 | - // Create a new request object for the internal request |
|
170 | - $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
171 | - |
|
172 | - // Handle the request in the kernel and prepare a response |
|
173 | - $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
174 | - |
|
175 | - return $response; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @param string $method |
|
180 | - * @param string $uri |
|
181 | - * @param array $data |
|
182 | - * @param array $headers |
|
183 | - * @param string $content |
|
184 | - * @return \Illuminate\Http\Request |
|
185 | - */ |
|
186 | - private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
187 | - { |
|
188 | - $server = $this->transformHeadersToServerVariables($headers); |
|
189 | - |
|
190 | - return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
191 | - } |
|
192 | - |
|
193 | - private function transformHeadersToUppercaseUnderscoreType($headers) |
|
194 | - { |
|
195 | - $transformed = []; |
|
196 | - |
|
197 | - foreach ($headers as $headerType => $headerValue) { |
|
198 | - $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
199 | - |
|
200 | - $transformed[$headerType] = $headerValue; |
|
201 | - } |
|
202 | - |
|
203 | - return $transformed; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * https://github.com/symfony/symfony/issues/5074 |
|
208 | - * |
|
209 | - * @param array $headers |
|
210 | - * @return array |
|
211 | - */ |
|
212 | - private function transformHeadersToServerVariables($headers) |
|
213 | - { |
|
214 | - $server = []; |
|
215 | - |
|
216 | - foreach ($headers as $headerType => $headerValue) { |
|
217 | - $headerType = 'HTTP_' . $headerType; |
|
218 | - |
|
219 | - $server[$headerType] = $headerValue; |
|
220 | - } |
|
221 | - |
|
222 | - return $server; |
|
223 | - } |
|
10 | + private $app; |
|
11 | + |
|
12 | + private $router; |
|
13 | + |
|
14 | + private $request; |
|
15 | + |
|
16 | + private $disableMiddleware = false; |
|
17 | + |
|
18 | + /** |
|
19 | + * @param \Illuminate\Foundation\Application $app |
|
20 | + * @param \Illuminate\Http\Request $request, |
|
21 | + * @param \Illuminate\Routing\Router $router |
|
22 | + */ |
|
23 | + public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
24 | + { |
|
25 | + $this->app = $app; |
|
26 | + $this->request = $request; |
|
27 | + $this->router = $router; |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * @param string $uri |
|
32 | + * @param array $data |
|
33 | + * @param array $headers |
|
34 | + * @param string $content |
|
35 | + * @return \Illuminate\Http\Response |
|
36 | + */ |
|
37 | + public function get() |
|
38 | + { |
|
39 | + return $this->quickCall('GET', func_get_args()); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @param string $uri |
|
44 | + * @param array $data |
|
45 | + * @param array $headers |
|
46 | + * @param string $content |
|
47 | + * @return \Illuminate\Http\Response |
|
48 | + */ |
|
49 | + public function post() |
|
50 | + { |
|
51 | + return $this->quickCall('POST', func_get_args()); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param string $uri |
|
56 | + * @param array $data |
|
57 | + * @param array $headers |
|
58 | + * @param string $content |
|
59 | + * @return \Illuminate\Http\Response |
|
60 | + */ |
|
61 | + public function put() |
|
62 | + { |
|
63 | + return $this->quickCall('PUT', func_get_args()); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @param string $uri |
|
68 | + * @param array $data |
|
69 | + * @param array $headers |
|
70 | + * @param string $content |
|
71 | + * @return \Illuminate\Http\Response |
|
72 | + */ |
|
73 | + public function delete() |
|
74 | + { |
|
75 | + return $this->quickCall('DELETE', func_get_args()); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param array $requests An array of requests |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + public function batchRequest(array $requests) |
|
83 | + { |
|
84 | + foreach ($requests as $i => $request) { |
|
85 | + $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
86 | + } |
|
87 | + |
|
88 | + return $requests; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $method |
|
93 | + * @param array $args |
|
94 | + * @return \Illuminate\Http\Response |
|
95 | + */ |
|
96 | + public function quickCall($method, array $args) |
|
97 | + { |
|
98 | + array_unshift($args, $method); |
|
99 | + return call_user_func_array([$this, "singleRequest"], $args); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $method |
|
104 | + * @param string $uri |
|
105 | + * @param array $data |
|
106 | + * @param array $headers |
|
107 | + * @param string $content |
|
108 | + * @return \Illuminate\Http\Response |
|
109 | + */ |
|
110 | + public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
111 | + { |
|
112 | + // Save the current request so we can reset the router back to it |
|
113 | + // after we've completed our internal request. |
|
114 | + $currentRequest = $this->request->instance()->duplicate(); |
|
115 | + |
|
116 | + $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
117 | + |
|
118 | + if ($this->disableMiddleware) { |
|
119 | + $this->app->instance('middleware.disable', true); |
|
120 | + } |
|
121 | + |
|
122 | + $response = $this->request($method, $uri, $data, $headers, $content); |
|
123 | + |
|
124 | + if ($this->disableMiddleware) { |
|
125 | + $this->app->instance('middleware.disable', false); |
|
126 | + } |
|
127 | + |
|
128 | + // Once the request has completed we reset the currentRequest of the router |
|
129 | + // to match the original request. |
|
130 | + $this->request->instance()->initialize( |
|
131 | + $currentRequest->query->all(), |
|
132 | + $currentRequest->request->all(), |
|
133 | + $currentRequest->attributes->all(), |
|
134 | + $currentRequest->cookies->all(), |
|
135 | + $currentRequest->files->all(), |
|
136 | + $currentRequest->server->all(), |
|
137 | + $currentRequest->content |
|
138 | + ); |
|
139 | + |
|
140 | + return $response; |
|
141 | + } |
|
142 | + |
|
143 | + private function overrideHeaders(array $default, array $headers) |
|
144 | + { |
|
145 | + $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
146 | + return array_merge($default, $headers); |
|
147 | + } |
|
148 | + |
|
149 | + public function enableMiddleware() |
|
150 | + { |
|
151 | + $this->disableMiddleware = false; |
|
152 | + } |
|
153 | + |
|
154 | + public function disableMiddleware() |
|
155 | + { |
|
156 | + $this->disableMiddleware = true; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param string $method |
|
161 | + * @param string $uri |
|
162 | + * @param array $data |
|
163 | + * @param array $headers |
|
164 | + * @param string $content |
|
165 | + * @return \Illuminate\Http\Response |
|
166 | + */ |
|
167 | + private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
168 | + { |
|
169 | + // Create a new request object for the internal request |
|
170 | + $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
171 | + |
|
172 | + // Handle the request in the kernel and prepare a response |
|
173 | + $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
174 | + |
|
175 | + return $response; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @param string $method |
|
180 | + * @param string $uri |
|
181 | + * @param array $data |
|
182 | + * @param array $headers |
|
183 | + * @param string $content |
|
184 | + * @return \Illuminate\Http\Request |
|
185 | + */ |
|
186 | + private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
187 | + { |
|
188 | + $server = $this->transformHeadersToServerVariables($headers); |
|
189 | + |
|
190 | + return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
191 | + } |
|
192 | + |
|
193 | + private function transformHeadersToUppercaseUnderscoreType($headers) |
|
194 | + { |
|
195 | + $transformed = []; |
|
196 | + |
|
197 | + foreach ($headers as $headerType => $headerValue) { |
|
198 | + $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
199 | + |
|
200 | + $transformed[$headerType] = $headerValue; |
|
201 | + } |
|
202 | + |
|
203 | + return $transformed; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * https://github.com/symfony/symfony/issues/5074 |
|
208 | + * |
|
209 | + * @param array $headers |
|
210 | + * @return array |
|
211 | + */ |
|
212 | + private function transformHeadersToServerVariables($headers) |
|
213 | + { |
|
214 | + $server = []; |
|
215 | + |
|
216 | + foreach ($headers as $headerType => $headerValue) { |
|
217 | + $headerType = 'HTTP_' . $headerType; |
|
218 | + |
|
219 | + $server[$headerType] = $headerValue; |
|
220 | + } |
|
221 | + |
|
222 | + return $server; |
|
223 | + } |
|
224 | 224 | } |
@@ -214,7 +214,7 @@ |
||
214 | 214 | $server = []; |
215 | 215 | |
216 | 216 | foreach ($headers as $headerType => $headerValue) { |
217 | - $headerType = 'HTTP_' . $headerType; |
|
217 | + $headerType = 'HTTP_'.$headerType; |
|
218 | 218 | |
219 | 219 | $server[$headerType] = $headerValue; |
220 | 220 | } |
@@ -5,23 +5,23 @@ |
||
5 | 5 | class Notification extends DatabaseNotification |
6 | 6 | { |
7 | 7 | |
8 | - public function getCreatedAtAttribute($value) |
|
9 | - { |
|
10 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
11 | - } |
|
8 | + public function getCreatedAtAttribute($value) |
|
9 | + { |
|
10 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
11 | + } |
|
12 | 12 | |
13 | - public function getUpdatedAtAttribute($value) |
|
14 | - { |
|
15 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
16 | - } |
|
13 | + public function getUpdatedAtAttribute($value) |
|
14 | + { |
|
15 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
16 | + } |
|
17 | 17 | |
18 | - public function getDeletedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | - } |
|
18 | + public function getDeletedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getReadAtAttribute($value) |
|
24 | - { |
|
25 | - return ! $value ? false : \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
26 | - } |
|
23 | + public function getReadAtAttribute($value) |
|
24 | + { |
|
25 | + return ! $value ? false : \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
26 | + } |
|
27 | 27 | } |
@@ -1,13 +1,13 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\Notifications\Notification::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
5 | - 'type' => '', |
|
6 | - 'notifiable_type' => '', |
|
7 | - 'notifiable_id' => '', |
|
8 | - 'data' => '', |
|
9 | - 'read_at' => null, |
|
10 | - 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
|
11 | - 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
|
12 | - ]; |
|
4 | + return [ |
|
5 | + 'type' => '', |
|
6 | + 'notifiable_type' => '', |
|
7 | + 'notifiable_id' => '', |
|
8 | + 'data' => '', |
|
9 | + 'read_at' => null, |
|
10 | + 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
|
11 | + 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
|
12 | + ]; |
|
13 | 13 | }); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\Notifications\Notification::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\Notifications\Notification::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'type' => '', |
6 | 6 | 'notifiable_type' => '', |
@@ -1,10 +1,10 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\Notifications\PushNotificationDevice::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
5 | - 'device_token' => $faker->sha1(), |
|
6 | - 'user_id' => $faker->randomDigitNotNull(), |
|
7 | - 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
|
8 | - 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
|
9 | - ]; |
|
4 | + return [ |
|
5 | + 'device_token' => $faker->sha1(), |
|
6 | + 'user_id' => $faker->randomDigitNotNull(), |
|
7 | + 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
|
8 | + 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
|
9 | + ]; |
|
10 | 10 | }); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\Notifications\PushNotificationDevice::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\Notifications\PushNotificationDevice::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'device_token' => $faker->sha1(), |
6 | 6 | 'user_id' => $faker->randomDigitNotNull(), |
@@ -5,31 +5,31 @@ |
||
5 | 5 | |
6 | 6 | class PushNotificationsDevices extends Migration |
7 | 7 | { |
8 | - /** |
|
9 | - * Run the migrations. |
|
10 | - * |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function up() |
|
14 | - { |
|
15 | - Schema::create('push_notifications_devices', function (Blueprint $table) { |
|
16 | - $table->increments('id'); |
|
17 | - $table->string('device_token'); |
|
18 | - $table->integer('user_id'); |
|
19 | - $table->text('access_token')->nullable(); |
|
20 | - $table->unique(array('device_token', 'user_id')); |
|
21 | - $table->softDeletes(); |
|
22 | - $table->timestamps(); |
|
23 | - }); |
|
24 | - } |
|
8 | + /** |
|
9 | + * Run the migrations. |
|
10 | + * |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function up() |
|
14 | + { |
|
15 | + Schema::create('push_notifications_devices', function (Blueprint $table) { |
|
16 | + $table->increments('id'); |
|
17 | + $table->string('device_token'); |
|
18 | + $table->integer('user_id'); |
|
19 | + $table->text('access_token')->nullable(); |
|
20 | + $table->unique(array('device_token', 'user_id')); |
|
21 | + $table->softDeletes(); |
|
22 | + $table->timestamps(); |
|
23 | + }); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Reverse the migrations. |
|
28 | - * |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function down() |
|
32 | - { |
|
33 | - Schema::dropIfExists('push_notifications_devices'); |
|
34 | - } |
|
26 | + /** |
|
27 | + * Reverse the migrations. |
|
28 | + * |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function down() |
|
32 | + { |
|
33 | + Schema::dropIfExists('push_notifications_devices'); |
|
34 | + } |
|
35 | 35 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | */ |
13 | 13 | public function up() |
14 | 14 | { |
15 | - Schema::create('push_notifications_devices', function (Blueprint $table) { |
|
15 | + Schema::create('push_notifications_devices', function(Blueprint $table) { |
|
16 | 16 | $table->increments('id'); |
17 | 17 | $table->string('device_token'); |
18 | 18 | $table->integer('user_id'); |