├── .editorconfig ├── .idea ├── .gitignore ├── PresenterFilter.iml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── modules.xml ├── php.xml └── vcs.xml ├── Examples ├── Filter │ ├── Controllers │ │ └── PostController.php │ ├── Filters │ │ └── PostFilter.php │ └── Models │ │ └── Post.php └── Presenter │ ├── Models │ └── User.php │ ├── Resources │ └── UserResource.php │ ├── presenter │ └── UserPresenter.php │ └── views │ └── index.blade.php ├── LICENSE.md ├── README.md ├── composer.json ├── config └── package-modules.php └── src ├── Commands ├── FilterMake.php ├── PresenterMake.php └── stubs │ ├── Filters │ ├── filterImportClass&Trait.stub │ ├── filterImportNamespace.stub │ └── filters.stub │ └── Presenter │ ├── presenter.stub │ ├── presenterImportClass&Trait.stub │ └── presenterImportNamespace.stub ├── Filters └── contracts │ ├── Filterable.php │ └── QueryFilter.php ├── Presenter └── Contracts │ ├── PresentAble.php │ └── Presenter.php └── PresenterFilterServiceProvider.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = false 9 | max_line_length = 120 10 | tab_width = 4 11 | 12 | [{*.ctp,*.hphp,*.inc,*.module,*.php,*.php4,*.php5,*.phtml}] 13 | max_line_length = 80 14 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/PresenterFilter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/php.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Examples/Filter/Controllers/PostController.php: -------------------------------------------------------------------------------- 1 | get(); 11 | } 12 | } -------------------------------------------------------------------------------- /Examples/Filter/Filters/PostFilter.php: -------------------------------------------------------------------------------- 1 | builder->where('title','LIKE',"%$value%"); 10 | } 11 | } -------------------------------------------------------------------------------- /Examples/Filter/Models/Post.php: -------------------------------------------------------------------------------- 1 | $this->id, 16 | 'full_name'=>$this->present()->full_name, 17 | 'email'=>$this->email 18 | ]; 19 | } 20 | } -------------------------------------------------------------------------------- /Examples/Presenter/presenter/UserPresenter.php: -------------------------------------------------------------------------------- 1 | entity->name." ".$this->entity->family; 8 | } 9 | } -------------------------------------------------------------------------------- /Examples/Presenter/views/index.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 | @foreach($users as $user) 12 |

FullName : {{$user->present()->full_name}}

13 | @endforeach 14 | 15 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [mr.hamze00@gmail.com](mailto:mr.hamze00@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Presenter-Filter Laravel 2 | 3 | ## Introduction 4 | The package allows you to create two types of classes: filter class and presenter class 5 | 6 | ### Filter 7 | This package allows you to easily handle database filtering through query strings. 8 | 9 | example: /users?status=1&name='kami' 10 | ### Presenter 11 | Sometimes our models get too fat, and that can make our development a lot harder. 12 | In this case, we use a second class that has the same function as the model and is used as a second model, and the Harrow method can be included in this class. 13 | 14 | ## Installation 15 | you can install the package via composer: 16 | ```bash 17 | composer require mhamzeh/presenter-filter 18 | ``` 19 | 20 | You Must by publishing configuration by issuing following artisan command ```php artisan vendor:publish```. 21 | 22 | ## Usage 23 | You have access to two commands and you can use them to create your own filter and presenter classes 24 | #### make:filter command 25 | You can use the following command to create a new filter. 26 | 27 | ```php artisan make:filter UserFilter``` 28 | 29 | This will create a new filter in the app/Filters directory. 30 | 31 | options: 32 | 33 | 1-You can add the model to the command 34 | 35 | ‍‍‍‍‍‍```php artisan make:filter UserFilter --model=User``` 36 | 37 | Used by default Models folder If you have saved models elsewhere, change the config Modules of this folder 38 | 39 | #### make:presenter Command 40 | You can use the following command to create a new Presenter 41 | 42 | ```php artisan make:presenter UserPresenter``` 43 | 44 | options: 45 | 46 | 1- You can add the model to command 47 | 48 | ```php artisan make:presenter UserPresenter --model=User``` 49 | 50 | ## Example With Filter 51 | Let's say you want to use filterable on User model. You will have to create the filter class App/Filters/PostFilter.php (```php artisan make:filter PostFilter --model=Post```) 52 | 53 | If you use the --model option, filterable will be added directly to the model 54 | 55 | ```php 56 | builder->where('name','LIKE',"%$value%"); 65 | } 66 | } 67 | ``` 68 | 69 | Now you need to add local scope to your model if you have not used the --model option: 70 | ```php 71 | use mhamzeh\packageFp\Filters\contracts\Filterable; 72 | ... 73 | class User extends Model 74 | { 75 | use Filterable; 76 | ... 77 | } 78 | ``` 79 | 80 | Finaly, call the scope in controller like so: 81 | 82 | ```php 83 | use App\Filters\UserFilter; 84 | ... 85 | public function index() 86 | { 87 | return User::filters(new UserFilter())->paginate(); 88 | } 89 | ``` 90 | 91 | 92 | ## Example With Presenter 93 | Let's say you want to use Presentable And introduce the presenter class on User model. You will have to create the filter class App/Presenter/UserPresenter.php (```php artisan make:presenter UserPresenter --model=User```) 94 | 95 | If you use the --model option, Presentable and presenter class will be added directly to the model 96 | 97 | ```php 98 | entity->name ." ".$this->entity->full_name; 107 | } 108 | } 109 | ``` 110 | Now you need to add local scope to your model if you have not used the --model option: 111 | ```php 112 | use mhamzeh\packageFp\Presenter\Contracts\Presentable; 113 | ... 114 | class User extends Model 115 | { 116 | use Presentable; 117 | protected $presenter = UserPresenter::class; 118 | 119 | ... 120 | } 121 | ``` 122 | Finally you can use this method in your [Blade](https://laravel.com/docs/8.x/blade) or [Api Resources](https://laravel.com/docs/8.x/eloquent-resources) or Controller 123 | for example [Blade](https://laravel.com/docs/8.x/blade): 124 | ```php 125 | @foreach($users as $user) 126 |
127 |

fullname : $user->present()->full_name

128 |
129 | @endforeach 130 | ``` 131 | in [Api Resources](https://laravel.com/docs/8.x/eloquent-resources): 132 | ```php 133 | class UserResource extends JsonResource 134 | { 135 | /** 136 | * Transform the resource into an array. 137 | * 138 | * @param \Illuminate\Http\Request $request 139 | * @return array 140 | */ 141 | public function toArray($request) 142 | { 143 | return [ 144 | 'id'=>$this->id, 145 | 'full_name'=> $this->present()->full_name, 146 | 'email'=>$this->email 147 | ]; 148 | } 149 | } 150 | ``` 151 | 152 | 153 | 154 | 155 | 156 | 157 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mhamzeh/presenter-filter", 3 | "type": "library", 4 | "license": "MIT", 5 | "keywords": [ 6 | "mohammad Hamzeh", 7 | "laravel-presenter-filter" 8 | ], 9 | "authors": [ 10 | { 11 | "name": "Mohammad Hamzeh", 12 | "email": "mr.hamze00@gmail.com" 13 | } 14 | ], 15 | "homepage": "https://github.com/mohammadHamzeh/laravel-presenter-filter", 16 | "autoload": { 17 | "psr-4": { 18 | "mhamzeh\\packageFp\\": "src/" 19 | } 20 | }, 21 | "require": { 22 | "php": "^7.3" 23 | }, 24 | "extra": { 25 | "laravel": { 26 | "providers": [ 27 | "mhamzeh\\packageFp\\PresenterFilterServiceProvider" 28 | ] 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /config/package-modules.php: -------------------------------------------------------------------------------- 1 | "Models/" 4 | ]; -------------------------------------------------------------------------------- /src/Commands/FilterMake.php: -------------------------------------------------------------------------------- 1 | file = $file; 45 | } 46 | 47 | /** 48 | * Execute the console command. 49 | * 50 | * @return mixed 51 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 52 | */ 53 | public function handle() 54 | { 55 | $name = $this->argument('name'); 56 | $model = $this->option('model'); 57 | 58 | $filterNameSpace = $this->getNamespace($name); 59 | 60 | $this->getNameClass($name); 61 | 62 | $this->classExists(); 63 | 64 | $this->fileExists($filterNameSpace); 65 | 66 | $stub = $this->changeInStub($filterNameSpace); 67 | 68 | if (!is_null($model)) { 69 | try { 70 | $this->updateModel($model, $filterNameSpace); 71 | } catch (FileNotFoundException $e) { 72 | $this->error("Model $model Not Found"); 73 | die(); 74 | } 75 | } 76 | 77 | $this->putFileFilter($filterNameSpace, $stub); 78 | 79 | echo "The Filter $name successFully Created"; 80 | } 81 | 82 | 83 | /** 84 | * get The NameSpace of Name Argument 85 | * the code from core laravel 86 | * @param $name 87 | * @return string 88 | */ 89 | private 90 | function getNamespace(string $name) 91 | { 92 | return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\'); 93 | } 94 | 95 | /** 96 | * get Name Class Filter WithOut Namespace 97 | * @param $name 98 | * 99 | * @return mixed 100 | */ 101 | private function getNameClass($name) 102 | { 103 | $nameClass = explode('\\', $name); 104 | return $this->nameClass = end($nameClass); 105 | } 106 | 107 | /** 108 | * check the file Filter Has Exists if yes return error 109 | * */ 110 | private function classExists() 111 | { 112 | if ($this->file->exists(app_path("Filters/$this->nameClass.php"))) { 113 | $this->error("Class $this->nameClass Has Exists"); 114 | die(); 115 | }; 116 | } 117 | 118 | /** 119 | * check the Folder in $this->namespace has Exists if yes do nothing else create A Directory 120 | * @param $filterNameSpace 121 | * @return bool 122 | */ 123 | private function fileExists($filterNameSpace) 124 | { 125 | if (!file_exists(app_path("Filters/$filterNameSpace"))) 126 | return mkdir(app_path("Filters/$filterNameSpace")); 127 | } 128 | 129 | private function changeInStub($filterNameSpace) 130 | { 131 | $stub = $this->getStub(); 132 | $nameClass = $this->nameClass; 133 | $search = [ 134 | 'namespace' => '{{ namespace }}', 135 | 'class' => '{{ class }}' 136 | ]; 137 | $namespace = $filterNameSpace == "" ? '' : "\\$filterNameSpace"; 138 | return Str::of($stub)->replace($search['namespace'], $this->namespace . $namespace)->replace($search['class'], 139 | $nameClass); 140 | } 141 | 142 | /* 143 | * get the stub for create A new Filter 144 | * */ 145 | private function getStub() 146 | { 147 | return $this->file->get(__DIR__ . "/stubs/Filters/filters.stub"); 148 | } 149 | 150 | /** 151 | *put file filter in App\\Filters 152 | * @param String $filterNameSpace 153 | * @param Stringable $stub 154 | * 155 | * @return bool|int 156 | */ 157 | private function putFileFilter(string $filterNameSpace, \Illuminate\Support\Stringable $stub) 158 | { 159 | $filterNameSpace= str_replace('\\','//',$filterNameSpace); 160 | return $this->file->put(app_path("Filters/$filterNameSpace/$this->nameClass.php"), $stub); 161 | } 162 | 163 | /** 164 | * Update the model => import Filterable and namespace 165 | * @param $model 166 | * @param string $filterNameSpace 167 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 168 | */ 169 | private function updateModel($model, string $filterNameSpace) 170 | { 171 | $modelPath = config('package-modules.modelPath'); 172 | $fullPath = app_path("$modelPath$model.php"); 173 | $originalContent = $this->file->get($fullPath); 174 | 175 | /*import NameSpace*/ 176 | $originalContent = $this->importNameSpaceModel($originalContent,$model); 177 | /*End Import Name Space*/ 178 | 179 | /*import Class*/ 180 | $originalContent = $this->importClassAndTraitInModel($originalContent,$model); 181 | /*End Import Class*/ 182 | 183 | /*updateFile Model */ 184 | $this->file->put($fullPath, $originalContent); 185 | /*End Update file model*/ 186 | 187 | } 188 | 189 | /** 190 | * @param string $originalContent 191 | * @return \Illuminate\Support\Stringable 192 | * @var string $originalContent 193 | * import the NameSpace Filterable to Model 194 | */ 195 | private function importNameSpaceModel(string $originalContent,$model) 196 | { 197 | $search = "use Illuminate\Database\Eloquent\Model;\n"; 198 | if ($model == "User" or $model =='user') 199 | { 200 | $search= "use Illuminate\Foundation\Auth\User as Authenticatable;"; 201 | } 202 | 203 | $setting = [ 204 | 'search' => $search, 205 | 'stubNameSpace' => __DIR__ . "/stubs/Filters/filterImportNamespace.stub", 206 | ]; 207 | $stub = $this->file->get($setting['stubNameSpace']); 208 | $stub = $setting['search'] . $stub; 209 | return Str::of($originalContent)->replace($setting['search'], $stub); 210 | } 211 | 212 | /** 213 | * import the trait Filterable to Model 214 | * @param $originalContent 215 | * 216 | * @return Stringable 217 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 218 | */ 219 | private function importClassAndTraitInModel($originalContent,$model) 220 | { 221 | $search = "Model\n{"; 222 | if ($model == "User" or $model =='user') 223 | { 224 | $search= "Authenticatable\n{"; 225 | } 226 | 227 | $setting = [ 228 | 'stubImportClass' => __DIR__ . "/stubs/Filters/filterImportClass&Trait.stub", 229 | 'search2' => $search 230 | ]; 231 | $stub = $this->file->get($setting['stubImportClass']); 232 | $stub = $setting['search2'] . $stub; 233 | $originalContent2 = Str::of($originalContent)->replace($setting['search2'], $stub); 234 | return $originalContent2; 235 | } 236 | 237 | /** 238 | * validation for input Arguments 239 | */ 240 | protected function getArguments() 241 | { 242 | return [ 243 | [ 244 | 'name', InputArgument::REQUIRED, 245 | '--model', InputArgument::OPTIONAL 246 | ], 247 | ]; 248 | } 249 | 250 | } 251 | -------------------------------------------------------------------------------- /src/Commands/PresenterMake.php: -------------------------------------------------------------------------------- 1 | file = $file; 49 | } 50 | 51 | /** 52 | * Execute the console command. 53 | * 54 | * @return mixed 55 | */ 56 | public function handle() 57 | { 58 | $name = $this->argument('name'); 59 | $model = $this->option('model'); 60 | /*todo not found model */ 61 | $PresenterNameSpace = $this->getNamespace($name); 62 | 63 | $this->getNameClass($name); 64 | 65 | $this->classExists($name); 66 | 67 | $this->fileExists($PresenterNameSpace); 68 | 69 | $stub = $this->changeInStub($PresenterNameSpace); 70 | 71 | if (!is_null($model)) { 72 | try { 73 | $this->UpdateModel($model, $PresenterNameSpace); 74 | } catch (FileNotFoundException $e) { 75 | $this->error('Model Not Found'); 76 | die(); 77 | } 78 | } 79 | $this->putFilePresenter($PresenterNameSpace, $stub); 80 | 81 | 82 | echo "The Presenter $name successFully Created"; 83 | } 84 | 85 | /** 86 | * validation for input Arguments 87 | */ 88 | protected 89 | function getArguments() 90 | { 91 | return [ 92 | [ 93 | 'name', InputArgument::REQUIRED, 94 | '--model', InputArgument::OPTIONAL 95 | ], 96 | ]; 97 | } 98 | 99 | /** 100 | * Get The File Stub 101 | */ 102 | private 103 | function getStub() 104 | { 105 | return $this->file->get(__DIR__.'/stubs/Presenter/presenter.stub'); 106 | } 107 | 108 | /** 109 | * get The NameSpace of Name Argument 110 | * the code from core laravel 111 | * @param $name 112 | * @return string 113 | */ 114 | private 115 | function getNamespace(string $name) 116 | { 117 | return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\'); 118 | } 119 | 120 | /** 121 | * check the file Presenter Has Exists if yes return error 122 | * @param $name 123 | * */ 124 | private function classExists($name) 125 | { 126 | if ($this->file->exists(app_path("Presenter/$name.php"))) { 127 | $this->error("Class $name Has Exists"); 128 | die(); 129 | }; 130 | } 131 | 132 | /** 133 | * search and replace name Class & namespace in stub and return it 134 | * @param $namespace 135 | * @return \Illuminate\Support\Stringable 136 | */ 137 | private 138 | function changeInStub($namespace) 139 | { 140 | $stub = $this->getStub(); 141 | $nameClass = $this->nameClass; 142 | $settings = 143 | [ 144 | 'search' => '{{ class }}', 145 | 'namespace' => '{{ namespace }}' 146 | ]; 147 | $namespace = $namespace == "" ? '' : "\\$namespace"; 148 | return Str::of($stub)->replace($settings['search'], $nameClass)->replace($settings['namespace'], 149 | $this->namespace . $namespace); 150 | } 151 | 152 | /** 153 | * get Name Class Presenter WithOut Namespace 154 | * @param $name 155 | * 156 | * @return mixed 157 | */ 158 | private 159 | function getNameClass($name) 160 | { 161 | $nameClass = explode('\\', $name); 162 | return $this->nameClass = end($nameClass); 163 | } 164 | 165 | /** 166 | * check the Folder in $this->namespace has Exists if yes do nothing else create A Directory 167 | * @param $PresenterNameSpace 168 | * @return bool 169 | */ 170 | private 171 | function fileExists($PresenterNameSpace) 172 | { 173 | if (!file_exists(app_path("Presenter/$PresenterNameSpace"))) 174 | return mkdir(app_path("Presenter/$PresenterNameSpace")); 175 | } 176 | 177 | /** 178 | * put The file in App\\Presenter\\$namespace\\$file 179 | * @param $PresenterNameSpace 180 | * @param $stub 181 | * @return bool|int 182 | */ 183 | private function putFilePresenter($PresenterNameSpace, $stub) 184 | { 185 | $PresenterNameSpace= str_replace('\\','//',$PresenterNameSpace); 186 | return $this->file->put(app_path("Presenter/$PresenterNameSpace/$this->nameClass.php"), $stub); 187 | } 188 | 189 | /** 190 | * @param $modelName 191 | * @param $PresenterNameSpace 192 | * @throws FileNotFoundException 193 | * import NameSpace Presenter And PresenterClass And Trait Presentable In Model 194 | */ 195 | private 196 | function UpdateModel($modelName, $PresenterNameSpace) 197 | { 198 | $modelPath = config('package-modules.modelPath'); 199 | $fullPath = app_path("$modelPath$modelName.php"); 200 | $originalContent = $this->file->get($fullPath); 201 | 202 | /*import NameSpace*/ 203 | $originalContent = $this->importNameSpaceModel($PresenterNameSpace, $originalContent,$modelName); 204 | /*End Import Name Space*/ 205 | 206 | /*import Class*/ 207 | $originalContent = $this->importClassAndTraitInModel($originalContent,$modelName); 208 | /*End Import Class*/ 209 | 210 | /*updateFile Model */ 211 | $this->file->put($fullPath, $originalContent); 212 | /*End Update file model*/ 213 | } 214 | 215 | /** 216 | * @param $PresenterNameSpace 217 | * @param string $originalContent 218 | * @param $modelName 219 | * @return \Illuminate\Support\Stringable|string 220 | */ 221 | private 222 | function importNameSpaceModel($PresenterNameSpace, string $originalContent,$modelName) 223 | { 224 | $search = "use Illuminate\Database\Eloquent\Model;\n"; 225 | if ($modelName == "User" or $modelName =='user') 226 | { 227 | $search= "use Illuminate\Foundation\Auth\User as Authenticatable;"; 228 | } 229 | 230 | $setting = [ 231 | 'search' =>$search , 232 | 'stubNameSpace' => __DIR__ . "/stubs/Presenter/presenterImportNamespace.stub", 233 | 'namespace' => '{{ namespace }}', 234 | ]; 235 | $stub = $this->file->get($setting['stubNameSpace']); 236 | $namespace = $PresenterNameSpace == '' ? "\\$this->nameClass" : "\\$PresenterNameSpace\\$this->nameClass"; 237 | $stub = Str::of($stub)->replace($setting['namespace'], $this->namespace . "$namespace;"); 238 | $stub = $setting['search'] . $stub; 239 | $originalContent = Str::of($originalContent)->replace($setting['search'], $stub); 240 | return $originalContent; 241 | } 242 | 243 | /** 244 | * @param array $setting 245 | * @param $originalContent 246 | * @return \Illuminate\Support\Stringable 247 | * @throws FileNotFoundException 248 | */ 249 | private 250 | function importClassAndTraitInModel($originalContent,$model): \Illuminate\Support\Stringable 251 | { 252 | $search = "Model\n{"; 253 | if ($model == "User" or $model =='user') 254 | { 255 | $search= "Authenticatable\n{"; 256 | } 257 | $setting = [ 258 | 'stubImportClass' => __DIR__ . "/stubs/Presenter/presenterImportClass&Trait.stub", 259 | 'search2' => $search 260 | ]; 261 | $stub = $this->file->get($setting['stubImportClass']); 262 | $stub = Str::of($stub)->replace('{{ namePresenter }}', $this->nameClass); 263 | $stub = $setting['search2'] . $stub; 264 | $originalContent2 = Str::of($originalContent)->replace($setting['search2'], $stub); 265 | return $originalContent2; 266 | } 267 | 268 | } 269 | -------------------------------------------------------------------------------- /src/Commands/stubs/Filters/filterImportClass&Trait.stub: -------------------------------------------------------------------------------- 1 | 2 | use Filterable; 3 | -------------------------------------------------------------------------------- /src/Commands/stubs/Filters/filterImportNamespace.stub: -------------------------------------------------------------------------------- 1 | use mhamzeh\packageFp\Filters\contracts\Filterable; 2 | -------------------------------------------------------------------------------- /src/Commands/stubs/Filters/filters.stub: -------------------------------------------------------------------------------- 1 | apply($query); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Filters/contracts/QueryFilter.php: -------------------------------------------------------------------------------- 1 | request = Request::capture(); 18 | } 19 | 20 | 21 | public function apply(Builder $builder) 22 | { 23 | $this->builder = $builder; 24 | foreach ($this->filters() as $key => $value) { 25 | if (!method_exists($this, $key)) { 26 | return $this->builder; 27 | } 28 | !empty($value) ? $this->{$key}($value) : $this->{$key}; 29 | } 30 | return $this->builder; 31 | } 32 | 33 | private function filters() 34 | { 35 | return array_filter( 36 | $this->request->all() 37 | ); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Presenter/Contracts/PresentAble.php: -------------------------------------------------------------------------------- 1 | presenter || ! class_exists($this->presenter)) { 14 | throw new \Exception('presenter not found'); 15 | } 16 | 17 | if ( ! $this->presenterInstance) { 18 | $this->presenterInstance = new $this->presenter($this); 19 | } 20 | 21 | return $this->presenterInstance; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Presenter/Contracts/Presenter.php: -------------------------------------------------------------------------------- 1 | entity = $entity; 14 | } 15 | 16 | public function __get($property) 17 | { 18 | if (method_exists($this, $property)) { 19 | return $this->{$property}(); 20 | } 21 | 22 | return $this->entity->{$property}; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/PresenterFilterServiceProvider.php: -------------------------------------------------------------------------------- 1 | commands([ 19 | FilterMake::class, 20 | PresenterMake::class 21 | ]); 22 | $this->publishes([ 23 | __DIR__.'/../config' => config_path('/'), 24 | ]); 25 | } 26 | 27 | public function register() 28 | { 29 | // 30 | } 31 | 32 | 33 | } --------------------------------------------------------------------------------