@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * Register the given device to the logged in user. |
| 22 | 22 | * |
| 23 | 23 | * @param array $data |
| 24 | - * @return void |
|
| 24 | + * @return boolean |
|
| 25 | 25 | */ |
| 26 | 26 | public function registerDevice($data) |
| 27 | 27 | { |
@@ -8,60 +8,60 @@ |
||
| 8 | 8 | |
| 9 | 9 | class PushNotificationDeviceRepository extends BaseRepository |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Init new object. |
|
| 13 | - * |
|
| 14 | - * @param PushNotificationDevice $model |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function __construct(PushNotificationDevice $model) |
|
| 18 | - { |
|
| 19 | - parent::__construct($model); |
|
| 20 | - } |
|
| 11 | + /** |
|
| 12 | + * Init new object. |
|
| 13 | + * |
|
| 14 | + * @param PushNotificationDevice $model |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function __construct(PushNotificationDevice $model) |
|
| 18 | + { |
|
| 19 | + parent::__construct($model); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Register the given device to the logged in user. |
|
| 24 | - * |
|
| 25 | - * @param array $data |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function registerDevice($data) |
|
| 29 | - { |
|
| 30 | - $data['access_token'] = \Auth::user()->token(); |
|
| 31 | - $data['user_id'] = \Auth::id(); |
|
| 32 | - $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | - where('user_id', $data['user_id'])-> |
|
| 34 | - first(); |
|
| 22 | + /** |
|
| 23 | + * Register the given device to the logged in user. |
|
| 24 | + * |
|
| 25 | + * @param array $data |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function registerDevice($data) |
|
| 29 | + { |
|
| 30 | + $data['access_token'] = \Auth::user()->token(); |
|
| 31 | + $data['user_id'] = \Auth::id(); |
|
| 32 | + $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | + where('user_id', $data['user_id'])-> |
|
| 34 | + first(); |
|
| 35 | 35 | |
| 36 | - if ($device) { |
|
| 37 | - $data['id'] = $device->id; |
|
| 38 | - } |
|
| 36 | + if ($device) { |
|
| 37 | + $data['id'] = $device->id; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return $this->save($data); |
|
| 41 | - } |
|
| 40 | + return $this->save($data); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Generate the given message data. |
|
| 45 | - * |
|
| 46 | - * @param string $title |
|
| 47 | - * @param string $message |
|
| 48 | - * @param array $data |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function generateMessageData($title, $message, $data = []) |
|
| 52 | - { |
|
| 53 | - $optionBuilder = new OptionsBuilder(); |
|
| 54 | - $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | - $dataBuilder = new PayloadDataBuilder(); |
|
| 43 | + /** |
|
| 44 | + * Generate the given message data. |
|
| 45 | + * |
|
| 46 | + * @param string $title |
|
| 47 | + * @param string $message |
|
| 48 | + * @param array $data |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function generateMessageData($title, $message, $data = []) |
|
| 52 | + { |
|
| 53 | + $optionBuilder = new OptionsBuilder(); |
|
| 54 | + $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | + $dataBuilder = new PayloadDataBuilder(); |
|
| 56 | 56 | |
| 57 | - $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | - $notificationBuilder->setBody($message); |
|
| 59 | - $dataBuilder->addData($data); |
|
| 57 | + $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | + $notificationBuilder->setBody($message); |
|
| 59 | + $dataBuilder->addData($data); |
|
| 60 | 60 | |
| 61 | - $options = $optionBuilder->build(); |
|
| 62 | - $notification = $notificationBuilder->build(); |
|
| 63 | - $data = $dataBuilder->build(); |
|
| 61 | + $options = $optionBuilder->build(); |
|
| 62 | + $notification = $notificationBuilder->build(); |
|
| 63 | + $data = $dataBuilder->build(); |
|
| 64 | 64 | |
| 65 | - return compact('options', 'notification', 'data'); |
|
| 66 | - } |
|
| 65 | + return compact('options', 'notification', 'data'); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -9,41 +9,41 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ConfirmEmail extends Notification implements ShouldQueue |
| 11 | 11 | { |
| 12 | - use Queueable; |
|
| 12 | + use Queueable; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Init new object. |
|
| 16 | - * |
|
| 17 | - * @return void |
|
| 18 | - */ |
|
| 19 | - public function __construct() |
|
| 20 | - { |
|
| 21 | - // |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * Init new object. |
|
| 16 | + * |
|
| 17 | + * @return void |
|
| 18 | + */ |
|
| 19 | + public function __construct() |
|
| 20 | + { |
|
| 21 | + // |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the notification's delivery channels. |
|
| 26 | - * |
|
| 27 | - * @param mixed $notifiable |
|
| 28 | - * @return string[] |
|
| 29 | - */ |
|
| 30 | - public function via($notifiable) |
|
| 31 | - { |
|
| 32 | - return ['mail']; |
|
| 33 | - } |
|
| 24 | + /** |
|
| 25 | + * Get the notification's delivery channels. |
|
| 26 | + * |
|
| 27 | + * @param mixed $notifiable |
|
| 28 | + * @return string[] |
|
| 29 | + */ |
|
| 30 | + public function via($notifiable) |
|
| 31 | + { |
|
| 32 | + return ['mail']; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the mail representation of the notification. |
|
| 37 | - * |
|
| 38 | - * @param mixed $notifiable |
|
| 39 | - * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 40 | - */ |
|
| 41 | - public function toMail($notifiable) |
|
| 42 | - { |
|
| 43 | - return (new MailMessage) |
|
| 44 | - ->subject('Email verification') |
|
| 45 | - ->line('Email verification') |
|
| 46 | - ->line('To validate your email click on the button below') |
|
| 47 | - ->action('Verify your email', config('skeleton.confrim_email_url').'/'.$notifiable->confirmation_code); |
|
| 48 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the mail representation of the notification. |
|
| 37 | + * |
|
| 38 | + * @param mixed $notifiable |
|
| 39 | + * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 40 | + */ |
|
| 41 | + public function toMail($notifiable) |
|
| 42 | + { |
|
| 43 | + return (new MailMessage) |
|
| 44 | + ->subject('Email verification') |
|
| 45 | + ->line('Email verification') |
|
| 46 | + ->line('To validate your email click on the button below') |
|
| 47 | + ->action('Verify your email', config('skeleton.confrim_email_url').'/'.$notifiable->confirmation_code); |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -9,43 +9,43 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ResetPassword extends Notification implements ShouldQueue |
| 11 | 11 | { |
| 12 | - use Queueable; |
|
| 12 | + use Queueable; |
|
| 13 | 13 | |
| 14 | - protected $token; |
|
| 14 | + protected $token; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Init new object. |
|
| 18 | - * |
|
| 19 | - * @return void |
|
| 20 | - */ |
|
| 21 | - public function __construct($token) |
|
| 22 | - { |
|
| 23 | - $this->token = $token; |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * Init new object. |
|
| 18 | + * |
|
| 19 | + * @return void |
|
| 20 | + */ |
|
| 21 | + public function __construct($token) |
|
| 22 | + { |
|
| 23 | + $this->token = $token; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get the notification's delivery channels. |
|
| 28 | - * |
|
| 29 | - * @param mixed $notifiable |
|
| 30 | - * @return string[] |
|
| 31 | - */ |
|
| 32 | - public function via($notifiable) |
|
| 33 | - { |
|
| 34 | - return ['mail']; |
|
| 35 | - } |
|
| 26 | + /** |
|
| 27 | + * Get the notification's delivery channels. |
|
| 28 | + * |
|
| 29 | + * @param mixed $notifiable |
|
| 30 | + * @return string[] |
|
| 31 | + */ |
|
| 32 | + public function via($notifiable) |
|
| 33 | + { |
|
| 34 | + return ['mail']; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get the mail representation of the notification. |
|
| 39 | - * |
|
| 40 | - * @param mixed $notifiable |
|
| 41 | - * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 42 | - */ |
|
| 43 | - public function toMail($notifiable) |
|
| 44 | - { |
|
| 45 | - return (new MailMessage) |
|
| 46 | - ->subject('Reset passowrd') |
|
| 47 | - ->line('Reset passowrd') |
|
| 48 | - ->line('To reset your password click on the button below') |
|
| 49 | - ->action('Reset password', config('skeleton.reset_password_url').'/'.$this->token); |
|
| 50 | - } |
|
| 37 | + /** |
|
| 38 | + * Get the mail representation of the notification. |
|
| 39 | + * |
|
| 40 | + * @param mixed $notifiable |
|
| 41 | + * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 42 | + */ |
|
| 43 | + public function toMail($notifiable) |
|
| 44 | + { |
|
| 45 | + return (new MailMessage) |
|
| 46 | + ->subject('Reset passowrd') |
|
| 47 | + ->line('Reset passowrd') |
|
| 48 | + ->line('To reset your password click on the button below') |
|
| 49 | + ->action('Reset password', config('skeleton.reset_password_url').'/'.$this->token); |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -5,73 +5,73 @@ |
||
| 5 | 5 | |
| 6 | 6 | class NotificationRepository extends BaseRepository |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Init new object. |
|
| 10 | - * |
|
| 11 | - * @param Notification $model |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function __construct(Notification $model) |
|
| 15 | - { |
|
| 16 | - parent::__construct($model); |
|
| 17 | - } |
|
| 8 | + /** |
|
| 9 | + * Init new object. |
|
| 10 | + * |
|
| 11 | + * @param Notification $model |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function __construct(Notification $model) |
|
| 15 | + { |
|
| 16 | + parent::__construct($model); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Retrieve all notifications of the logged in user. |
|
| 21 | - * |
|
| 22 | - * @param integer $perPage |
|
| 23 | - * @return Collection |
|
| 24 | - */ |
|
| 25 | - public function list($perPage) |
|
| 26 | - { |
|
| 27 | - return \Auth::user()->notifications()->paginate($perPage); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Retrieve all notifications of the logged in user. |
|
| 21 | + * |
|
| 22 | + * @param integer $perPage |
|
| 23 | + * @return Collection |
|
| 24 | + */ |
|
| 25 | + public function list($perPage) |
|
| 26 | + { |
|
| 27 | + return \Auth::user()->notifications()->paginate($perPage); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Retrieve unread notifications of the logged in user. |
|
| 32 | - * |
|
| 33 | - * @param integer $perPage |
|
| 34 | - * @return Collection |
|
| 35 | - */ |
|
| 36 | - public function unread($perPage) |
|
| 37 | - { |
|
| 38 | - return \Auth::user()->unreadNotifications()->paginate($perPage); |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Retrieve unread notifications of the logged in user. |
|
| 32 | + * |
|
| 33 | + * @param integer $perPage |
|
| 34 | + * @return Collection |
|
| 35 | + */ |
|
| 36 | + public function unread($perPage) |
|
| 37 | + { |
|
| 38 | + return \Auth::user()->unreadNotifications()->paginate($perPage); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Mark the notification as read. |
|
| 43 | - * |
|
| 44 | - * @param integer $id |
|
| 45 | - * @return object |
|
| 46 | - */ |
|
| 47 | - public function markAsRead($id) |
|
| 48 | - { |
|
| 49 | - if ($notification = \Auth::user()->unreadNotifications()->where('id', $id)->first()) { |
|
| 50 | - $notification->markAsRead(); |
|
| 51 | - } |
|
| 52 | - } |
|
| 41 | + /** |
|
| 42 | + * Mark the notification as read. |
|
| 43 | + * |
|
| 44 | + * @param integer $id |
|
| 45 | + * @return object |
|
| 46 | + */ |
|
| 47 | + public function markAsRead($id) |
|
| 48 | + { |
|
| 49 | + if ($notification = \Auth::user()->unreadNotifications()->where('id', $id)->first()) { |
|
| 50 | + $notification->markAsRead(); |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Mark all notifications as read. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function markAllAsRead() |
|
| 60 | - { |
|
| 61 | - \Auth::user()->unreadNotifications()->update(['read_at' => now()]); |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * Mark all notifications as read. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function markAllAsRead() |
|
| 60 | + { |
|
| 61 | + \Auth::user()->unreadNotifications()->update(['read_at' => now()]); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Notify th given user with the given notification. |
|
| 66 | - * |
|
| 67 | - * @param collection $users |
|
| 68 | - * @param string $notification |
|
| 69 | - * @param object $notificationData |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function notify($users, $notification, $notificationData = false) |
|
| 73 | - { |
|
| 74 | - $notification = 'App\Modules\Notifications\Notifications\\'.$notification; |
|
| 75 | - \Notification::send($users, new $notification($notificationData)); |
|
| 76 | - } |
|
| 64 | + /** |
|
| 65 | + * Notify th given user with the given notification. |
|
| 66 | + * |
|
| 67 | + * @param collection $users |
|
| 68 | + * @param string $notification |
|
| 69 | + * @param object $notificationData |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function notify($users, $notification, $notificationData = false) |
|
| 73 | + { |
|
| 74 | + $notification = 'App\Modules\Notifications\Notifications\\'.$notification; |
|
| 75 | + \Notification::send($users, new $notification($notificationData)); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -5,32 +5,32 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Core implements BaseFactoryInterface |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Construct the repository class name based on |
|
| 10 | - * the method name called, search in the |
|
| 11 | - * given namespaces for the class and |
|
| 12 | - * return an instance. |
|
| 13 | - * |
|
| 14 | - * @param string $name the called method name |
|
| 15 | - * @param array $arguments the method arguments |
|
| 16 | - * @return object |
|
| 17 | - */ |
|
| 18 | - public function __call($name, $arguments) |
|
| 19 | - { |
|
| 20 | - foreach (\Module::all() as $module) { |
|
| 21 | - $nameSpace = 'App\\Modules\\' . $module['basename'] ; |
|
| 22 | - $model = ucfirst(\Str::singular($name)); |
|
| 23 | - $class = $nameSpace . '\\Repositories\\' . $model . 'Repository'; |
|
| 24 | - $decoratedClass = $class . '\\Decorated'; |
|
| 8 | + /** |
|
| 9 | + * Construct the repository class name based on |
|
| 10 | + * the method name called, search in the |
|
| 11 | + * given namespaces for the class and |
|
| 12 | + * return an instance. |
|
| 13 | + * |
|
| 14 | + * @param string $name the called method name |
|
| 15 | + * @param array $arguments the method arguments |
|
| 16 | + * @return object |
|
| 17 | + */ |
|
| 18 | + public function __call($name, $arguments) |
|
| 19 | + { |
|
| 20 | + foreach (\Module::all() as $module) { |
|
| 21 | + $nameSpace = 'App\\Modules\\' . $module['basename'] ; |
|
| 22 | + $model = ucfirst(\Str::singular($name)); |
|
| 23 | + $class = $nameSpace . '\\Repositories\\' . $model . 'Repository'; |
|
| 24 | + $decoratedClass = $class . '\\Decorated'; |
|
| 25 | 25 | |
| 26 | - if (class_exists($class)) { |
|
| 27 | - $classObj = \App::make($class); |
|
| 28 | - \App::singleton($class, function ($app) use ($classObj) { |
|
| 29 | - return new CachingDecorator($classObj, $app['cache.store']); |
|
| 30 | - }); |
|
| 26 | + if (class_exists($class)) { |
|
| 27 | + $classObj = \App::make($class); |
|
| 28 | + \App::singleton($class, function ($app) use ($classObj) { |
|
| 29 | + return new CachingDecorator($classObj, $app['cache.store']); |
|
| 30 | + }); |
|
| 31 | 31 | |
| 32 | - return \App::make($class); |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - } |
|
| 32 | + return \App::make($class); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -18,14 +18,14 @@ |
||
| 18 | 18 | public function __call($name, $arguments) |
| 19 | 19 | { |
| 20 | 20 | foreach (\Module::all() as $module) { |
| 21 | - $nameSpace = 'App\\Modules\\' . $module['basename'] ; |
|
| 21 | + $nameSpace = 'App\\Modules\\'.$module['basename']; |
|
| 22 | 22 | $model = ucfirst(\Str::singular($name)); |
| 23 | - $class = $nameSpace . '\\Repositories\\' . $model . 'Repository'; |
|
| 24 | - $decoratedClass = $class . '\\Decorated'; |
|
| 23 | + $class = $nameSpace.'\\Repositories\\'.$model.'Repository'; |
|
| 24 | + $decoratedClass = $class.'\\Decorated'; |
|
| 25 | 25 | |
| 26 | 26 | if (class_exists($class)) { |
| 27 | 27 | $classObj = \App::make($class); |
| 28 | - \App::singleton($class, function ($app) use ($classObj) { |
|
| 28 | + \App::singleton($class, function($app) use ($classObj) { |
|
| 29 | 29 | return new CachingDecorator($classObj, $app['cache.store']); |
| 30 | 30 | }); |
| 31 | 31 | |
@@ -5,29 +5,29 @@ |
||
| 5 | 5 | |
| 6 | 6 | class SettingRepository extends BaseRepository |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Init new object. |
|
| 10 | - * |
|
| 11 | - * @param Setting $model |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function __construct(Setting $model) |
|
| 15 | - { |
|
| 16 | - parent::__construct($model); |
|
| 17 | - } |
|
| 8 | + /** |
|
| 9 | + * Init new object. |
|
| 10 | + * |
|
| 11 | + * @param Setting $model |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function __construct(Setting $model) |
|
| 15 | + { |
|
| 16 | + parent::__construct($model); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Save list of settings. |
|
| 21 | - * |
|
| 22 | - * @param array $data |
|
| 23 | - * @return void |
|
| 24 | - */ |
|
| 25 | - public function saveMany(array $data) |
|
| 26 | - { |
|
| 27 | - \DB::transaction(function () use ($data) { |
|
| 28 | - foreach ($data as $value) { |
|
| 29 | - $this->save($value); |
|
| 30 | - } |
|
| 31 | - }); |
|
| 32 | - } |
|
| 19 | + /** |
|
| 20 | + * Save list of settings. |
|
| 21 | + * |
|
| 22 | + * @param array $data |
|
| 23 | + * @return void |
|
| 24 | + */ |
|
| 25 | + public function saveMany(array $data) |
|
| 26 | + { |
|
| 27 | + \DB::transaction(function () use ($data) { |
|
| 28 | + foreach ($data as $value) { |
|
| 29 | + $this->save($value); |
|
| 30 | + } |
|
| 31 | + }); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -6,619 +6,619 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BaseRepository implements BaseRepositoryInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The model implementation. |
|
| 11 | - * |
|
| 12 | - * @var object |
|
| 13 | - */ |
|
| 14 | - public $model; |
|
| 9 | + /** |
|
| 10 | + * The model implementation. |
|
| 11 | + * |
|
| 12 | + * @var object |
|
| 13 | + */ |
|
| 14 | + public $model; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Init new object. |
|
| 18 | - * |
|
| 19 | - * @var mixed model |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function __construct($model) |
|
| 23 | - { |
|
| 24 | - $this->model = $model; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Fetch all records with relations from the storage. |
|
| 29 | - * |
|
| 30 | - * @param array $relations |
|
| 31 | - * @param string $sortBy |
|
| 32 | - * @param boolean $desc |
|
| 33 | - * @param array $columns |
|
| 34 | - * @return collection |
|
| 35 | - */ |
|
| 36 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 37 | - { |
|
| 38 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 39 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Fetch all records with relations from storage in pages |
|
| 44 | - * that matche the given query. |
|
| 45 | - * |
|
| 46 | - * @param string $query |
|
| 47 | - * @param integer $perPage |
|
| 48 | - * @param array $relations |
|
| 49 | - * @param string $sortBy |
|
| 50 | - * @param boolean $desc |
|
| 51 | - * @param array $columns |
|
| 52 | - * @return collection |
|
| 53 | - */ |
|
| 54 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 55 | - { |
|
| 56 | - $model = $this->model->with($relations); |
|
| 57 | - $conditionColumns = $this->model->searchable; |
|
| 58 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Construct the select conditions for the model. |
|
| 62 | - */ |
|
| 63 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 64 | - |
|
| 65 | - if (count($conditionColumns)) { |
|
| 66 | - $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
| 67 | - if (Str::contains($column, '->')) { |
|
| 68 | - $column = $this->wrapJsonSelector($column); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Use the first element in the model columns to construct the first condition. |
|
| 73 | - */ |
|
| 74 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 79 | - */ |
|
| 80 | - foreach ($conditionColumns as $column) { |
|
| 81 | - $column = 'LOWER('.$column.')'; |
|
| 82 | - if (Str::contains($column, '->')) { |
|
| 83 | - $column = $this->wrapJsonSelector($column); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Loop through the model relations. |
|
| 91 | - */ |
|
| 92 | - foreach ($relations as $relation) { |
|
| 93 | - /** |
|
| 94 | - * Remove the sub relation if exists. |
|
| 95 | - */ |
|
| 96 | - $relation = explode('.', $relation)[0]; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Try to fetch the relation repository from the core. |
|
| 100 | - */ |
|
| 101 | - if (\Core::$relation()) { |
|
| 102 | - /** |
|
| 103 | - * Construct the relation condition. |
|
| 104 | - */ |
|
| 105 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 106 | - |
|
| 107 | - $subModel->where(function ($q) use ($query, $relation) { |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get columns of the relation. |
|
| 111 | - */ |
|
| 112 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 113 | - |
|
| 114 | - if (count($subConditionColumns)) { |
|
| 115 | - $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
| 116 | - if (Str::contains($column, '->')) { |
|
| 117 | - $column = $this->wrapJsonSelector($column); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Use the first element in the relation model columns to construct the first condition. |
|
| 122 | - */ |
|
| 123 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 128 | - */ |
|
| 129 | - foreach ($subConditionColumns as $subConditionColumn) { |
|
| 130 | - $column = 'LOWER('.$subConditionColumn.')'; |
|
| 131 | - if (Str::contains($column, '->')) { |
|
| 132 | - $column = $this->wrapJsonSelector($column); |
|
| 133 | - } |
|
| 16 | + /** |
|
| 17 | + * Init new object. |
|
| 18 | + * |
|
| 19 | + * @var mixed model |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function __construct($model) |
|
| 23 | + { |
|
| 24 | + $this->model = $model; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Fetch all records with relations from the storage. |
|
| 29 | + * |
|
| 30 | + * @param array $relations |
|
| 31 | + * @param string $sortBy |
|
| 32 | + * @param boolean $desc |
|
| 33 | + * @param array $columns |
|
| 34 | + * @return collection |
|
| 35 | + */ |
|
| 36 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 37 | + { |
|
| 38 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 39 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Fetch all records with relations from storage in pages |
|
| 44 | + * that matche the given query. |
|
| 45 | + * |
|
| 46 | + * @param string $query |
|
| 47 | + * @param integer $perPage |
|
| 48 | + * @param array $relations |
|
| 49 | + * @param string $sortBy |
|
| 50 | + * @param boolean $desc |
|
| 51 | + * @param array $columns |
|
| 52 | + * @return collection |
|
| 53 | + */ |
|
| 54 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 55 | + { |
|
| 56 | + $model = $this->model->with($relations); |
|
| 57 | + $conditionColumns = $this->model->searchable; |
|
| 58 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Construct the select conditions for the model. |
|
| 62 | + */ |
|
| 63 | + $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 64 | + |
|
| 65 | + if (count($conditionColumns)) { |
|
| 66 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
| 67 | + if (Str::contains($column, '->')) { |
|
| 68 | + $column = $this->wrapJsonSelector($column); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Use the first element in the model columns to construct the first condition. |
|
| 73 | + */ |
|
| 74 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 79 | + */ |
|
| 80 | + foreach ($conditionColumns as $column) { |
|
| 81 | + $column = 'LOWER('.$column.')'; |
|
| 82 | + if (Str::contains($column, '->')) { |
|
| 83 | + $column = $this->wrapJsonSelector($column); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Loop through the model relations. |
|
| 91 | + */ |
|
| 92 | + foreach ($relations as $relation) { |
|
| 93 | + /** |
|
| 94 | + * Remove the sub relation if exists. |
|
| 95 | + */ |
|
| 96 | + $relation = explode('.', $relation)[0]; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Try to fetch the relation repository from the core. |
|
| 100 | + */ |
|
| 101 | + if (\Core::$relation()) { |
|
| 102 | + /** |
|
| 103 | + * Construct the relation condition. |
|
| 104 | + */ |
|
| 105 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 106 | + |
|
| 107 | + $subModel->where(function ($q) use ($query, $relation) { |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get columns of the relation. |
|
| 111 | + */ |
|
| 112 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 113 | + |
|
| 114 | + if (count($subConditionColumns)) { |
|
| 115 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
| 116 | + if (Str::contains($column, '->')) { |
|
| 117 | + $column = $this->wrapJsonSelector($column); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Use the first element in the relation model columns to construct the first condition. |
|
| 122 | + */ |
|
| 123 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 128 | + */ |
|
| 129 | + foreach ($subConditionColumns as $subConditionColumn) { |
|
| 130 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
| 131 | + if (Str::contains($column, '->')) { |
|
| 132 | + $column = $this->wrapJsonSelector($column); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 136 | - } |
|
| 137 | - }); |
|
| 138 | - }); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - }); |
|
| 135 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 136 | + } |
|
| 137 | + }); |
|
| 138 | + }); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + }); |
|
| 142 | 142 | |
| 143 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 144 | - } |
|
| 143 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Fetch all records with relations from storage in pages. |
|
| 148 | - * |
|
| 149 | - * @param integer $perPage |
|
| 150 | - * @param array $relations |
|
| 151 | - * @param string $sortBy |
|
| 152 | - * @param boolean $desc |
|
| 153 | - * @param array $columns |
|
| 154 | - * @return collection |
|
| 155 | - */ |
|
| 156 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 157 | - { |
|
| 158 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 159 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Fetch all records with relations based on |
|
| 164 | - * the given condition from storage in pages. |
|
| 165 | - * |
|
| 166 | - * @param array $conditions array of conditions |
|
| 167 | - * @param integer $perPage |
|
| 168 | - * @param array $relations |
|
| 169 | - * @param string $sortBy |
|
| 170 | - * @param boolean $desc |
|
| 171 | - * @param array $columns |
|
| 172 | - * @return collection |
|
| 173 | - */ |
|
| 174 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 175 | - { |
|
| 176 | - unset($conditions['page']); |
|
| 177 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 178 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 179 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 180 | - } |
|
| 146 | + /** |
|
| 147 | + * Fetch all records with relations from storage in pages. |
|
| 148 | + * |
|
| 149 | + * @param integer $perPage |
|
| 150 | + * @param array $relations |
|
| 151 | + * @param string $sortBy |
|
| 152 | + * @param boolean $desc |
|
| 153 | + * @param array $columns |
|
| 154 | + * @return collection |
|
| 155 | + */ |
|
| 156 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 157 | + { |
|
| 158 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 159 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Fetch all records with relations based on |
|
| 164 | + * the given condition from storage in pages. |
|
| 165 | + * |
|
| 166 | + * @param array $conditions array of conditions |
|
| 167 | + * @param integer $perPage |
|
| 168 | + * @param array $relations |
|
| 169 | + * @param string $sortBy |
|
| 170 | + * @param boolean $desc |
|
| 171 | + * @param array $columns |
|
| 172 | + * @return collection |
|
| 173 | + */ |
|
| 174 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 175 | + { |
|
| 176 | + unset($conditions['page']); |
|
| 177 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 178 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 179 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Save the given model to the storage. |
|
| 184 | - * |
|
| 185 | - * @param array $data |
|
| 186 | - * @return mixed |
|
| 187 | - */ |
|
| 188 | - public function save(array $data) |
|
| 189 | - { |
|
| 190 | - \Session::put('locale', 'all'); |
|
| 191 | - $model = false; |
|
| 192 | - $modelClass = $this->model; |
|
| 193 | - $relations = []; |
|
| 194 | - |
|
| 195 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 196 | - /** |
|
| 197 | - * If the id is present in the data then select the model for updating, |
|
| 198 | - * else create new model. |
|
| 199 | - * @var array |
|
| 200 | - */ |
|
| 201 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 202 | - if (! $model) { |
|
| 203 | - \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Construct the model object with the given data, |
|
| 208 | - * and if there is a relation add it to relations array, |
|
| 209 | - * then save the model. |
|
| 210 | - */ |
|
| 211 | - foreach ($data as $key => $value) { |
|
| 212 | - /** |
|
| 213 | - * If the attribute is a relation. |
|
| 214 | - */ |
|
| 215 | - $relation = camel_case($key); |
|
| 216 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 217 | - /** |
|
| 218 | - * Check if the relation is a collection. |
|
| 219 | - */ |
|
| 220 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 221 | - /** |
|
| 222 | - * If the relation has no value then marke the relation data |
|
| 223 | - * related to the model to be deleted. |
|
| 224 | - */ |
|
| 225 | - if (! $value || ! count($value)) { |
|
| 226 | - $relations[$relation] = 'delete'; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - if (is_array($value)) { |
|
| 230 | - /** |
|
| 231 | - * Loop through the relation data. |
|
| 232 | - */ |
|
| 233 | - foreach ($value as $attr => $val) { |
|
| 234 | - /** |
|
| 235 | - * Get the relation model. |
|
| 236 | - */ |
|
| 237 | - $relationBaseModel = \Core::$relation()->model; |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Check if the relation is a collection. |
|
| 241 | - */ |
|
| 242 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 243 | - /** |
|
| 244 | - * If the id is present in the data then select the relation model for updating, |
|
| 245 | - * else create new model. |
|
| 246 | - */ |
|
| 247 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * If model doesn't exists. |
|
| 251 | - */ |
|
| 252 | - if (! $relationModel) { |
|
| 253 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Loop through the relation attributes. |
|
| 258 | - */ |
|
| 259 | - foreach ($val as $attr => $val) { |
|
| 260 | - /** |
|
| 261 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 262 | - */ |
|
| 263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 264 | - $relationModel->$attr = $val; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $relations[$relation][] = $relationModel; |
|
| 269 | - } else { |
|
| 270 | - /** |
|
| 271 | - * Prevent the sub relations. |
|
| 272 | - */ |
|
| 273 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 274 | - /** |
|
| 275 | - * If the id is present in the data then select the relation model for updating, |
|
| 276 | - * else create new model. |
|
| 277 | - */ |
|
| 278 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * If model doesn't exists. |
|
| 282 | - */ |
|
| 283 | - if (! $relationModel) { |
|
| 284 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - foreach ($value as $relationAttribute => $relationValue) { |
|
| 288 | - /** |
|
| 289 | - * Prevent attributes not in the fillable. |
|
| 290 | - */ |
|
| 291 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 292 | - $relationModel->$relationAttribute = $relationValue; |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $relations[$relation] = $relationModel; |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - } elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
| 302 | - /** |
|
| 303 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 304 | - */ |
|
| 305 | - $model->$key = $value; |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Loop through the relations array. |
|
| 311 | - */ |
|
| 312 | - foreach ($relations as $key => $value) { |
|
| 313 | - /** |
|
| 314 | - * If the relation is marked for delete then delete it. |
|
| 315 | - */ |
|
| 316 | - if ($value == 'delete' && $model->$key()->count()) { |
|
| 317 | - $model->$key()->delete(); |
|
| 318 | - } elseif (gettype($value) == 'array') { |
|
| 319 | - /** |
|
| 320 | - * Save the model. |
|
| 321 | - */ |
|
| 322 | - $model->save(); |
|
| 323 | - $ids = []; |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Loop through the relations. |
|
| 327 | - */ |
|
| 328 | - foreach ($value as $val) { |
|
| 329 | - switch (class_basename($model->$key())) { |
|
| 330 | - /** |
|
| 331 | - * If the relation is one to many then update it's foreign key with |
|
| 332 | - * the model id and save it then add its id to ids array to delete all |
|
| 333 | - * relations who's id isn't in the ids array. |
|
| 334 | - */ |
|
| 335 | - case 'HasMany': |
|
| 336 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 337 | - $val->$foreignKeyName = $model->id; |
|
| 338 | - $val->save(); |
|
| 339 | - $ids[] = $val->id; |
|
| 340 | - break; |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * If the relation is many to many then add it's id to the ids array to |
|
| 344 | - * attache these ids to the model. |
|
| 345 | - */ |
|
| 346 | - case 'BelongsToMany': |
|
| 347 | - $val->save(); |
|
| 348 | - $ids[] = $val->id; |
|
| 349 | - break; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - switch (class_basename($model->$key())) { |
|
| 353 | - /** |
|
| 354 | - * If the relation is one to many then delete all |
|
| 355 | - * relations who's id isn't in the ids array. |
|
| 356 | - */ |
|
| 357 | - case 'HasMany': |
|
| 358 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 359 | - break; |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * If the relation is many to many then |
|
| 363 | - * detach the previous data and attach |
|
| 364 | - * the ids array to the model. |
|
| 365 | - */ |
|
| 366 | - case 'BelongsToMany': |
|
| 367 | - $model->$key()->detach(); |
|
| 368 | - $model->$key()->attach($ids); |
|
| 369 | - break; |
|
| 370 | - } |
|
| 371 | - } else { |
|
| 372 | - switch (class_basename($model->$key())) { |
|
| 373 | - /** |
|
| 374 | - * If the relation is one to one. |
|
| 375 | - */ |
|
| 376 | - case 'HasOne': |
|
| 377 | - /** |
|
| 378 | - * Save the model. |
|
| 379 | - */ |
|
| 380 | - $model->save(); |
|
| 381 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 382 | - $value->$foreignKeyName = $model->id; |
|
| 383 | - $value->save(); |
|
| 384 | - break; |
|
| 385 | - case 'BelongsTo': |
|
| 386 | - /** |
|
| 387 | - * Save the model. |
|
| 388 | - */ |
|
| 389 | - $value->save(); |
|
| 390 | - $model->$key()->associate($value); |
|
| 391 | - break; |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Save the model. |
|
| 398 | - */ |
|
| 399 | - $model->save(); |
|
| 400 | - }); |
|
| 182 | + /** |
|
| 183 | + * Save the given model to the storage. |
|
| 184 | + * |
|
| 185 | + * @param array $data |
|
| 186 | + * @return mixed |
|
| 187 | + */ |
|
| 188 | + public function save(array $data) |
|
| 189 | + { |
|
| 190 | + \Session::put('locale', 'all'); |
|
| 191 | + $model = false; |
|
| 192 | + $modelClass = $this->model; |
|
| 193 | + $relations = []; |
|
| 194 | + |
|
| 195 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 196 | + /** |
|
| 197 | + * If the id is present in the data then select the model for updating, |
|
| 198 | + * else create new model. |
|
| 199 | + * @var array |
|
| 200 | + */ |
|
| 201 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 202 | + if (! $model) { |
|
| 203 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Construct the model object with the given data, |
|
| 208 | + * and if there is a relation add it to relations array, |
|
| 209 | + * then save the model. |
|
| 210 | + */ |
|
| 211 | + foreach ($data as $key => $value) { |
|
| 212 | + /** |
|
| 213 | + * If the attribute is a relation. |
|
| 214 | + */ |
|
| 215 | + $relation = camel_case($key); |
|
| 216 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 217 | + /** |
|
| 218 | + * Check if the relation is a collection. |
|
| 219 | + */ |
|
| 220 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 221 | + /** |
|
| 222 | + * If the relation has no value then marke the relation data |
|
| 223 | + * related to the model to be deleted. |
|
| 224 | + */ |
|
| 225 | + if (! $value || ! count($value)) { |
|
| 226 | + $relations[$relation] = 'delete'; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + if (is_array($value)) { |
|
| 230 | + /** |
|
| 231 | + * Loop through the relation data. |
|
| 232 | + */ |
|
| 233 | + foreach ($value as $attr => $val) { |
|
| 234 | + /** |
|
| 235 | + * Get the relation model. |
|
| 236 | + */ |
|
| 237 | + $relationBaseModel = \Core::$relation()->model; |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Check if the relation is a collection. |
|
| 241 | + */ |
|
| 242 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 243 | + /** |
|
| 244 | + * If the id is present in the data then select the relation model for updating, |
|
| 245 | + * else create new model. |
|
| 246 | + */ |
|
| 247 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * If model doesn't exists. |
|
| 251 | + */ |
|
| 252 | + if (! $relationModel) { |
|
| 253 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Loop through the relation attributes. |
|
| 258 | + */ |
|
| 259 | + foreach ($val as $attr => $val) { |
|
| 260 | + /** |
|
| 261 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 262 | + */ |
|
| 263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 264 | + $relationModel->$attr = $val; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $relations[$relation][] = $relationModel; |
|
| 269 | + } else { |
|
| 270 | + /** |
|
| 271 | + * Prevent the sub relations. |
|
| 272 | + */ |
|
| 273 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 274 | + /** |
|
| 275 | + * If the id is present in the data then select the relation model for updating, |
|
| 276 | + * else create new model. |
|
| 277 | + */ |
|
| 278 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * If model doesn't exists. |
|
| 282 | + */ |
|
| 283 | + if (! $relationModel) { |
|
| 284 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + foreach ($value as $relationAttribute => $relationValue) { |
|
| 288 | + /** |
|
| 289 | + * Prevent attributes not in the fillable. |
|
| 290 | + */ |
|
| 291 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 292 | + $relationModel->$relationAttribute = $relationValue; |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $relations[$relation] = $relationModel; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + } elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
| 302 | + /** |
|
| 303 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 304 | + */ |
|
| 305 | + $model->$key = $value; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Loop through the relations array. |
|
| 311 | + */ |
|
| 312 | + foreach ($relations as $key => $value) { |
|
| 313 | + /** |
|
| 314 | + * If the relation is marked for delete then delete it. |
|
| 315 | + */ |
|
| 316 | + if ($value == 'delete' && $model->$key()->count()) { |
|
| 317 | + $model->$key()->delete(); |
|
| 318 | + } elseif (gettype($value) == 'array') { |
|
| 319 | + /** |
|
| 320 | + * Save the model. |
|
| 321 | + */ |
|
| 322 | + $model->save(); |
|
| 323 | + $ids = []; |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Loop through the relations. |
|
| 327 | + */ |
|
| 328 | + foreach ($value as $val) { |
|
| 329 | + switch (class_basename($model->$key())) { |
|
| 330 | + /** |
|
| 331 | + * If the relation is one to many then update it's foreign key with |
|
| 332 | + * the model id and save it then add its id to ids array to delete all |
|
| 333 | + * relations who's id isn't in the ids array. |
|
| 334 | + */ |
|
| 335 | + case 'HasMany': |
|
| 336 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 337 | + $val->$foreignKeyName = $model->id; |
|
| 338 | + $val->save(); |
|
| 339 | + $ids[] = $val->id; |
|
| 340 | + break; |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * If the relation is many to many then add it's id to the ids array to |
|
| 344 | + * attache these ids to the model. |
|
| 345 | + */ |
|
| 346 | + case 'BelongsToMany': |
|
| 347 | + $val->save(); |
|
| 348 | + $ids[] = $val->id; |
|
| 349 | + break; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + switch (class_basename($model->$key())) { |
|
| 353 | + /** |
|
| 354 | + * If the relation is one to many then delete all |
|
| 355 | + * relations who's id isn't in the ids array. |
|
| 356 | + */ |
|
| 357 | + case 'HasMany': |
|
| 358 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 359 | + break; |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * If the relation is many to many then |
|
| 363 | + * detach the previous data and attach |
|
| 364 | + * the ids array to the model. |
|
| 365 | + */ |
|
| 366 | + case 'BelongsToMany': |
|
| 367 | + $model->$key()->detach(); |
|
| 368 | + $model->$key()->attach($ids); |
|
| 369 | + break; |
|
| 370 | + } |
|
| 371 | + } else { |
|
| 372 | + switch (class_basename($model->$key())) { |
|
| 373 | + /** |
|
| 374 | + * If the relation is one to one. |
|
| 375 | + */ |
|
| 376 | + case 'HasOne': |
|
| 377 | + /** |
|
| 378 | + * Save the model. |
|
| 379 | + */ |
|
| 380 | + $model->save(); |
|
| 381 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 382 | + $value->$foreignKeyName = $model->id; |
|
| 383 | + $value->save(); |
|
| 384 | + break; |
|
| 385 | + case 'BelongsTo': |
|
| 386 | + /** |
|
| 387 | + * Save the model. |
|
| 388 | + */ |
|
| 389 | + $value->save(); |
|
| 390 | + $model->$key()->associate($value); |
|
| 391 | + break; |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Save the model. |
|
| 398 | + */ |
|
| 399 | + $model->save(); |
|
| 400 | + }); |
|
| 401 | 401 | |
| 402 | - return $model; |
|
| 403 | - } |
|
| 402 | + return $model; |
|
| 403 | + } |
|
| 404 | 404 | |
| 405 | - /** |
|
| 406 | - * Update record in the storage based on the given |
|
| 407 | - * condition. |
|
| 408 | - * |
|
| 409 | - * @param var $value condition value |
|
| 410 | - * @param array $data |
|
| 411 | - * @param string $attribute condition column name |
|
| 412 | - * @return void |
|
| 413 | - */ |
|
| 414 | - public function update($value, array $data, $attribute = 'id') |
|
| 415 | - { |
|
| 416 | - if ($attribute == 'id') { |
|
| 417 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 418 | - $model ? $model->update($data) : 0; |
|
| 419 | - } else { |
|
| 420 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
| 421 | - $model->update($data); |
|
| 422 | - }); |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Delete record from the storage based on the given |
|
| 428 | - * condition. |
|
| 429 | - * |
|
| 430 | - * @param var $value condition value |
|
| 431 | - * @param string $attribute condition column name |
|
| 432 | - * @return void |
|
| 433 | - */ |
|
| 434 | - public function delete($value, $attribute = 'id') |
|
| 435 | - { |
|
| 436 | - if ($attribute == 'id') { |
|
| 437 | - \DB::transaction(function () use ($value) { |
|
| 438 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 439 | - if (! $model) { |
|
| 440 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 441 | - } |
|
| 405 | + /** |
|
| 406 | + * Update record in the storage based on the given |
|
| 407 | + * condition. |
|
| 408 | + * |
|
| 409 | + * @param var $value condition value |
|
| 410 | + * @param array $data |
|
| 411 | + * @param string $attribute condition column name |
|
| 412 | + * @return void |
|
| 413 | + */ |
|
| 414 | + public function update($value, array $data, $attribute = 'id') |
|
| 415 | + { |
|
| 416 | + if ($attribute == 'id') { |
|
| 417 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 418 | + $model ? $model->update($data) : 0; |
|
| 419 | + } else { |
|
| 420 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
| 421 | + $model->update($data); |
|
| 422 | + }); |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Delete record from the storage based on the given |
|
| 428 | + * condition. |
|
| 429 | + * |
|
| 430 | + * @param var $value condition value |
|
| 431 | + * @param string $attribute condition column name |
|
| 432 | + * @return void |
|
| 433 | + */ |
|
| 434 | + public function delete($value, $attribute = 'id') |
|
| 435 | + { |
|
| 436 | + if ($attribute == 'id') { |
|
| 437 | + \DB::transaction(function () use ($value) { |
|
| 438 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 439 | + if (! $model) { |
|
| 440 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 441 | + } |
|
| 442 | 442 | |
| 443 | - $model->delete(); |
|
| 444 | - }); |
|
| 445 | - } else { |
|
| 446 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 447 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 448 | - $model->delete(); |
|
| 449 | - }); |
|
| 450 | - }); |
|
| 451 | - } |
|
| 452 | - } |
|
| 443 | + $model->delete(); |
|
| 444 | + }); |
|
| 445 | + } else { |
|
| 446 | + \DB::transaction(function () use ($value, $attribute) { |
|
| 447 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 448 | + $model->delete(); |
|
| 449 | + }); |
|
| 450 | + }); |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | 453 | |
| 454 | - /** |
|
| 455 | - * Fetch records from the storage based on the given |
|
| 456 | - * id. |
|
| 457 | - * |
|
| 458 | - * @param integer $id |
|
| 459 | - * @param string[] $relations |
|
| 460 | - * @param array $columns |
|
| 461 | - * @return object |
|
| 462 | - */ |
|
| 463 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 464 | - { |
|
| 465 | - return $this->model->with($relations)->find($id, $columns); |
|
| 466 | - } |
|
| 454 | + /** |
|
| 455 | + * Fetch records from the storage based on the given |
|
| 456 | + * id. |
|
| 457 | + * |
|
| 458 | + * @param integer $id |
|
| 459 | + * @param string[] $relations |
|
| 460 | + * @param array $columns |
|
| 461 | + * @return object |
|
| 462 | + */ |
|
| 463 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 464 | + { |
|
| 465 | + return $this->model->with($relations)->find($id, $columns); |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | - /** |
|
| 469 | - * Fetch records from the storage based on the given |
|
| 470 | - * condition. |
|
| 471 | - * |
|
| 472 | - * @param array $conditions array of conditions |
|
| 473 | - * @param array $relations |
|
| 474 | - * @param string $sortBy |
|
| 475 | - * @param boolean $desc |
|
| 476 | - * @param array $columns |
|
| 477 | - * @return collection |
|
| 478 | - */ |
|
| 479 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 480 | - { |
|
| 481 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 482 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 483 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * Fetch the first record from the storage based on the given |
|
| 488 | - * condition. |
|
| 489 | - * |
|
| 490 | - * @param array $conditions array of conditions |
|
| 491 | - * @param array $relations |
|
| 492 | - * @param array $columns |
|
| 493 | - * @return object |
|
| 494 | - */ |
|
| 495 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 496 | - { |
|
| 497 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 498 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Return the deleted models in pages based on the given conditions. |
|
| 503 | - * |
|
| 504 | - * @param array $conditions array of conditions |
|
| 505 | - * @param integer $perPage |
|
| 506 | - * @param string $sortBy |
|
| 507 | - * @param boolean $desc |
|
| 508 | - * @param array $columns |
|
| 509 | - * @return collection |
|
| 510 | - */ |
|
| 511 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 512 | - { |
|
| 513 | - unset($conditions['page']); |
|
| 514 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 515 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 516 | - $model = $this->model->onlyTrashed(); |
|
| 517 | - |
|
| 518 | - if (count($conditions['conditionValues'])) { |
|
| 519 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * Restore the deleted model. |
|
| 527 | - * |
|
| 528 | - * @param integer $id |
|
| 529 | - * @return void |
|
| 530 | - */ |
|
| 531 | - public function restore($id) |
|
| 532 | - { |
|
| 533 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 534 | - |
|
| 535 | - if (! $model) { |
|
| 536 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - $model->restore(); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Build the conditions recursively for the retrieving methods. |
|
| 544 | - * @param array $conditions |
|
| 545 | - * @return array |
|
| 546 | - */ |
|
| 547 | - protected function constructConditions($conditions, $model) |
|
| 548 | - { |
|
| 549 | - $conditionString = ''; |
|
| 550 | - $conditionValues = []; |
|
| 551 | - foreach ($conditions as $key => $value) { |
|
| 552 | - if (Str::contains($key, '->')) { |
|
| 553 | - $key = $this->wrapJsonSelector($key); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - if ($key == 'and') { |
|
| 557 | - $conditions = $this->constructConditions($value, $model); |
|
| 558 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 559 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 560 | - } elseif ($key == 'or') { |
|
| 561 | - $conditions = $this->constructConditions($value, $model); |
|
| 562 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 563 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 564 | - } else { |
|
| 565 | - if (is_array($value)) { |
|
| 566 | - $operator = $value['op']; |
|
| 567 | - if (strtolower($operator) == 'between') { |
|
| 568 | - $value1 = $value['val1']; |
|
| 569 | - $value2 = $value['val2']; |
|
| 570 | - } else { |
|
| 571 | - $value = Arr::get($value, 'val', ''); |
|
| 572 | - } |
|
| 573 | - } else { |
|
| 574 | - $operator = '='; |
|
| 575 | - } |
|
| 468 | + /** |
|
| 469 | + * Fetch records from the storage based on the given |
|
| 470 | + * condition. |
|
| 471 | + * |
|
| 472 | + * @param array $conditions array of conditions |
|
| 473 | + * @param array $relations |
|
| 474 | + * @param string $sortBy |
|
| 475 | + * @param boolean $desc |
|
| 476 | + * @param array $columns |
|
| 477 | + * @return collection |
|
| 478 | + */ |
|
| 479 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 480 | + { |
|
| 481 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 482 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 483 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * Fetch the first record from the storage based on the given |
|
| 488 | + * condition. |
|
| 489 | + * |
|
| 490 | + * @param array $conditions array of conditions |
|
| 491 | + * @param array $relations |
|
| 492 | + * @param array $columns |
|
| 493 | + * @return object |
|
| 494 | + */ |
|
| 495 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 496 | + { |
|
| 497 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 498 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Return the deleted models in pages based on the given conditions. |
|
| 503 | + * |
|
| 504 | + * @param array $conditions array of conditions |
|
| 505 | + * @param integer $perPage |
|
| 506 | + * @param string $sortBy |
|
| 507 | + * @param boolean $desc |
|
| 508 | + * @param array $columns |
|
| 509 | + * @return collection |
|
| 510 | + */ |
|
| 511 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 512 | + { |
|
| 513 | + unset($conditions['page']); |
|
| 514 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 515 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 516 | + $model = $this->model->onlyTrashed(); |
|
| 517 | + |
|
| 518 | + if (count($conditions['conditionValues'])) { |
|
| 519 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * Restore the deleted model. |
|
| 527 | + * |
|
| 528 | + * @param integer $id |
|
| 529 | + * @return void |
|
| 530 | + */ |
|
| 531 | + public function restore($id) |
|
| 532 | + { |
|
| 533 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 534 | + |
|
| 535 | + if (! $model) { |
|
| 536 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + $model->restore(); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Build the conditions recursively for the retrieving methods. |
|
| 544 | + * @param array $conditions |
|
| 545 | + * @return array |
|
| 546 | + */ |
|
| 547 | + protected function constructConditions($conditions, $model) |
|
| 548 | + { |
|
| 549 | + $conditionString = ''; |
|
| 550 | + $conditionValues = []; |
|
| 551 | + foreach ($conditions as $key => $value) { |
|
| 552 | + if (Str::contains($key, '->')) { |
|
| 553 | + $key = $this->wrapJsonSelector($key); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + if ($key == 'and') { |
|
| 557 | + $conditions = $this->constructConditions($value, $model); |
|
| 558 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 559 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 560 | + } elseif ($key == 'or') { |
|
| 561 | + $conditions = $this->constructConditions($value, $model); |
|
| 562 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 563 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 564 | + } else { |
|
| 565 | + if (is_array($value)) { |
|
| 566 | + $operator = $value['op']; |
|
| 567 | + if (strtolower($operator) == 'between') { |
|
| 568 | + $value1 = $value['val1']; |
|
| 569 | + $value2 = $value['val2']; |
|
| 570 | + } else { |
|
| 571 | + $value = Arr::get($value, 'val', ''); |
|
| 572 | + } |
|
| 573 | + } else { |
|
| 574 | + $operator = '='; |
|
| 575 | + } |
|
| 576 | 576 | |
| 577 | - if (strtolower($operator) == 'between') { |
|
| 578 | - $conditionString .= $key.' >= ? and '; |
|
| 579 | - $conditionValues[] = $value1; |
|
| 580 | - |
|
| 581 | - $conditionString .= $key.' <= ? {op} '; |
|
| 582 | - $conditionValues[] = $value2; |
|
| 583 | - } elseif (strtolower($operator) == 'in') { |
|
| 584 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 585 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 586 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 587 | - } elseif (strtolower($operator) == 'null') { |
|
| 588 | - $conditionString .= $key.' is null {op} '; |
|
| 589 | - } elseif (strtolower($operator) == 'not null') { |
|
| 590 | - $conditionString .= $key.' is not null {op} '; |
|
| 591 | - } elseif (strtolower($operator) == 'has') { |
|
| 592 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 593 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 594 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 595 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 596 | - } else { |
|
| 597 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 598 | - $conditionValues[] = $value; |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - } |
|
| 602 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 603 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Wrap the given JSON selector. |
|
| 608 | - * |
|
| 609 | - * @param string $value |
|
| 610 | - * @return string |
|
| 611 | - */ |
|
| 612 | - protected function wrapJsonSelector($value) |
|
| 613 | - { |
|
| 614 | - $removeLast = strpos($value, ')'); |
|
| 615 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 616 | - $path = explode('->', $value); |
|
| 617 | - $field = array_shift($path); |
|
| 618 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 619 | - return '"'.$part.'"'; |
|
| 620 | - })->implode('.')); |
|
| 577 | + if (strtolower($operator) == 'between') { |
|
| 578 | + $conditionString .= $key.' >= ? and '; |
|
| 579 | + $conditionValues[] = $value1; |
|
| 580 | + |
|
| 581 | + $conditionString .= $key.' <= ? {op} '; |
|
| 582 | + $conditionValues[] = $value2; |
|
| 583 | + } elseif (strtolower($operator) == 'in') { |
|
| 584 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 585 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 586 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 587 | + } elseif (strtolower($operator) == 'null') { |
|
| 588 | + $conditionString .= $key.' is null {op} '; |
|
| 589 | + } elseif (strtolower($operator) == 'not null') { |
|
| 590 | + $conditionString .= $key.' is not null {op} '; |
|
| 591 | + } elseif (strtolower($operator) == 'has') { |
|
| 592 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 593 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 594 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 595 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 596 | + } else { |
|
| 597 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 598 | + $conditionValues[] = $value; |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + } |
|
| 602 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 603 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Wrap the given JSON selector. |
|
| 608 | + * |
|
| 609 | + * @param string $value |
|
| 610 | + * @return string |
|
| 611 | + */ |
|
| 612 | + protected function wrapJsonSelector($value) |
|
| 613 | + { |
|
| 614 | + $removeLast = strpos($value, ')'); |
|
| 615 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 616 | + $path = explode('->', $value); |
|
| 617 | + $field = array_shift($path); |
|
| 618 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 619 | + return '"'.$part.'"'; |
|
| 620 | + })->implode('.')); |
|
| 621 | 621 | |
| 622 | - return $removeLast === false ? $result : $result.')'; |
|
| 623 | - } |
|
| 622 | + return $removeLast === false ? $result : $result.')'; |
|
| 623 | + } |
|
| 624 | 624 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function __construct($model) |
| 23 | 23 | { |
| 24 | - $this->model = $model; |
|
| 24 | + $this->model = $model; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | /** |
| 61 | 61 | * Construct the select conditions for the model. |
| 62 | 62 | */ |
| 63 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 63 | + $model->where(function($q) use ($query, $conditionColumns, $relations) { |
|
| 64 | 64 | |
| 65 | 65 | if (count($conditionColumns)) { |
| 66 | 66 | $column = 'LOWER('.array_shift($conditionColumns).')'; |
@@ -102,9 +102,9 @@ discard block |
||
| 102 | 102 | /** |
| 103 | 103 | * Construct the relation condition. |
| 104 | 104 | */ |
| 105 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 105 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation) { |
|
| 106 | 106 | |
| 107 | - $subModel->where(function ($q) use ($query, $relation) { |
|
| 107 | + $subModel->where(function($q) use ($query, $relation) { |
|
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | 110 | * Get columns of the relation. |
@@ -192,14 +192,14 @@ discard block |
||
| 192 | 192 | $modelClass = $this->model; |
| 193 | 193 | $relations = []; |
| 194 | 194 | |
| 195 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 195 | + \DB::transaction(function() use (&$model, &$relations, $data, $modelClass) { |
|
| 196 | 196 | /** |
| 197 | 197 | * If the id is present in the data then select the model for updating, |
| 198 | 198 | * else create new model. |
| 199 | 199 | * @var array |
| 200 | 200 | */ |
| 201 | 201 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 202 | - if (! $model) { |
|
| 202 | + if ( ! $model) { |
|
| 203 | 203 | \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
| 204 | 204 | } |
| 205 | 205 | |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | * If the relation has no value then marke the relation data |
| 223 | 223 | * related to the model to be deleted. |
| 224 | 224 | */ |
| 225 | - if (! $value || ! count($value)) { |
|
| 225 | + if ( ! $value || ! count($value)) { |
|
| 226 | 226 | $relations[$relation] = 'delete'; |
| 227 | 227 | } |
| 228 | 228 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | /** |
| 250 | 250 | * If model doesn't exists. |
| 251 | 251 | */ |
| 252 | - if (! $relationModel) { |
|
| 252 | + if ( ! $relationModel) { |
|
| 253 | 253 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
| 254 | 254 | } |
| 255 | 255 | |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | /** |
| 281 | 281 | * If model doesn't exists. |
| 282 | 282 | */ |
| 283 | - if (! $relationModel) { |
|
| 283 | + if ( ! $relationModel) { |
|
| 284 | 284 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
| 285 | 285 | } |
| 286 | 286 | |
@@ -417,7 +417,7 @@ discard block |
||
| 417 | 417 | $model = $this->model->lockForUpdate()->find($value); |
| 418 | 418 | $model ? $model->update($data) : 0; |
| 419 | 419 | } else { |
| 420 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
| 420 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) use ($data) { |
|
| 421 | 421 | $model->update($data); |
| 422 | 422 | }); |
| 423 | 423 | } |
@@ -434,17 +434,17 @@ discard block |
||
| 434 | 434 | public function delete($value, $attribute = 'id') |
| 435 | 435 | { |
| 436 | 436 | if ($attribute == 'id') { |
| 437 | - \DB::transaction(function () use ($value) { |
|
| 437 | + \DB::transaction(function() use ($value) { |
|
| 438 | 438 | $model = $this->model->lockForUpdate()->find($value); |
| 439 | - if (! $model) { |
|
| 439 | + if ( ! $model) { |
|
| 440 | 440 | \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | $model->delete(); |
| 444 | 444 | }); |
| 445 | 445 | } else { |
| 446 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 447 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 446 | + \DB::transaction(function() use ($value, $attribute) { |
|
| 447 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
| 448 | 448 | $model->delete(); |
| 449 | 449 | }); |
| 450 | 450 | }); |
@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | { |
| 533 | 533 | $model = $this->model->onlyTrashed()->find($id); |
| 534 | 534 | |
| 535 | - if (! $model) { |
|
| 535 | + if ( ! $model) { |
|
| 536 | 536 | \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
| 537 | 537 | } |
| 538 | 538 | |
@@ -615,7 +615,7 @@ discard block |
||
| 615 | 615 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
| 616 | 616 | $path = explode('->', $value); |
| 617 | 617 | $field = array_shift($path); |
| 618 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 618 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
| 619 | 619 | return '"'.$part.'"'; |
| 620 | 620 | })->implode('.')); |
| 621 | 621 | |
@@ -7,254 +7,254 @@ |
||
| 7 | 7 | |
| 8 | 8 | class GenerateDoc extends Command |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The name and signature of the console command. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $signature = 'doc:generate'; |
|
| 10 | + /** |
|
| 11 | + * The name and signature of the console command. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $signature = 'doc:generate'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The console command description. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $description = 'Generate api documentation'; |
|
| 17 | + /** |
|
| 18 | + * The console command description. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $description = 'Generate api documentation'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Init new object. |
|
| 26 | - * |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function __construct() |
|
| 30 | - { |
|
| 31 | - parent::__construct(); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Init new object. |
|
| 26 | + * |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function __construct() |
|
| 30 | + { |
|
| 31 | + parent::__construct(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Execute the console command. |
|
| 36 | - * |
|
| 37 | - * @return mixed |
|
| 38 | - */ |
|
| 39 | - public function handle() |
|
| 40 | - { |
|
| 41 | - $docData = []; |
|
| 42 | - $docData['models'] = []; |
|
| 43 | - $routes = $this->getRoutes(); |
|
| 44 | - foreach ($routes as $route) { |
|
| 45 | - if ($route) { |
|
| 46 | - $actoinArray = explode('@', $route['action']); |
|
| 47 | - if (Arr::get($actoinArray, 1, false)) { |
|
| 48 | - $controller = $actoinArray[0]; |
|
| 49 | - $method = $actoinArray[1]; |
|
| 50 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 34 | + /** |
|
| 35 | + * Execute the console command. |
|
| 36 | + * |
|
| 37 | + * @return mixed |
|
| 38 | + */ |
|
| 39 | + public function handle() |
|
| 40 | + { |
|
| 41 | + $docData = []; |
|
| 42 | + $docData['models'] = []; |
|
| 43 | + $routes = $this->getRoutes(); |
|
| 44 | + foreach ($routes as $route) { |
|
| 45 | + if ($route) { |
|
| 46 | + $actoinArray = explode('@', $route['action']); |
|
| 47 | + if (Arr::get($actoinArray, 1, false)) { |
|
| 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 = Arr::get($classProperties, 'skipLoginCheck', false); |
|
| 56 | - $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
| 52 | + $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | + $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
| 56 | + $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
| 57 | 57 | |
| 58 | - $this->processDocBlock($route, $reflectionMethod); |
|
| 59 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 60 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 58 | + $this->processDocBlock($route, $reflectionMethod); |
|
| 59 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 60 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 61 | 61 | |
| 62 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 62 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 63 | 63 | |
| 64 | - preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
| 65 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
| 64 | + preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
| 65 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
| 66 | 66 | |
| 67 | - $this->getModels($classProperties['model'], $docData); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - } |
|
| 67 | + $this->getModels($classProperties['model'], $docData); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $docData['errors'] = $this->getErrors(); |
|
| 73 | - $docData['reports'] = \Core::reports()->all(); |
|
| 74 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
| 75 | - } |
|
| 72 | + $docData['errors'] = $this->getErrors(); |
|
| 73 | + $docData['reports'] = \Core::reports()->all(); |
|
| 74 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Get list of all registered routes. |
|
| 79 | - * |
|
| 80 | - * @return collection |
|
| 81 | - */ |
|
| 82 | - protected function getRoutes() |
|
| 83 | - { |
|
| 84 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 85 | - if (strpos($route->uri(), 'api/') !== false) { |
|
| 86 | - return [ |
|
| 87 | - 'method' => $route->methods()[0], |
|
| 88 | - 'uri' => $route->uri(), |
|
| 89 | - 'action' => $route->getActionName(), |
|
| 90 | - 'prefix' => $route->getPrefix() |
|
| 91 | - ]; |
|
| 92 | - } |
|
| 93 | - return false; |
|
| 94 | - })->all(); |
|
| 95 | - } |
|
| 77 | + /** |
|
| 78 | + * Get list of all registered routes. |
|
| 79 | + * |
|
| 80 | + * @return collection |
|
| 81 | + */ |
|
| 82 | + protected function getRoutes() |
|
| 83 | + { |
|
| 84 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
| 85 | + if (strpos($route->uri(), 'api/') !== false) { |
|
| 86 | + return [ |
|
| 87 | + 'method' => $route->methods()[0], |
|
| 88 | + 'uri' => $route->uri(), |
|
| 89 | + 'action' => $route->getActionName(), |
|
| 90 | + 'prefix' => $route->getPrefix() |
|
| 91 | + ]; |
|
| 92 | + } |
|
| 93 | + return false; |
|
| 94 | + })->all(); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Generate headers for the given route. |
|
| 99 | - * |
|
| 100 | - * @param array &$route |
|
| 101 | - * @param string $method |
|
| 102 | - * @param array $skipLoginCheck |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 106 | - { |
|
| 107 | - $route['headers'] = [ |
|
| 108 | - 'Accept' => 'application/json', |
|
| 109 | - 'Content-Type' => 'application/json', |
|
| 110 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 111 | - 'time-zone' => 'Your locale time zone', |
|
| 112 | - ]; |
|
| 97 | + /** |
|
| 98 | + * Generate headers for the given route. |
|
| 99 | + * |
|
| 100 | + * @param array &$route |
|
| 101 | + * @param string $method |
|
| 102 | + * @param array $skipLoginCheck |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 106 | + { |
|
| 107 | + $route['headers'] = [ |
|
| 108 | + 'Accept' => 'application/json', |
|
| 109 | + 'Content-Type' => 'application/json', |
|
| 110 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 111 | + 'time-zone' => 'Your locale time zone', |
|
| 112 | + ]; |
|
| 113 | 113 | |
| 114 | 114 | |
| 115 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
| 116 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 117 | - } |
|
| 118 | - } |
|
| 115 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
| 116 | + $route['headers']['Authorization'] = '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 \ReflectionMethod $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 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 135 | - foreach ($params as $param) { |
|
| 136 | - $name = $param->getVariableName(); |
|
| 137 | - if ($name !== 'request') { |
|
| 138 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - } |
|
| 120 | + /** |
|
| 121 | + * Generate description and params for the given route |
|
| 122 | + * based on the docblock. |
|
| 123 | + * |
|
| 124 | + * @param array &$route |
|
| 125 | + * @param \ReflectionMethod $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 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 135 | + foreach ($params as $param) { |
|
| 136 | + $name = $param->getVariableName(); |
|
| 137 | + if ($name !== 'request') { |
|
| 138 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Generate post body for the given route. |
|
| 145 | - * |
|
| 146 | - * @param array &$route |
|
| 147 | - * @param \ReflectionMethod $reflectionMethod |
|
| 148 | - * @param array $validationRules |
|
| 149 | - * @return void |
|
| 150 | - */ |
|
| 151 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 152 | - { |
|
| 153 | - if ($route['method'] == 'POST') { |
|
| 154 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 143 | + /** |
|
| 144 | + * Generate post body for the given route. |
|
| 145 | + * |
|
| 146 | + * @param array &$route |
|
| 147 | + * @param \ReflectionMethod $reflectionMethod |
|
| 148 | + * @param array $validationRules |
|
| 149 | + * @return void |
|
| 150 | + */ |
|
| 151 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 152 | + { |
|
| 153 | + if ($route['method'] == 'POST') { |
|
| 154 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 155 | 155 | |
| 156 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 157 | - if (count($match)) { |
|
| 158 | - if ($match[1] == '$this->validationRules') { |
|
| 159 | - $route['body'] = $validationRules; |
|
| 160 | - } else { |
|
| 161 | - $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 162 | - } |
|
| 156 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 157 | + if (count($match)) { |
|
| 158 | + if ($match[1] == '$this->validationRules') { |
|
| 159 | + $route['body'] = $validationRules; |
|
| 160 | + } else { |
|
| 161 | + $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - foreach ($route['body'] as &$rule) { |
|
| 165 | - if (strpos($rule, 'unique')) { |
|
| 166 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 167 | - } elseif (strpos($rule, 'exists')) { |
|
| 168 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - } else { |
|
| 172 | - $route['body'] = 'conditions'; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - } |
|
| 164 | + foreach ($route['body'] as &$rule) { |
|
| 165 | + if (strpos($rule, 'unique')) { |
|
| 166 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 167 | + } elseif (strpos($rule, 'exists')) { |
|
| 168 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + } else { |
|
| 172 | + $route['body'] = 'conditions'; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Generate application errors. |
|
| 179 | - * |
|
| 180 | - * @return array |
|
| 181 | - */ |
|
| 182 | - protected function getErrors() |
|
| 183 | - { |
|
| 184 | - $errors = []; |
|
| 185 | - $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
| 186 | - foreach ($reflectionClass->getMethods() as $method) { |
|
| 187 | - $methodName = $method->name; |
|
| 188 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 189 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 177 | + /** |
|
| 178 | + * Generate application errors. |
|
| 179 | + * |
|
| 180 | + * @return array |
|
| 181 | + */ |
|
| 182 | + protected function getErrors() |
|
| 183 | + { |
|
| 184 | + $errors = []; |
|
| 185 | + $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
| 186 | + foreach ($reflectionClass->getMethods() as $method) { |
|
| 187 | + $methodName = $method->name; |
|
| 188 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 189 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 190 | 190 | |
| 191 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 191 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 192 | 192 | |
| 193 | - if (count($match)) { |
|
| 194 | - $errors[$match[1]][] = $methodName; |
|
| 195 | - } |
|
| 196 | - } |
|
| 193 | + if (count($match)) { |
|
| 194 | + $errors[$match[1]][] = $methodName; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - return $errors; |
|
| 199 | - } |
|
| 198 | + return $errors; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * Get the given method body code. |
|
| 203 | - * |
|
| 204 | - * @param object $reflectionMethod |
|
| 205 | - * @return string |
|
| 206 | - */ |
|
| 207 | - protected function getMethodBody($reflectionMethod) |
|
| 208 | - { |
|
| 209 | - $filename = $reflectionMethod->getFileName(); |
|
| 210 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 211 | - $end_line = $reflectionMethod->getEndLine(); |
|
| 212 | - $length = $end_line - $start_line; |
|
| 213 | - $source = file($filename); |
|
| 214 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
| 215 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 201 | + /** |
|
| 202 | + * Get the given method body code. |
|
| 203 | + * |
|
| 204 | + * @param object $reflectionMethod |
|
| 205 | + * @return string |
|
| 206 | + */ |
|
| 207 | + protected function getMethodBody($reflectionMethod) |
|
| 208 | + { |
|
| 209 | + $filename = $reflectionMethod->getFileName(); |
|
| 210 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 211 | + $end_line = $reflectionMethod->getEndLine(); |
|
| 212 | + $length = $end_line - $start_line; |
|
| 213 | + $source = file($filename); |
|
| 214 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
| 215 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 216 | 216 | |
| 217 | - return $body; |
|
| 218 | - } |
|
| 217 | + return $body; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Get example object of all availble models. |
|
| 222 | - * |
|
| 223 | - * @param string $modelName |
|
| 224 | - * @param array $docData |
|
| 225 | - * @return string |
|
| 226 | - */ |
|
| 227 | - protected function getModels($modelName, &$docData) |
|
| 228 | - { |
|
| 229 | - if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
| 230 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 231 | - $model = factory($modelClass)->make(); |
|
| 232 | - $modelArr = $model->toArray(); |
|
| 220 | + /** |
|
| 221 | + * Get example object of all availble models. |
|
| 222 | + * |
|
| 223 | + * @param string $modelName |
|
| 224 | + * @param array $docData |
|
| 225 | + * @return string |
|
| 226 | + */ |
|
| 227 | + protected function getModels($modelName, &$docData) |
|
| 228 | + { |
|
| 229 | + if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
| 230 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 231 | + $model = factory($modelClass)->make(); |
|
| 232 | + $modelArr = $model->toArray(); |
|
| 233 | 233 | |
| 234 | - if ($model->trans && ! $model->trans->count()) { |
|
| 235 | - $modelArr['trans'] = [ |
|
| 236 | - 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 237 | - ]; |
|
| 238 | - } |
|
| 234 | + if ($model->trans && ! $model->trans->count()) { |
|
| 235 | + $modelArr['trans'] = [ |
|
| 236 | + 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 237 | + ]; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 241 | - } |
|
| 242 | - } |
|
| 240 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * Get the route response object type. |
|
| 246 | - * |
|
| 247 | - * @param string $modelName |
|
| 248 | - * @param string $method |
|
| 249 | - * @param string $returnDocBlock |
|
| 250 | - * @return array |
|
| 251 | - */ |
|
| 252 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 253 | - { |
|
| 254 | - $config = \CoreConfig::getConfig(); |
|
| 255 | - $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
| 256 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 244 | + /** |
|
| 245 | + * Get the route response object type. |
|
| 246 | + * |
|
| 247 | + * @param string $modelName |
|
| 248 | + * @param string $method |
|
| 249 | + * @param string $returnDocBlock |
|
| 250 | + * @return array |
|
| 251 | + */ |
|
| 252 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 253 | + { |
|
| 254 | + $config = \CoreConfig::getConfig(); |
|
| 255 | + $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
| 256 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 257 | 257 | |
| 258 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 259 | - } |
|
| 258 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 259 | + } |
|
| 260 | 260 | } |
@@ -2,15 +2,15 @@ |
||
| 2 | 2 | |
| 3 | 3 | interface BaseFactoryInterface |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Construct the repository class name based on |
|
| 7 | - * the method name called, search in the |
|
| 8 | - * given namespaces for the class and |
|
| 9 | - * return an instance. |
|
| 10 | - * |
|
| 11 | - * @param string $name the called method name |
|
| 12 | - * @param array $arguments the method arguments |
|
| 13 | - * @return object |
|
| 14 | - */ |
|
| 15 | - public function __call($name, $arguments); |
|
| 5 | + /** |
|
| 6 | + * Construct the repository class name based on |
|
| 7 | + * the method name called, search in the |
|
| 8 | + * given namespaces for the class and |
|
| 9 | + * return an instance. |
|
| 10 | + * |
|
| 11 | + * @param string $name the called method name |
|
| 12 | + * @param array $arguments the method arguments |
|
| 13 | + * @return object |
|
| 14 | + */ |
|
| 15 | + public function __call($name, $arguments); |
|
| 16 | 16 | } |