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