@@ -7,64 +7,64 @@ |
||
7 | 7 | |
8 | 8 | class OauthClientRepository extends BaseRepository |
9 | 9 | { |
10 | - /** |
|
11 | - * Init new object. |
|
12 | - * |
|
13 | - * @param OauthClient $model |
|
14 | - * @return void |
|
15 | - */ |
|
16 | - public function __construct(OauthClient $model) |
|
17 | - { |
|
18 | - parent::__construct($model); |
|
19 | - } |
|
10 | + /** |
|
11 | + * Init new object. |
|
12 | + * |
|
13 | + * @param OauthClient $model |
|
14 | + * @return void |
|
15 | + */ |
|
16 | + public function __construct(OauthClient $model) |
|
17 | + { |
|
18 | + parent::__construct($model); |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * Revoke the given client tokens. |
|
23 | - * |
|
24 | - * @param mixed $client |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - public function revokeClientTokens($client) |
|
28 | - { |
|
29 | - $client = ! filter_var($client, FILTER_VALIDATE_INT) ? $client : $this->find($client); |
|
30 | - $client->tokens()->update(['revoked' => true]); |
|
31 | - } |
|
21 | + /** |
|
22 | + * Revoke the given client tokens. |
|
23 | + * |
|
24 | + * @param mixed $client |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + public function revokeClientTokens($client) |
|
28 | + { |
|
29 | + $client = ! filter_var($client, FILTER_VALIDATE_INT) ? $client : $this->find($client); |
|
30 | + $client->tokens()->update(['revoked' => true]); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Ensure access token hasn't expired or revoked. |
|
35 | - * |
|
36 | - * @param string $accessToken |
|
37 | - * @return boolean |
|
38 | - */ |
|
39 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
40 | - { |
|
41 | - $accessTokenId = json_decode($accessToken, true)['id']; |
|
42 | - $accessToken = \DB::table('oauth_access_tokens') |
|
43 | - ->where('id', $accessTokenId) |
|
44 | - ->first(); |
|
33 | + /** |
|
34 | + * Ensure access token hasn't expired or revoked. |
|
35 | + * |
|
36 | + * @param string $accessToken |
|
37 | + * @return boolean |
|
38 | + */ |
|
39 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
40 | + { |
|
41 | + $accessTokenId = json_decode($accessToken, true)['id']; |
|
42 | + $accessToken = \DB::table('oauth_access_tokens') |
|
43 | + ->where('id', $accessTokenId) |
|
44 | + ->first(); |
|
45 | 45 | |
46 | - if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
47 | - return true; |
|
48 | - } |
|
46 | + if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
47 | + return true; |
|
48 | + } |
|
49 | 49 | |
50 | - return false; |
|
51 | - } |
|
50 | + return false; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Revoke the given access token and all |
|
55 | - * associated refresh tokens. |
|
56 | - * |
|
57 | - * @param oject $accessToken |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function revokeAccessToken($accessToken) |
|
61 | - { |
|
62 | - \DB::table('oauth_refresh_tokens') |
|
63 | - ->where('access_token_id', $accessToken->id) |
|
64 | - ->update([ |
|
65 | - 'revoked' => true |
|
66 | - ]); |
|
53 | + /** |
|
54 | + * Revoke the given access token and all |
|
55 | + * associated refresh tokens. |
|
56 | + * |
|
57 | + * @param oject $accessToken |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function revokeAccessToken($accessToken) |
|
61 | + { |
|
62 | + \DB::table('oauth_refresh_tokens') |
|
63 | + ->where('access_token_id', $accessToken->id) |
|
64 | + ->update([ |
|
65 | + 'revoked' => true |
|
66 | + ]); |
|
67 | 67 | |
68 | - $accessToken->revoke(); |
|
69 | - } |
|
68 | + $accessToken->revoke(); |
|
69 | + } |
|
70 | 70 | } |
@@ -6,30 +6,30 @@ |
||
6 | 6 | |
7 | 7 | class Core implements BaseFactoryInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * Construct the repository class name based on |
|
11 | - * the method name called, search in the |
|
12 | - * given namespaces for the class and |
|
13 | - * return an instance. |
|
14 | - * |
|
15 | - * @param string $name the called method name |
|
16 | - * @param array $arguments the method arguments |
|
17 | - * @return object |
|
18 | - */ |
|
19 | - public function __call($name, $arguments) |
|
20 | - { |
|
21 | - foreach (\Module::all() as $module) { |
|
22 | - $nameSpace = 'App\\Modules\\' . $module['basename'] ; |
|
23 | - $model = ucfirst(\Str::singular($name)); |
|
24 | - if(count($arguments) == 1 && $arguments[0]) { |
|
25 | - $class = $nameSpace . '\\Services\\' . $model . 'Service'; |
|
26 | - } else { |
|
27 | - $class = $nameSpace . '\\Repositories\\' . $model . 'Repository'; |
|
28 | - } |
|
9 | + /** |
|
10 | + * Construct the repository class name based on |
|
11 | + * the method name called, search in the |
|
12 | + * given namespaces for the class and |
|
13 | + * return an instance. |
|
14 | + * |
|
15 | + * @param string $name the called method name |
|
16 | + * @param array $arguments the method arguments |
|
17 | + * @return object |
|
18 | + */ |
|
19 | + public function __call($name, $arguments) |
|
20 | + { |
|
21 | + foreach (\Module::all() as $module) { |
|
22 | + $nameSpace = 'App\\Modules\\' . $module['basename'] ; |
|
23 | + $model = ucfirst(\Str::singular($name)); |
|
24 | + if(count($arguments) == 1 && $arguments[0]) { |
|
25 | + $class = $nameSpace . '\\Services\\' . $model . 'Service'; |
|
26 | + } else { |
|
27 | + $class = $nameSpace . '\\Repositories\\' . $model . 'Repository'; |
|
28 | + } |
|
29 | 29 | |
30 | - if (class_exists($class)) { |
|
31 | - return \App::make($class); |
|
32 | - } |
|
33 | - } |
|
34 | - } |
|
30 | + if (class_exists($class)) { |
|
31 | + return \App::make($class); |
|
32 | + } |
|
33 | + } |
|
34 | + } |
|
35 | 35 | } |
@@ -4,46 +4,46 @@ |
||
4 | 4 | |
5 | 5 | trait Translatable |
6 | 6 | { |
7 | - /** |
|
8 | - * Create a new model instance that is existing. |
|
9 | - * |
|
10 | - * @param array $attributes |
|
11 | - * @param string|null $connection |
|
12 | - * @return static |
|
13 | - */ |
|
14 | - public function newFromBuilder($attributes = [], $connection = null) |
|
15 | - { |
|
16 | - $model = parent::newFromBuilder($attributes, $connection); |
|
17 | - |
|
18 | - foreach ($model->attributes as $key => $value) { |
|
19 | - if (isset($this->translatable) && in_array($key, $this->translatable)) { |
|
20 | - $model->$key = $this->getTranslatedAttribute($value); |
|
21 | - } |
|
22 | - } |
|
23 | - |
|
24 | - return $model; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Returns a translatable model attribute based on the application's locale settings. |
|
29 | - * |
|
30 | - * @param $values |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - protected function getTranslatedAttribute($values) |
|
34 | - { |
|
35 | - $values = json_decode($values); |
|
36 | - $primaryLocale = \Session::get('locale'); |
|
37 | - $fallbackLocale = 'en'; |
|
38 | - |
|
39 | - if ($primaryLocale == 'all') { |
|
40 | - return $values; |
|
41 | - } |
|
42 | - |
|
43 | - if (! $primaryLocale || ! is_object($values) || ! property_exists($values, $primaryLocale)) { |
|
44 | - return $values ? (isset($values->$fallbackLocale) ? $values->$fallbackLocale : $values) : ''; |
|
45 | - } |
|
46 | - |
|
47 | - return $primaryLocale == 'all' ? $values : $values->$primaryLocale; |
|
48 | - } |
|
7 | + /** |
|
8 | + * Create a new model instance that is existing. |
|
9 | + * |
|
10 | + * @param array $attributes |
|
11 | + * @param string|null $connection |
|
12 | + * @return static |
|
13 | + */ |
|
14 | + public function newFromBuilder($attributes = [], $connection = null) |
|
15 | + { |
|
16 | + $model = parent::newFromBuilder($attributes, $connection); |
|
17 | + |
|
18 | + foreach ($model->attributes as $key => $value) { |
|
19 | + if (isset($this->translatable) && in_array($key, $this->translatable)) { |
|
20 | + $model->$key = $this->getTranslatedAttribute($value); |
|
21 | + } |
|
22 | + } |
|
23 | + |
|
24 | + return $model; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Returns a translatable model attribute based on the application's locale settings. |
|
29 | + * |
|
30 | + * @param $values |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + protected function getTranslatedAttribute($values) |
|
34 | + { |
|
35 | + $values = json_decode($values); |
|
36 | + $primaryLocale = \Session::get('locale'); |
|
37 | + $fallbackLocale = 'en'; |
|
38 | + |
|
39 | + if ($primaryLocale == 'all') { |
|
40 | + return $values; |
|
41 | + } |
|
42 | + |
|
43 | + if (! $primaryLocale || ! is_object($values) || ! property_exists($values, $primaryLocale)) { |
|
44 | + return $values ? (isset($values->$fallbackLocale) ? $values->$fallbackLocale : $values) : ''; |
|
45 | + } |
|
46 | + |
|
47 | + return $primaryLocale == 'all' ? $values : $values->$primaryLocale; |
|
48 | + } |
|
49 | 49 | } |
@@ -6,223 +6,223 @@ |
||
6 | 6 | |
7 | 7 | abstract class BaseService implements BaseServiceInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * @var object |
|
11 | - */ |
|
12 | - public $repo; |
|
9 | + /** |
|
10 | + * @var object |
|
11 | + */ |
|
12 | + public $repo; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Init new object. |
|
16 | - * |
|
17 | - * @param mixed $repo |
|
18 | - * @return void |
|
19 | - */ |
|
20 | - public function __construct($repo) |
|
21 | - { |
|
22 | - $this->repo = $repo; |
|
23 | - } |
|
14 | + /** |
|
15 | + * Init new object. |
|
16 | + * |
|
17 | + * @param mixed $repo |
|
18 | + * @return void |
|
19 | + */ |
|
20 | + public function __construct($repo) |
|
21 | + { |
|
22 | + $this->repo = $repo; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Fetch records with relations based on the given params. |
|
27 | - * |
|
28 | - * @param array $relations |
|
29 | - * @param array $conditions |
|
30 | - * @param integer $perPage |
|
31 | - * @param string $sortBy |
|
32 | - * @param boolean $desc |
|
33 | - * @param boolean $trashed |
|
34 | - * @return collection |
|
35 | - */ |
|
36 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
37 | - { |
|
38 | - $translatable = $this->repo->model->translatable ?? []; |
|
39 | - $filters = $this->constructFilters($conditions); |
|
40 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
25 | + /** |
|
26 | + * Fetch records with relations based on the given params. |
|
27 | + * |
|
28 | + * @param array $relations |
|
29 | + * @param array $conditions |
|
30 | + * @param integer $perPage |
|
31 | + * @param string $sortBy |
|
32 | + * @param boolean $desc |
|
33 | + * @param boolean $trashed |
|
34 | + * @return collection |
|
35 | + */ |
|
36 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
37 | + { |
|
38 | + $translatable = $this->repo->model->translatable ?? []; |
|
39 | + $filters = $this->constructFilters($conditions); |
|
40 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
41 | 41 | |
42 | - if ($trashed) { |
|
43 | - return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
44 | - } |
|
42 | + if ($trashed) { |
|
43 | + return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
44 | + } |
|
45 | 45 | |
46 | - if (count($filters)) { |
|
47 | - return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
48 | - } |
|
46 | + if (count($filters)) { |
|
47 | + return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
48 | + } |
|
49 | 49 | |
50 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
51 | - } |
|
50 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Fetch all records with relations from the storage. |
|
55 | - * |
|
56 | - * @param array $relations |
|
57 | - * @param string $sortBy |
|
58 | - * @param boolean $desc |
|
59 | - * @param array $columns |
|
60 | - * @return collection |
|
61 | - */ |
|
62 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
63 | - { |
|
64 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
65 | - } |
|
53 | + /** |
|
54 | + * Fetch all records with relations from the storage. |
|
55 | + * |
|
56 | + * @param array $relations |
|
57 | + * @param string $sortBy |
|
58 | + * @param boolean $desc |
|
59 | + * @param array $columns |
|
60 | + * @return collection |
|
61 | + */ |
|
62 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
63 | + { |
|
64 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Fetch all records with relations from storage in pages. |
|
69 | - * |
|
70 | - * @param integer $perPage |
|
71 | - * @param array $relations |
|
72 | - * @param string $sortBy |
|
73 | - * @param boolean $desc |
|
74 | - * @param array $columns |
|
75 | - * @return collection |
|
76 | - */ |
|
77 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
78 | - { |
|
79 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
80 | - } |
|
67 | + /** |
|
68 | + * Fetch all records with relations from storage in pages. |
|
69 | + * |
|
70 | + * @param integer $perPage |
|
71 | + * @param array $relations |
|
72 | + * @param string $sortBy |
|
73 | + * @param boolean $desc |
|
74 | + * @param array $columns |
|
75 | + * @return collection |
|
76 | + */ |
|
77 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
78 | + { |
|
79 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Fetch all records with relations based on |
|
84 | - * the given condition from storage in pages. |
|
85 | - * |
|
86 | - * @param array $conditions array of conditions |
|
87 | - * @param integer $perPage |
|
88 | - * @param array $relations |
|
89 | - * @param string $sortBy |
|
90 | - * @param boolean $desc |
|
91 | - * @param array $columns |
|
92 | - * @return collection |
|
93 | - */ |
|
94 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
95 | - { |
|
96 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
97 | - } |
|
82 | + /** |
|
83 | + * Fetch all records with relations based on |
|
84 | + * the given condition from storage in pages. |
|
85 | + * |
|
86 | + * @param array $conditions array of conditions |
|
87 | + * @param integer $perPage |
|
88 | + * @param array $relations |
|
89 | + * @param string $sortBy |
|
90 | + * @param boolean $desc |
|
91 | + * @param array $columns |
|
92 | + * @return collection |
|
93 | + */ |
|
94 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
95 | + { |
|
96 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Save the given model to the storage. |
|
101 | - * |
|
102 | - * @param array $data |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function save(array $data) |
|
106 | - { |
|
107 | - return $this->repo->save($data); |
|
108 | - } |
|
99 | + /** |
|
100 | + * Save the given model to the storage. |
|
101 | + * |
|
102 | + * @param array $data |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function save(array $data) |
|
106 | + { |
|
107 | + return $this->repo->save($data); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Delete record from the storage based on the given |
|
112 | - * condition. |
|
113 | - * |
|
114 | - * @param var $value condition value |
|
115 | - * @param string $attribute condition column name |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - public function delete($value, $attribute = 'id') |
|
119 | - { |
|
120 | - return $this->repo->delete($value, $attribute); |
|
121 | - } |
|
110 | + /** |
|
111 | + * Delete record from the storage based on the given |
|
112 | + * condition. |
|
113 | + * |
|
114 | + * @param var $value condition value |
|
115 | + * @param string $attribute condition column name |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + public function delete($value, $attribute = 'id') |
|
119 | + { |
|
120 | + return $this->repo->delete($value, $attribute); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Fetch records from the storage based on the given |
|
125 | - * id. |
|
126 | - * |
|
127 | - * @param integer $id |
|
128 | - * @param array $relations |
|
129 | - * @param array $columns |
|
130 | - * @return object |
|
131 | - */ |
|
132 | - public function find($id, $relations = [], $columns = ['*']) |
|
133 | - { |
|
134 | - return $this->repo->find($id, $relations, $columns); |
|
135 | - } |
|
123 | + /** |
|
124 | + * Fetch records from the storage based on the given |
|
125 | + * id. |
|
126 | + * |
|
127 | + * @param integer $id |
|
128 | + * @param array $relations |
|
129 | + * @param array $columns |
|
130 | + * @return object |
|
131 | + */ |
|
132 | + public function find($id, $relations = [], $columns = ['*']) |
|
133 | + { |
|
134 | + return $this->repo->find($id, $relations, $columns); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Fetch records from the storage based on the given |
|
139 | - * condition. |
|
140 | - * |
|
141 | - * @param array $conditions array of conditions |
|
142 | - * @param array $relations |
|
143 | - * @param string $sortBy |
|
144 | - * @param boolean $desc |
|
145 | - * @param array $columns |
|
146 | - * @return collection |
|
147 | - */ |
|
148 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
149 | - { |
|
150 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
151 | - } |
|
137 | + /** |
|
138 | + * Fetch records from the storage based on the given |
|
139 | + * condition. |
|
140 | + * |
|
141 | + * @param array $conditions array of conditions |
|
142 | + * @param array $relations |
|
143 | + * @param string $sortBy |
|
144 | + * @param boolean $desc |
|
145 | + * @param array $columns |
|
146 | + * @return collection |
|
147 | + */ |
|
148 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
149 | + { |
|
150 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Fetch the first record from the storage based on the given |
|
155 | - * condition. |
|
156 | - * |
|
157 | - * @param array $conditions array of conditions |
|
158 | - * @param array $relations |
|
159 | - * @param array $columns |
|
160 | - * @return object |
|
161 | - */ |
|
162 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
163 | - { |
|
164 | - return $this->repo->first($conditions, $relations, $columns); |
|
165 | - } |
|
153 | + /** |
|
154 | + * Fetch the first record from the storage based on the given |
|
155 | + * condition. |
|
156 | + * |
|
157 | + * @param array $conditions array of conditions |
|
158 | + * @param array $relations |
|
159 | + * @param array $columns |
|
160 | + * @return object |
|
161 | + */ |
|
162 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
163 | + { |
|
164 | + return $this->repo->first($conditions, $relations, $columns); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Return the deleted models in pages based on the given conditions. |
|
169 | - * |
|
170 | - * @param array $conditions array of conditions |
|
171 | - * @param integer $perPage |
|
172 | - * @param string $sortBy |
|
173 | - * @param boolean $desc |
|
174 | - * @param array $columns |
|
175 | - * @return collection |
|
176 | - */ |
|
177 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
178 | - { |
|
179 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
180 | - } |
|
167 | + /** |
|
168 | + * Return the deleted models in pages based on the given conditions. |
|
169 | + * |
|
170 | + * @param array $conditions array of conditions |
|
171 | + * @param integer $perPage |
|
172 | + * @param string $sortBy |
|
173 | + * @param boolean $desc |
|
174 | + * @param array $columns |
|
175 | + * @return collection |
|
176 | + */ |
|
177 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
178 | + { |
|
179 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Restore the deleted model. |
|
184 | - * |
|
185 | - * @param integer $id |
|
186 | - * @return void |
|
187 | - */ |
|
188 | - public function restore($id) |
|
189 | - { |
|
190 | - return $this->repo->restore($id); |
|
191 | - } |
|
182 | + /** |
|
183 | + * Restore the deleted model. |
|
184 | + * |
|
185 | + * @param integer $id |
|
186 | + * @return void |
|
187 | + */ |
|
188 | + public function restore($id) |
|
189 | + { |
|
190 | + return $this->repo->restore($id); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Prepare filters for repo. |
|
195 | - * |
|
196 | - * @param array $conditions |
|
197 | - * @return array |
|
198 | - */ |
|
199 | - public function constructFilters($conditions) |
|
200 | - { |
|
201 | - $filters = []; |
|
202 | - $translatable = $this->repo->model->translatable ?? []; |
|
203 | - foreach ($conditions as $key => $value) { |
|
204 | - if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
205 | - $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
206 | - if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
207 | - $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
208 | - } elseif (! is_array($value) && strpos($key, '_id')) { |
|
209 | - $filters[$key] = [ |
|
210 | - 'op' => 'in', |
|
211 | - 'val' => explode(',', $value) |
|
212 | - ]; |
|
213 | - } elseif (is_array($value)) { |
|
214 | - $filters[$key] = $value; |
|
215 | - } elseif($value) { |
|
216 | - $key = 'LOWER(' . $key . ')'; |
|
217 | - $value = strtolower($value); |
|
218 | - $filters[$key] = [ |
|
219 | - 'op' => 'like', |
|
220 | - 'val' => '%' . $value . '%' |
|
221 | - ]; |
|
222 | - } |
|
223 | - } |
|
224 | - } |
|
193 | + /** |
|
194 | + * Prepare filters for repo. |
|
195 | + * |
|
196 | + * @param array $conditions |
|
197 | + * @return array |
|
198 | + */ |
|
199 | + public function constructFilters($conditions) |
|
200 | + { |
|
201 | + $filters = []; |
|
202 | + $translatable = $this->repo->model->translatable ?? []; |
|
203 | + foreach ($conditions as $key => $value) { |
|
204 | + if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
205 | + $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
206 | + if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
207 | + $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
208 | + } elseif (! is_array($value) && strpos($key, '_id')) { |
|
209 | + $filters[$key] = [ |
|
210 | + 'op' => 'in', |
|
211 | + 'val' => explode(',', $value) |
|
212 | + ]; |
|
213 | + } elseif (is_array($value)) { |
|
214 | + $filters[$key] = $value; |
|
215 | + } elseif($value) { |
|
216 | + $key = 'LOWER(' . $key . ')'; |
|
217 | + $value = strtolower($value); |
|
218 | + $filters[$key] = [ |
|
219 | + 'op' => 'like', |
|
220 | + 'val' => '%' . $value . '%' |
|
221 | + ]; |
|
222 | + } |
|
223 | + } |
|
224 | + } |
|
225 | 225 | |
226 | - return $filters; |
|
227 | - } |
|
226 | + return $filters; |
|
227 | + } |
|
228 | 228 | } |
@@ -7,84 +7,84 @@ |
||
7 | 7 | |
8 | 8 | class CachingDecorator |
9 | 9 | { |
10 | - /** |
|
11 | - * @var String |
|
12 | - */ |
|
13 | - public $service; |
|
10 | + /** |
|
11 | + * @var String |
|
12 | + */ |
|
13 | + public $service; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var Cache |
|
17 | - */ |
|
18 | - protected $cache; |
|
15 | + /** |
|
16 | + * @var Cache |
|
17 | + */ |
|
18 | + protected $cache; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var mixed |
|
22 | - */ |
|
23 | - public $cacheConfig; |
|
20 | + /** |
|
21 | + * @var mixed |
|
22 | + */ |
|
23 | + public $cacheConfig; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - public $cacheTag; |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + public $cacheTag; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Init new object. |
|
32 | - * |
|
33 | - * @param Object $service |
|
34 | - * @param Cache $cache |
|
35 | - * |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function __construct($service, Cache $cache) |
|
39 | - { |
|
40 | - $this->service = $service; |
|
41 | - $this->cache = $cache; |
|
42 | - $serviceClass = explode('\\', get_class($this->service)); |
|
43 | - $serviceName = end($serviceClass); |
|
44 | - $this->cacheTag = lcfirst(substr($serviceName, 0, strpos($serviceName, 'Service'))); |
|
45 | - } |
|
30 | + /** |
|
31 | + * Init new object. |
|
32 | + * |
|
33 | + * @param Object $service |
|
34 | + * @param Cache $cache |
|
35 | + * |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function __construct($service, Cache $cache) |
|
39 | + { |
|
40 | + $this->service = $service; |
|
41 | + $this->cache = $cache; |
|
42 | + $serviceClass = explode('\\', get_class($this->service)); |
|
43 | + $serviceName = end($serviceClass); |
|
44 | + $this->cacheTag = lcfirst(substr($serviceName, 0, strpos($serviceName, 'Service'))); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Handle the cache mechanism for the called method |
|
49 | - * based the configurations. |
|
50 | - * |
|
51 | - * @param string $name the called method name |
|
52 | - * @param array $arguments the method arguments |
|
53 | - * @return object |
|
54 | - */ |
|
55 | - public function __call($name, $arguments) |
|
56 | - { |
|
57 | - $this->setCacheConfig($name); |
|
47 | + /** |
|
48 | + * Handle the cache mechanism for the called method |
|
49 | + * based the configurations. |
|
50 | + * |
|
51 | + * @param string $name the called method name |
|
52 | + * @param array $arguments the method arguments |
|
53 | + * @return object |
|
54 | + */ |
|
55 | + public function __call($name, $arguments) |
|
56 | + { |
|
57 | + $this->setCacheConfig($name); |
|
58 | 58 | |
59 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
60 | - $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
61 | - $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
62 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
63 | - return call_user_func_array([$this->service, $name], $arguments); |
|
64 | - }); |
|
65 | - } elseif ($this->cacheConfig) { |
|
66 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
67 | - return call_user_func_array([$this->service, $name], $arguments); |
|
68 | - } |
|
59 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
60 | + $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
61 | + $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
62 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
63 | + return call_user_func_array([$this->service, $name], $arguments); |
|
64 | + }); |
|
65 | + } elseif ($this->cacheConfig) { |
|
66 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
67 | + return call_user_func_array([$this->service, $name], $arguments); |
|
68 | + } |
|
69 | 69 | |
70 | - return call_user_func_array([$this->service, $name], $arguments); |
|
71 | - } |
|
70 | + return call_user_func_array([$this->service, $name], $arguments); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Set cache config based on the called method. |
|
75 | - * |
|
76 | - * @param string $name |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - private function setCacheConfig($name) |
|
80 | - { |
|
81 | - $cacheConfig = Arr::get(config('core.cache_config'), $this->cacheTag, false); |
|
82 | - $this->cacheConfig = false; |
|
73 | + /** |
|
74 | + * Set cache config based on the called method. |
|
75 | + * |
|
76 | + * @param string $name |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + private function setCacheConfig($name) |
|
80 | + { |
|
81 | + $cacheConfig = Arr::get(config('core.cache_config'), $this->cacheTag, false); |
|
82 | + $this->cacheConfig = false; |
|
83 | 83 | |
84 | - if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
85 | - $this->cacheConfig = 'cache'; |
|
86 | - } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
87 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
88 | - } |
|
89 | - } |
|
84 | + if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
85 | + $this->cacheConfig = 'cache'; |
|
86 | + } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
87 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
88 | + } |
|
89 | + } |
|
90 | 90 | } |
@@ -13,8 +13,8 @@ |
||
13 | 13 | |
14 | 14 | Route::group(['prefix' => 'settings'], function () { |
15 | 15 | |
16 | - Route::get('/', 'SettingController@index'); |
|
17 | - Route::get('{id}', 'SettingController@show'); |
|
18 | - Route::patch('{id}', 'SettingController@update'); |
|
19 | - Route::post('save/many', 'SettingController@saveMany'); |
|
16 | + Route::get('/', 'SettingController@index'); |
|
17 | + Route::get('{id}', 'SettingController@show'); |
|
18 | + Route::patch('{id}', 'SettingController@update'); |
|
19 | + Route::post('save/many', 'SettingController@saveMany'); |
|
20 | 20 | }); |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - /* |
|
5 | + /* |
|
6 | 6 | |-------------------------------------------------------------------------- |
7 | 7 | | Relations Between Models |
8 | 8 | |-------------------------------------------------------------------------- |
@@ -11,43 +11,43 @@ discard block |
||
11 | 11 | | |
12 | 12 | */ |
13 | 13 | |
14 | - 'relations' => [ |
|
15 | - 'user' => [ |
|
16 | - 'index' => [], |
|
17 | - 'show' => [], |
|
18 | - 'account' => [], |
|
19 | - ], |
|
20 | - 'permission' => [ |
|
21 | - 'index' => [], |
|
22 | - 'show' => [], |
|
23 | - ], |
|
24 | - 'role' => [ |
|
25 | - 'index' => [], |
|
26 | - 'show' => [], |
|
27 | - ], |
|
28 | - 'oauthClient' => [ |
|
29 | - 'index' => [], |
|
30 | - 'show' => [], |
|
31 | - ], |
|
32 | - 'notification' => [ |
|
33 | - 'index' => [], |
|
34 | - 'unread' => [], |
|
35 | - ], |
|
36 | - 'pushNotificationDevice' => [ |
|
37 | - 'index' => [], |
|
38 | - 'show' => [], |
|
39 | - ], |
|
40 | - 'report' => [ |
|
41 | - 'index' => [], |
|
42 | - 'show' => [], |
|
43 | - ], |
|
44 | - 'setting' => [ |
|
45 | - 'index' => [], |
|
46 | - 'show' => [], |
|
47 | - ], |
|
48 | - ], |
|
14 | + 'relations' => [ |
|
15 | + 'user' => [ |
|
16 | + 'index' => [], |
|
17 | + 'show' => [], |
|
18 | + 'account' => [], |
|
19 | + ], |
|
20 | + 'permission' => [ |
|
21 | + 'index' => [], |
|
22 | + 'show' => [], |
|
23 | + ], |
|
24 | + 'role' => [ |
|
25 | + 'index' => [], |
|
26 | + 'show' => [], |
|
27 | + ], |
|
28 | + 'oauthClient' => [ |
|
29 | + 'index' => [], |
|
30 | + 'show' => [], |
|
31 | + ], |
|
32 | + 'notification' => [ |
|
33 | + 'index' => [], |
|
34 | + 'unread' => [], |
|
35 | + ], |
|
36 | + 'pushNotificationDevice' => [ |
|
37 | + 'index' => [], |
|
38 | + 'show' => [], |
|
39 | + ], |
|
40 | + 'report' => [ |
|
41 | + 'index' => [], |
|
42 | + 'show' => [], |
|
43 | + ], |
|
44 | + 'setting' => [ |
|
45 | + 'index' => [], |
|
46 | + 'show' => [], |
|
47 | + ], |
|
48 | + ], |
|
49 | 49 | |
50 | - /* |
|
50 | + /* |
|
51 | 51 | |-------------------------------------------------------------------------- |
52 | 52 | | Cache Configurations |
53 | 53 | |-------------------------------------------------------------------------- |
@@ -56,40 +56,40 @@ discard block |
||
56 | 56 | | |
57 | 57 | */ |
58 | 58 | |
59 | - 'cache_config' => [ |
|
60 | - 'oauthClient' => [ |
|
61 | - 'cache' => [ |
|
62 | - 'list', |
|
63 | - 'find', |
|
64 | - 'findBy', |
|
65 | - 'paginate', |
|
66 | - 'paginateBy', |
|
67 | - 'first', |
|
68 | - 'deleted' |
|
69 | - ], |
|
70 | - 'clear' => [ |
|
71 | - 'save' => ['oauthClient'], |
|
72 | - 'delete' => ['oauthClient'], |
|
73 | - 'restore' => ['oauthClient'], |
|
74 | - 'revoke' => ['oauthClient'], |
|
75 | - 'ubRevoke' => ['oauthClient'], |
|
76 | - 'regenerateSecret' => ['oauthClient'], |
|
77 | - ], |
|
78 | - ], |
|
79 | - 'setting' => [ |
|
80 | - 'cache' => [ |
|
81 | - 'list', |
|
82 | - 'find', |
|
83 | - 'findBy', |
|
84 | - 'paginate', |
|
85 | - 'paginateBy', |
|
86 | - 'first', |
|
87 | - 'deleted' |
|
88 | - ], |
|
89 | - 'clear' => [ |
|
90 | - 'save' => ['setting'], |
|
91 | - 'saveMany' => ['setting'], |
|
92 | - ] |
|
93 | - ] |
|
94 | - ] |
|
59 | + 'cache_config' => [ |
|
60 | + 'oauthClient' => [ |
|
61 | + 'cache' => [ |
|
62 | + 'list', |
|
63 | + 'find', |
|
64 | + 'findBy', |
|
65 | + 'paginate', |
|
66 | + 'paginateBy', |
|
67 | + 'first', |
|
68 | + 'deleted' |
|
69 | + ], |
|
70 | + 'clear' => [ |
|
71 | + 'save' => ['oauthClient'], |
|
72 | + 'delete' => ['oauthClient'], |
|
73 | + 'restore' => ['oauthClient'], |
|
74 | + 'revoke' => ['oauthClient'], |
|
75 | + 'ubRevoke' => ['oauthClient'], |
|
76 | + 'regenerateSecret' => ['oauthClient'], |
|
77 | + ], |
|
78 | + ], |
|
79 | + 'setting' => [ |
|
80 | + 'cache' => [ |
|
81 | + 'list', |
|
82 | + 'find', |
|
83 | + 'findBy', |
|
84 | + 'paginate', |
|
85 | + 'paginateBy', |
|
86 | + 'first', |
|
87 | + 'deleted' |
|
88 | + ], |
|
89 | + 'clear' => [ |
|
90 | + 'save' => ['setting'], |
|
91 | + 'saveMany' => ['setting'], |
|
92 | + ] |
|
93 | + ] |
|
94 | + ] |
|
95 | 95 | ]; |
@@ -5,14 +5,14 @@ |
||
5 | 5 | |
6 | 6 | class DummyRepository extends BaseRepository |
7 | 7 | { |
8 | - /** |
|
9 | - * Init new object. |
|
10 | - * |
|
11 | - * @param DummyModel $model |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function __construct(DummyModel $model) |
|
15 | - { |
|
16 | - parent::__construct($model); |
|
17 | - } |
|
8 | + /** |
|
9 | + * Init new object. |
|
10 | + * |
|
11 | + * @param DummyModel $model |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function __construct(DummyModel $model) |
|
15 | + { |
|
16 | + parent::__construct($model); |
|
17 | + } |
|
18 | 18 | } |
@@ -8,577 +8,577 @@ |
||
8 | 8 | |
9 | 9 | abstract class BaseRepository implements BaseRepositoryInterface |
10 | 10 | { |
11 | - /** |
|
12 | - * @var object |
|
13 | - */ |
|
14 | - public $model; |
|
11 | + /** |
|
12 | + * @var object |
|
13 | + */ |
|
14 | + public $model; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Init new object. |
|
18 | - * |
|
19 | - * @var mixed model |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public function __construct($model) |
|
23 | - { |
|
24 | - $this->model = $model; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Fetch all records with relations from the storage. |
|
29 | - * |
|
30 | - * @param array $relations |
|
31 | - * @param string $sortBy |
|
32 | - * @param boolean $desc |
|
33 | - * @param array $columns |
|
34 | - * @return collection |
|
35 | - */ |
|
36 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37 | - { |
|
38 | - $sort = $desc ? 'desc' : 'asc'; |
|
39 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40 | - } |
|
16 | + /** |
|
17 | + * Init new object. |
|
18 | + * |
|
19 | + * @var mixed model |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public function __construct($model) |
|
23 | + { |
|
24 | + $this->model = $model; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Fetch all records with relations from the storage. |
|
29 | + * |
|
30 | + * @param array $relations |
|
31 | + * @param string $sortBy |
|
32 | + * @param boolean $desc |
|
33 | + * @param array $columns |
|
34 | + * @return collection |
|
35 | + */ |
|
36 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37 | + { |
|
38 | + $sort = $desc ? 'desc' : 'asc'; |
|
39 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Fetch all records with relations from storage in pages. |
|
44 | - * |
|
45 | - * @param integer $perPage |
|
46 | - * @param array $relations |
|
47 | - * @param string $sortBy |
|
48 | - * @param boolean $desc |
|
49 | - * @param array $columns |
|
50 | - * @return collection |
|
51 | - */ |
|
52 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
53 | - { |
|
54 | - $sort = $desc ? 'desc' : 'asc'; |
|
55 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Fetch all records with relations based on |
|
60 | - * the given condition from storage in pages. |
|
61 | - * |
|
62 | - * @param array $conditions array of conditions |
|
63 | - * @param integer $perPage |
|
64 | - * @param array $relations |
|
65 | - * @param string $sortBy |
|
66 | - * @param boolean $desc |
|
67 | - * @param array $columns |
|
68 | - * @return collection |
|
69 | - */ |
|
70 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
71 | - { |
|
72 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
73 | - $sort = $desc ? 'desc' : 'asc'; |
|
74 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Count all records based on the given condition from storage. |
|
79 | - * |
|
80 | - * @param array $conditions array of conditions |
|
81 | - * @return collection |
|
82 | - */ |
|
83 | - public function count($conditions) |
|
84 | - { |
|
85 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
86 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
87 | - } |
|
42 | + /** |
|
43 | + * Fetch all records with relations from storage in pages. |
|
44 | + * |
|
45 | + * @param integer $perPage |
|
46 | + * @param array $relations |
|
47 | + * @param string $sortBy |
|
48 | + * @param boolean $desc |
|
49 | + * @param array $columns |
|
50 | + * @return collection |
|
51 | + */ |
|
52 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
53 | + { |
|
54 | + $sort = $desc ? 'desc' : 'asc'; |
|
55 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Fetch all records with relations based on |
|
60 | + * the given condition from storage in pages. |
|
61 | + * |
|
62 | + * @param array $conditions array of conditions |
|
63 | + * @param integer $perPage |
|
64 | + * @param array $relations |
|
65 | + * @param string $sortBy |
|
66 | + * @param boolean $desc |
|
67 | + * @param array $columns |
|
68 | + * @return collection |
|
69 | + */ |
|
70 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
71 | + { |
|
72 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
73 | + $sort = $desc ? 'desc' : 'asc'; |
|
74 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Count all records based on the given condition from storage. |
|
79 | + * |
|
80 | + * @param array $conditions array of conditions |
|
81 | + * @return collection |
|
82 | + */ |
|
83 | + public function count($conditions) |
|
84 | + { |
|
85 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
86 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Save the given model to the storage. |
|
91 | - * |
|
92 | - * @param array $data |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - public function save(array $data) |
|
96 | - { |
|
97 | - $local = \Session::get('locale'); |
|
98 | - \Session::put('locale', 'all'); |
|
99 | - $model = false; |
|
100 | - $relations = []; |
|
101 | - |
|
102 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
89 | + /** |
|
90 | + * Save the given model to the storage. |
|
91 | + * |
|
92 | + * @param array $data |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + public function save(array $data) |
|
96 | + { |
|
97 | + $local = \Session::get('locale'); |
|
98 | + \Session::put('locale', 'all'); |
|
99 | + $model = false; |
|
100 | + $relations = []; |
|
101 | + |
|
102 | + \DB::transaction(function () use (&$model, &$relations, $data) { |
|
103 | 103 | |
104 | - $model = $this->prepareModel($data); |
|
105 | - $relations = $this->prepareRelations($data, $model); |
|
106 | - $model = $this->saveModel($model, $relations); |
|
107 | - }); |
|
108 | - \Session::put('locale', $local); |
|
104 | + $model = $this->prepareModel($data); |
|
105 | + $relations = $this->prepareRelations($data, $model); |
|
106 | + $model = $this->saveModel($model, $relations); |
|
107 | + }); |
|
108 | + \Session::put('locale', $local); |
|
109 | 109 | |
110 | - if (count($relations)) { |
|
111 | - $model->load(...array_keys($relations)); |
|
112 | - } |
|
113 | - |
|
114 | - return $model; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Delete record from the storage based on the given |
|
119 | - * condition. |
|
120 | - * |
|
121 | - * @param var $value condition value |
|
122 | - * @param string $attribute condition column name |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function delete($value, $attribute = 'id') |
|
126 | - { |
|
127 | - \DB::transaction(function () use ($value, $attribute) { |
|
128 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
129 | - $model->delete(); |
|
130 | - }); |
|
131 | - }); |
|
132 | - } |
|
110 | + if (count($relations)) { |
|
111 | + $model->load(...array_keys($relations)); |
|
112 | + } |
|
113 | + |
|
114 | + return $model; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Delete record from the storage based on the given |
|
119 | + * condition. |
|
120 | + * |
|
121 | + * @param var $value condition value |
|
122 | + * @param string $attribute condition column name |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function delete($value, $attribute = 'id') |
|
126 | + { |
|
127 | + \DB::transaction(function () use ($value, $attribute) { |
|
128 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
129 | + $model->delete(); |
|
130 | + }); |
|
131 | + }); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * Fetch records from the storage based on the given |
|
136 | - * id. |
|
137 | - * |
|
138 | - * @param integer $id |
|
139 | - * @param string[] $relations |
|
140 | - * @param array $columns |
|
141 | - * @return object |
|
142 | - */ |
|
143 | - public function find($id, $relations = [], $columns = ['*']) |
|
144 | - { |
|
145 | - return $this->model->with($relations)->find($id, $columns); |
|
146 | - } |
|
134 | + /** |
|
135 | + * Fetch records from the storage based on the given |
|
136 | + * id. |
|
137 | + * |
|
138 | + * @param integer $id |
|
139 | + * @param string[] $relations |
|
140 | + * @param array $columns |
|
141 | + * @return object |
|
142 | + */ |
|
143 | + public function find($id, $relations = [], $columns = ['*']) |
|
144 | + { |
|
145 | + return $this->model->with($relations)->find($id, $columns); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Fetch records from the storage based on the given |
|
150 | - * condition. |
|
151 | - * |
|
152 | - * @param array $conditions array of conditions |
|
153 | - * @param array $relations |
|
154 | - * @param string $sortBy |
|
155 | - * @param boolean $desc |
|
156 | - * @param array $columns |
|
157 | - * @return collection |
|
158 | - */ |
|
159 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
160 | - { |
|
161 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
162 | - $sort = $desc ? 'desc' : 'asc'; |
|
163 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Fetch the first record from the storage based on the given |
|
168 | - * condition. |
|
169 | - * |
|
170 | - * @param array $conditions array of conditions |
|
171 | - * @param array $relations |
|
172 | - * @param array $columns |
|
173 | - * @return object |
|
174 | - */ |
|
175 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
176 | - { |
|
177 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
178 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Return the deleted models in pages based on the given conditions. |
|
183 | - * |
|
184 | - * @param array $conditions array of conditions |
|
185 | - * @param integer $perPage |
|
186 | - * @param string $sortBy |
|
187 | - * @param boolean $desc |
|
188 | - * @param array $columns |
|
189 | - * @return collection |
|
190 | - */ |
|
191 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
192 | - { |
|
193 | - unset($conditions['page']); |
|
194 | - unset($conditions['perPage']); |
|
195 | - unset($conditions['sortBy']); |
|
196 | - unset($conditions['sort']); |
|
197 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
198 | - $sort = $desc ? 'desc' : 'asc'; |
|
199 | - $model = $this->model->onlyTrashed(); |
|
200 | - |
|
201 | - if (count($conditions['conditionValues'])) { |
|
202 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
203 | - } |
|
204 | - |
|
205 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Restore the deleted model. |
|
210 | - * |
|
211 | - * @param integer $id |
|
212 | - * @return void |
|
213 | - */ |
|
214 | - public function restore($id) |
|
215 | - { |
|
216 | - $model = $this->model->onlyTrashed()->find($id); |
|
217 | - |
|
218 | - if (! $model) { |
|
219 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
220 | - } |
|
221 | - |
|
222 | - $model->restore(); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Fill the model with the given data. |
|
227 | - * |
|
228 | - * @param array $data |
|
229 | - * |
|
230 | - * @return object |
|
231 | - */ |
|
232 | - public function prepareModel($data) |
|
233 | - { |
|
234 | - $modelClass = $this->model; |
|
235 | - |
|
236 | - /** |
|
237 | - * If the id is present in the data then select the model for updating, |
|
238 | - * else create new model. |
|
239 | - * @var array |
|
240 | - */ |
|
241 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
242 | - if (! $model) { |
|
243 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Construct the model object with the given data, |
|
248 | - * and if there is a relation add it to relations array, |
|
249 | - * then save the model. |
|
250 | - */ |
|
251 | - foreach ($data as $key => $value) { |
|
252 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
253 | - /** |
|
254 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
255 | - */ |
|
256 | - $model->$key = $value; |
|
257 | - } |
|
258 | - } |
|
259 | - |
|
260 | - return $model; |
|
261 | - } |
|
148 | + /** |
|
149 | + * Fetch records from the storage based on the given |
|
150 | + * condition. |
|
151 | + * |
|
152 | + * @param array $conditions array of conditions |
|
153 | + * @param array $relations |
|
154 | + * @param string $sortBy |
|
155 | + * @param boolean $desc |
|
156 | + * @param array $columns |
|
157 | + * @return collection |
|
158 | + */ |
|
159 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
160 | + { |
|
161 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
162 | + $sort = $desc ? 'desc' : 'asc'; |
|
163 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Fetch the first record from the storage based on the given |
|
168 | + * condition. |
|
169 | + * |
|
170 | + * @param array $conditions array of conditions |
|
171 | + * @param array $relations |
|
172 | + * @param array $columns |
|
173 | + * @return object |
|
174 | + */ |
|
175 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
176 | + { |
|
177 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
178 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Return the deleted models in pages based on the given conditions. |
|
183 | + * |
|
184 | + * @param array $conditions array of conditions |
|
185 | + * @param integer $perPage |
|
186 | + * @param string $sortBy |
|
187 | + * @param boolean $desc |
|
188 | + * @param array $columns |
|
189 | + * @return collection |
|
190 | + */ |
|
191 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
192 | + { |
|
193 | + unset($conditions['page']); |
|
194 | + unset($conditions['perPage']); |
|
195 | + unset($conditions['sortBy']); |
|
196 | + unset($conditions['sort']); |
|
197 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
198 | + $sort = $desc ? 'desc' : 'asc'; |
|
199 | + $model = $this->model->onlyTrashed(); |
|
200 | + |
|
201 | + if (count($conditions['conditionValues'])) { |
|
202 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
203 | + } |
|
204 | + |
|
205 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Restore the deleted model. |
|
210 | + * |
|
211 | + * @param integer $id |
|
212 | + * @return void |
|
213 | + */ |
|
214 | + public function restore($id) |
|
215 | + { |
|
216 | + $model = $this->model->onlyTrashed()->find($id); |
|
217 | + |
|
218 | + if (! $model) { |
|
219 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
220 | + } |
|
221 | + |
|
222 | + $model->restore(); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Fill the model with the given data. |
|
227 | + * |
|
228 | + * @param array $data |
|
229 | + * |
|
230 | + * @return object |
|
231 | + */ |
|
232 | + public function prepareModel($data) |
|
233 | + { |
|
234 | + $modelClass = $this->model; |
|
235 | + |
|
236 | + /** |
|
237 | + * If the id is present in the data then select the model for updating, |
|
238 | + * else create new model. |
|
239 | + * @var array |
|
240 | + */ |
|
241 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
242 | + if (! $model) { |
|
243 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Construct the model object with the given data, |
|
248 | + * and if there is a relation add it to relations array, |
|
249 | + * then save the model. |
|
250 | + */ |
|
251 | + foreach ($data as $key => $value) { |
|
252 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
253 | + /** |
|
254 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
255 | + */ |
|
256 | + $model->$key = $value; |
|
257 | + } |
|
258 | + } |
|
259 | + |
|
260 | + return $model; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * Prepare related models based on the given data for the given model. |
|
265 | - * |
|
266 | - * @param array $data |
|
267 | - * @param object $model |
|
268 | - * |
|
269 | - * @return array |
|
270 | - */ |
|
271 | - public function prepareRelations($data, $model) |
|
272 | - { |
|
273 | - /** |
|
274 | - * Init the relation array |
|
275 | - * |
|
276 | - * @var array |
|
277 | - */ |
|
278 | - $relations = []; |
|
279 | - |
|
280 | - /** |
|
281 | - * Construct the model object with the given data, |
|
282 | - * and if there is a relation add it to relations array, |
|
283 | - * then save the model. |
|
284 | - */ |
|
285 | - foreach ($data as $key => $value) { |
|
286 | - /** |
|
287 | - * If the attribute is a relation. |
|
288 | - */ |
|
289 | - $relation = \Str::camel($key); |
|
290 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
291 | - /** |
|
292 | - * Check if the relation is a collection. |
|
293 | - */ |
|
294 | - if (class_basename($model->$relation) == 'Collection') { |
|
295 | - /** |
|
296 | - * If the relation has no value then marke the relation data |
|
297 | - * related to the model to be deleted. |
|
298 | - */ |
|
299 | - if (! $value || ! count($value)) { |
|
300 | - $relations[$relation] = 'delete'; |
|
301 | - } |
|
302 | - } |
|
303 | - if (is_array($value)) { |
|
304 | - /** |
|
305 | - * Loop through the relation data. |
|
306 | - */ |
|
307 | - foreach ($value as $attr => $val) { |
|
308 | - /** |
|
309 | - * Get the relation model. |
|
310 | - */ |
|
311 | - $relationBaseModel = \Core::$relation()->model; |
|
312 | - |
|
313 | - /** |
|
314 | - * Check if the relation is a collection. |
|
315 | - */ |
|
316 | - if (class_basename($model->$relation) == 'Collection') { |
|
317 | - if (! is_array($val)) { |
|
318 | - $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
319 | - } else { |
|
320 | - /** |
|
321 | - * If the id is present in the data then select the relation model for updating, |
|
322 | - * else create new model. |
|
323 | - */ |
|
324 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * If model doesn't exists. |
|
329 | - */ |
|
330 | - if (! $relationModel) { |
|
331 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
332 | - } |
|
333 | - |
|
334 | - if (is_array($val)) { |
|
335 | - /** |
|
336 | - * Loop through the relation attributes. |
|
337 | - */ |
|
338 | - foreach ($val as $attr => $val) { |
|
339 | - /** |
|
340 | - * Prevent the sub relations or attributes not in the fillable. |
|
341 | - */ |
|
342 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
343 | - $relationModel->$attr = $val; |
|
344 | - } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
345 | - $extra[$attr] = $val; |
|
346 | - } |
|
347 | - } |
|
348 | - } |
|
349 | - |
|
350 | - if (isset($extra)) { |
|
351 | - $relationModel->extra = $extra; |
|
352 | - } |
|
353 | - $relations[$relation][] = $relationModel; |
|
354 | - } else { |
|
355 | - /** |
|
356 | - * Prevent the sub relations. |
|
357 | - */ |
|
358 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
359 | - /** |
|
360 | - * If the id is present in the data then select the relation model for updating, |
|
361 | - * else create new model. |
|
362 | - */ |
|
363 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
364 | - |
|
365 | - /** |
|
366 | - * If model doesn't exists. |
|
367 | - */ |
|
368 | - if (! $relationModel) { |
|
369 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
370 | - } |
|
371 | - |
|
372 | - foreach ($value as $relationAttribute => $relationValue) { |
|
373 | - /** |
|
374 | - * Prevent attributes not in the fillable. |
|
375 | - */ |
|
376 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
377 | - $relationModel->$relationAttribute = $relationValue; |
|
378 | - } |
|
379 | - } |
|
380 | - |
|
381 | - $relations[$relation] = $relationModel; |
|
382 | - } |
|
383 | - } |
|
384 | - } |
|
385 | - } |
|
386 | - } |
|
387 | - } |
|
388 | - |
|
389 | - return $relations; |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Save the model with related models. |
|
394 | - * |
|
395 | - * @param object $model |
|
396 | - * @param array $relations |
|
397 | - * |
|
398 | - * @return object |
|
399 | - */ |
|
400 | - public function saveModel($model, $relations) |
|
401 | - { |
|
402 | - |
|
403 | - /** |
|
404 | - * Loop through the relations array. |
|
405 | - */ |
|
406 | - foreach ($relations as $key => $value) { |
|
407 | - /** |
|
408 | - * If the relation is marked for delete then delete it. |
|
409 | - */ |
|
410 | - if ($value == 'delete' && $model->$key()->count()) { |
|
411 | - $model->$key()->delete(); |
|
412 | - } elseif (gettype($value) == 'array') { |
|
413 | - /** |
|
414 | - * Save the model. |
|
415 | - */ |
|
416 | - $model->save(); |
|
417 | - $ids = []; |
|
418 | - |
|
419 | - /** |
|
420 | - * Loop through the relations. |
|
421 | - */ |
|
422 | - foreach ($value as $val) { |
|
423 | - switch (class_basename($model->$key())) { |
|
424 | - /** |
|
425 | - * If the relation is one to many then update it's foreign key with |
|
426 | - * the model id and save it then add its id to ids array to delete all |
|
427 | - * relations who's id isn't in the ids array. |
|
428 | - */ |
|
429 | - case 'HasMany': |
|
430 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
431 | - $val->$foreignKeyName = $model->id; |
|
432 | - $val->save(); |
|
433 | - $ids[] = $val->id; |
|
434 | - break; |
|
435 | - |
|
436 | - /** |
|
437 | - * If the relation is many to many then add it's id to the ids array to |
|
438 | - * attache these ids to the model. |
|
439 | - */ |
|
440 | - case 'BelongsToMany': |
|
441 | - $extra = $val->extra; |
|
442 | - unset($val->extra); |
|
443 | - $val->save(); |
|
444 | - $ids[$val->id] = $extra ?? []; |
|
445 | - break; |
|
446 | - } |
|
447 | - } |
|
448 | - switch (class_basename($model->$key())) { |
|
449 | - /** |
|
450 | - * If the relation is one to many then delete all |
|
451 | - * relations who's id isn't in the ids array. |
|
452 | - */ |
|
453 | - case 'HasMany': |
|
454 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
455 | - break; |
|
456 | - |
|
457 | - /** |
|
458 | - * If the relation is many to many then |
|
459 | - * detach the previous data and attach |
|
460 | - * the ids array to the model. |
|
461 | - */ |
|
462 | - case 'BelongsToMany': |
|
463 | - $model->$key()->detach(); |
|
464 | - $model->$key()->attach($ids); |
|
465 | - break; |
|
466 | - } |
|
467 | - } else { |
|
468 | - switch (class_basename($model->$key())) { |
|
469 | - /** |
|
470 | - * If the relation is one to one. |
|
471 | - */ |
|
472 | - case 'HasOne': |
|
473 | - /** |
|
474 | - * Save the model. |
|
475 | - */ |
|
476 | - $model->save(); |
|
477 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
478 | - $value->$foreignKeyName = $model->id; |
|
479 | - $value->save(); |
|
480 | - break; |
|
481 | - case 'BelongsTo': |
|
482 | - /** |
|
483 | - * Save the model. |
|
484 | - */ |
|
485 | - $value->save(); |
|
486 | - $model->$key()->associate($value); |
|
487 | - break; |
|
488 | - } |
|
489 | - } |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Save the model. |
|
494 | - */ |
|
495 | - $model->save(); |
|
496 | - |
|
497 | - return $model; |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * Build the conditions recursively for the retrieving methods. |
|
502 | - * @param array $conditions |
|
503 | - * @return array |
|
504 | - */ |
|
505 | - protected function constructConditions($conditions, $model) |
|
506 | - { |
|
507 | - $conditionString = ''; |
|
508 | - $conditionValues = []; |
|
509 | - foreach ($conditions as $key => $value) { |
|
510 | - if (Str::contains($key, '->')) { |
|
511 | - $key = $this->wrapJsonSelector($key); |
|
512 | - } |
|
513 | - |
|
514 | - if ($key == 'and') { |
|
515 | - $conditions = $this->constructConditions($value, $model); |
|
516 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
517 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
518 | - } elseif ($key == 'or') { |
|
519 | - $conditions = $this->constructConditions($value, $model); |
|
520 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
521 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
522 | - } else { |
|
523 | - if (is_array($value)) { |
|
524 | - $operator = $value['op']; |
|
525 | - if (strtolower($operator) == 'between') { |
|
526 | - $value1 = $value['val1']; |
|
527 | - $value2 = $value['val2']; |
|
528 | - } else { |
|
529 | - $value = Arr::get($value, 'val', ''); |
|
530 | - } |
|
531 | - } else { |
|
532 | - $operator = '='; |
|
533 | - } |
|
263 | + /** |
|
264 | + * Prepare related models based on the given data for the given model. |
|
265 | + * |
|
266 | + * @param array $data |
|
267 | + * @param object $model |
|
268 | + * |
|
269 | + * @return array |
|
270 | + */ |
|
271 | + public function prepareRelations($data, $model) |
|
272 | + { |
|
273 | + /** |
|
274 | + * Init the relation array |
|
275 | + * |
|
276 | + * @var array |
|
277 | + */ |
|
278 | + $relations = []; |
|
279 | + |
|
280 | + /** |
|
281 | + * Construct the model object with the given data, |
|
282 | + * and if there is a relation add it to relations array, |
|
283 | + * then save the model. |
|
284 | + */ |
|
285 | + foreach ($data as $key => $value) { |
|
286 | + /** |
|
287 | + * If the attribute is a relation. |
|
288 | + */ |
|
289 | + $relation = \Str::camel($key); |
|
290 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
291 | + /** |
|
292 | + * Check if the relation is a collection. |
|
293 | + */ |
|
294 | + if (class_basename($model->$relation) == 'Collection') { |
|
295 | + /** |
|
296 | + * If the relation has no value then marke the relation data |
|
297 | + * related to the model to be deleted. |
|
298 | + */ |
|
299 | + if (! $value || ! count($value)) { |
|
300 | + $relations[$relation] = 'delete'; |
|
301 | + } |
|
302 | + } |
|
303 | + if (is_array($value)) { |
|
304 | + /** |
|
305 | + * Loop through the relation data. |
|
306 | + */ |
|
307 | + foreach ($value as $attr => $val) { |
|
308 | + /** |
|
309 | + * Get the relation model. |
|
310 | + */ |
|
311 | + $relationBaseModel = \Core::$relation()->model; |
|
312 | + |
|
313 | + /** |
|
314 | + * Check if the relation is a collection. |
|
315 | + */ |
|
316 | + if (class_basename($model->$relation) == 'Collection') { |
|
317 | + if (! is_array($val)) { |
|
318 | + $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
319 | + } else { |
|
320 | + /** |
|
321 | + * If the id is present in the data then select the relation model for updating, |
|
322 | + * else create new model. |
|
323 | + */ |
|
324 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * If model doesn't exists. |
|
329 | + */ |
|
330 | + if (! $relationModel) { |
|
331 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
332 | + } |
|
333 | + |
|
334 | + if (is_array($val)) { |
|
335 | + /** |
|
336 | + * Loop through the relation attributes. |
|
337 | + */ |
|
338 | + foreach ($val as $attr => $val) { |
|
339 | + /** |
|
340 | + * Prevent the sub relations or attributes not in the fillable. |
|
341 | + */ |
|
342 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
343 | + $relationModel->$attr = $val; |
|
344 | + } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
345 | + $extra[$attr] = $val; |
|
346 | + } |
|
347 | + } |
|
348 | + } |
|
349 | + |
|
350 | + if (isset($extra)) { |
|
351 | + $relationModel->extra = $extra; |
|
352 | + } |
|
353 | + $relations[$relation][] = $relationModel; |
|
354 | + } else { |
|
355 | + /** |
|
356 | + * Prevent the sub relations. |
|
357 | + */ |
|
358 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
359 | + /** |
|
360 | + * If the id is present in the data then select the relation model for updating, |
|
361 | + * else create new model. |
|
362 | + */ |
|
363 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
364 | + |
|
365 | + /** |
|
366 | + * If model doesn't exists. |
|
367 | + */ |
|
368 | + if (! $relationModel) { |
|
369 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
370 | + } |
|
371 | + |
|
372 | + foreach ($value as $relationAttribute => $relationValue) { |
|
373 | + /** |
|
374 | + * Prevent attributes not in the fillable. |
|
375 | + */ |
|
376 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
377 | + $relationModel->$relationAttribute = $relationValue; |
|
378 | + } |
|
379 | + } |
|
380 | + |
|
381 | + $relations[$relation] = $relationModel; |
|
382 | + } |
|
383 | + } |
|
384 | + } |
|
385 | + } |
|
386 | + } |
|
387 | + } |
|
388 | + |
|
389 | + return $relations; |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Save the model with related models. |
|
394 | + * |
|
395 | + * @param object $model |
|
396 | + * @param array $relations |
|
397 | + * |
|
398 | + * @return object |
|
399 | + */ |
|
400 | + public function saveModel($model, $relations) |
|
401 | + { |
|
402 | + |
|
403 | + /** |
|
404 | + * Loop through the relations array. |
|
405 | + */ |
|
406 | + foreach ($relations as $key => $value) { |
|
407 | + /** |
|
408 | + * If the relation is marked for delete then delete it. |
|
409 | + */ |
|
410 | + if ($value == 'delete' && $model->$key()->count()) { |
|
411 | + $model->$key()->delete(); |
|
412 | + } elseif (gettype($value) == 'array') { |
|
413 | + /** |
|
414 | + * Save the model. |
|
415 | + */ |
|
416 | + $model->save(); |
|
417 | + $ids = []; |
|
418 | + |
|
419 | + /** |
|
420 | + * Loop through the relations. |
|
421 | + */ |
|
422 | + foreach ($value as $val) { |
|
423 | + switch (class_basename($model->$key())) { |
|
424 | + /** |
|
425 | + * If the relation is one to many then update it's foreign key with |
|
426 | + * the model id and save it then add its id to ids array to delete all |
|
427 | + * relations who's id isn't in the ids array. |
|
428 | + */ |
|
429 | + case 'HasMany': |
|
430 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
431 | + $val->$foreignKeyName = $model->id; |
|
432 | + $val->save(); |
|
433 | + $ids[] = $val->id; |
|
434 | + break; |
|
435 | + |
|
436 | + /** |
|
437 | + * If the relation is many to many then add it's id to the ids array to |
|
438 | + * attache these ids to the model. |
|
439 | + */ |
|
440 | + case 'BelongsToMany': |
|
441 | + $extra = $val->extra; |
|
442 | + unset($val->extra); |
|
443 | + $val->save(); |
|
444 | + $ids[$val->id] = $extra ?? []; |
|
445 | + break; |
|
446 | + } |
|
447 | + } |
|
448 | + switch (class_basename($model->$key())) { |
|
449 | + /** |
|
450 | + * If the relation is one to many then delete all |
|
451 | + * relations who's id isn't in the ids array. |
|
452 | + */ |
|
453 | + case 'HasMany': |
|
454 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
455 | + break; |
|
456 | + |
|
457 | + /** |
|
458 | + * If the relation is many to many then |
|
459 | + * detach the previous data and attach |
|
460 | + * the ids array to the model. |
|
461 | + */ |
|
462 | + case 'BelongsToMany': |
|
463 | + $model->$key()->detach(); |
|
464 | + $model->$key()->attach($ids); |
|
465 | + break; |
|
466 | + } |
|
467 | + } else { |
|
468 | + switch (class_basename($model->$key())) { |
|
469 | + /** |
|
470 | + * If the relation is one to one. |
|
471 | + */ |
|
472 | + case 'HasOne': |
|
473 | + /** |
|
474 | + * Save the model. |
|
475 | + */ |
|
476 | + $model->save(); |
|
477 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
478 | + $value->$foreignKeyName = $model->id; |
|
479 | + $value->save(); |
|
480 | + break; |
|
481 | + case 'BelongsTo': |
|
482 | + /** |
|
483 | + * Save the model. |
|
484 | + */ |
|
485 | + $value->save(); |
|
486 | + $model->$key()->associate($value); |
|
487 | + break; |
|
488 | + } |
|
489 | + } |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Save the model. |
|
494 | + */ |
|
495 | + $model->save(); |
|
496 | + |
|
497 | + return $model; |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * Build the conditions recursively for the retrieving methods. |
|
502 | + * @param array $conditions |
|
503 | + * @return array |
|
504 | + */ |
|
505 | + protected function constructConditions($conditions, $model) |
|
506 | + { |
|
507 | + $conditionString = ''; |
|
508 | + $conditionValues = []; |
|
509 | + foreach ($conditions as $key => $value) { |
|
510 | + if (Str::contains($key, '->')) { |
|
511 | + $key = $this->wrapJsonSelector($key); |
|
512 | + } |
|
513 | + |
|
514 | + if ($key == 'and') { |
|
515 | + $conditions = $this->constructConditions($value, $model); |
|
516 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
517 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
518 | + } elseif ($key == 'or') { |
|
519 | + $conditions = $this->constructConditions($value, $model); |
|
520 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
521 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
522 | + } else { |
|
523 | + if (is_array($value)) { |
|
524 | + $operator = $value['op']; |
|
525 | + if (strtolower($operator) == 'between') { |
|
526 | + $value1 = $value['val1']; |
|
527 | + $value2 = $value['val2']; |
|
528 | + } else { |
|
529 | + $value = Arr::get($value, 'val', ''); |
|
530 | + } |
|
531 | + } else { |
|
532 | + $operator = '='; |
|
533 | + } |
|
534 | 534 | |
535 | - if (strtolower($operator) == 'between') { |
|
536 | - $conditionString .= $key.' >= ? and '; |
|
537 | - $conditionValues[] = $value1; |
|
538 | - |
|
539 | - $conditionString .= $key.' <= ? {op} '; |
|
540 | - $conditionValues[] = $value2; |
|
541 | - } elseif (strtolower($operator) == 'in') { |
|
542 | - $conditionValues = array_merge($conditionValues, $value); |
|
543 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
544 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
545 | - } elseif (strtolower($operator) == 'null') { |
|
546 | - $conditionString .= $key.' is null {op} '; |
|
547 | - } elseif (strtolower($operator) == 'not null') { |
|
548 | - $conditionString .= $key.' is not null {op} '; |
|
549 | - } elseif (strtolower($operator) == 'has') { |
|
550 | - $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
551 | - $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
552 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
553 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
554 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
555 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
556 | - } else { |
|
557 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
558 | - $conditionValues[] = $value; |
|
559 | - } |
|
560 | - } |
|
561 | - } |
|
562 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
563 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Wrap the given JSON selector. |
|
568 | - * |
|
569 | - * @param string $value |
|
570 | - * @return string |
|
571 | - */ |
|
572 | - protected function wrapJsonSelector($value) |
|
573 | - { |
|
574 | - $removeLast = strpos($value, ')'); |
|
575 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
576 | - $path = explode('->', $value); |
|
577 | - $field = array_shift($path); |
|
578 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
579 | - return '"'.$part.'"'; |
|
580 | - })->implode('.')); |
|
535 | + if (strtolower($operator) == 'between') { |
|
536 | + $conditionString .= $key.' >= ? and '; |
|
537 | + $conditionValues[] = $value1; |
|
538 | + |
|
539 | + $conditionString .= $key.' <= ? {op} '; |
|
540 | + $conditionValues[] = $value2; |
|
541 | + } elseif (strtolower($operator) == 'in') { |
|
542 | + $conditionValues = array_merge($conditionValues, $value); |
|
543 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
544 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
545 | + } elseif (strtolower($operator) == 'null') { |
|
546 | + $conditionString .= $key.' is null {op} '; |
|
547 | + } elseif (strtolower($operator) == 'not null') { |
|
548 | + $conditionString .= $key.' is not null {op} '; |
|
549 | + } elseif (strtolower($operator) == 'has') { |
|
550 | + $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
551 | + $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
552 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
553 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
554 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
555 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
556 | + } else { |
|
557 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
558 | + $conditionValues[] = $value; |
|
559 | + } |
|
560 | + } |
|
561 | + } |
|
562 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
563 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Wrap the given JSON selector. |
|
568 | + * |
|
569 | + * @param string $value |
|
570 | + * @return string |
|
571 | + */ |
|
572 | + protected function wrapJsonSelector($value) |
|
573 | + { |
|
574 | + $removeLast = strpos($value, ')'); |
|
575 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
576 | + $path = explode('->', $value); |
|
577 | + $field = array_shift($path); |
|
578 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
579 | + return '"'.$part.'"'; |
|
580 | + })->implode('.')); |
|
581 | 581 | |
582 | - return $removeLast === false ? $result : $result.')'; |
|
583 | - } |
|
582 | + return $removeLast === false ? $result : $result.')'; |
|
583 | + } |
|
584 | 584 | } |