diff --git a/README.md b/README.md index e3461a8..a121c18 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@

-[![Latest Version on Packagist](https://img.shields.io/packagist/v/coderflexx/laravel-csv.svg?style=flat-square)](https://packagist.org/packages/coderflexx/laravel-csv) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/coderflexx/laravel-csv.svg?style=flat-square)](https://packagist.org/packages/coderflex/laravel-csv) [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/coderflexx/laravel-csv/run-tests?label=tests)](https://github.com/coderflexx/laravel-csv/actions?query=workflow%3Arun-tests+branch%3Amain) [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/coderflexx/laravel-csv/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/coderflexx/laravel-csv/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) -[![Total Downloads](https://img.shields.io/packagist/dt/coderflexx/laravel-csv.svg?style=flat-square)](https://packagist.org/packages/coderflexx/laravel-csv) +[![Total Downloads](https://img.shields.io/packagist/dt/coderflexx/laravel-csv.svg?style=flat-square)](https://packagist.org/packages/coderflex/laravel-csv) - [Introduction](#introduction) @@ -27,7 +27,7 @@ - [License](#license) ## Introduction -__Laravel CSV__ Package is a package created on top of Laravel [livewire](https://laravel-livewire.com) package, and it handles importing thousands of records without any issues. +__Laravel CSV__ Package is a package created on top of Laravel [livewire](https://laravel-livewire.com) for easily handling imports with a simple API. ## Installation @@ -39,20 +39,20 @@ composer require coderflex/laravel-csv ## Configuration -You can publish and run the migrations with: +Publish and run the migrations with: ```bash php artisan vendor:publish --tag="csv-migrations" php artisan migrate ``` -You can publish the config file with: +Publish the config file with: ```bash php artisan vendor:publish --tag="csv-config" ``` -This is the contents of the published config file: +The following is the contents of the published config file: ```php @@ -63,10 +63,8 @@ return [ | Default Layout |-------------------------------------------------------------------------- | - | This package came with multiple layouts to serve your need, and - | currently it supports "tailwindcss" and "bootstrap", by default - | the layout is tailwind. - | currently support: "tailwindcss", "bootstrap" + | This package plans on supporting multiple CSS frameworks. + | Currently, 'tailwindcss' is the default and only supported framework. | */ 'layout' => 'tailwindcss', @@ -76,20 +74,18 @@ return [ | Max Upload File Size |-------------------------------------------------------------------------- | - | This package came with file validation for uploaded files, - | and by default the file should not be greater than 20MB. If - | you wish to increase/decrease this value, you may change the - | value below. - | Note that the value is defined by "KB". + | The default maximumum file size that can be imported by this + | package is 20MB. If you wish to increase/decrease this value, + | change the value in KB below. | */ 'file_upload_size' => 20000, ]; ``` -The `layout` option is for choosing which CSS Framework you are using, currently supports only `tailwindcss`, and we're working on other CSS frameworks to implement in the future. +The `layout` option is for choosing which CSS framework you are using and currently supports only `tailwindcss`. We are working on other CSS frameworks to implement in the future. -The `file_upload_size` is for validation rules, and it helps define the file size of the uploaded files, or. You can define this one from [livewire config](https://github.com/livewire/livewire/blob/master/config/livewire.php#L100) file. +The `file_upload_size` is for validation rules, and it defines the maximum file size of uploaded files. You may also define this value from the [livewire config](https://github.com/livewire/livewire/blob/master/config/livewire.php#L100) file. Optionally, you can publish the views using @@ -97,12 +93,12 @@ Optionally, you can publish the views using php artisan vendor:publish --tag="csv-views" ``` -> Before Using this command, please take a look at this [section](in-tall-stack-project) below. +> Before Using this command, please take a look at this [section](#in-tall-stack-project) below. ## Usage ### CSV Importer Component -Using this package, is really simple, all what you need to do is implementing the component inside your desired file. +Using this package is a breeze. To implmenent the importer in your project, simply include the following component inside a Blade view. ```blade Import ``` -If you want to style it, you can use the `class` attribute, or any attribute you want really +To style the button, use the `class` attribute with Tailwind utility classes. ```blade ``` ### In TALL stack project -If you are using this package in a [TALL Stack]() project, (Tailwindcss, Alpinejs, Laravel, Livewire) All what you need to do is publish the vendor views +If you are using this package in a [TALL Stack](https://tallstack.dev/) project, (Tailwindcss, Alpinejs, Laravel, Livewire) publish the vendor views to include Laravel-CSV in your project. ```bash php artisan vendor:publish --tag="csv-views" ``` -Then compile your assets, to add the additional classes, came with the component. +Then compile your assets. ```bash npm run dev ``` ### In none TALL Stack project -If you are not using the TALL Stack by default, you may use the `csv directives` to add the necessary styles/scripts +If you are not using the TALL Stack, use the `csv directives` to add the necessary styles/scripts. ```blade @@ -170,19 +166,19 @@ If you are not using the TALL Stack by default, you may use the `csv directives` ``` ### Using Queues -This package is using [queues](https://laravel.com/docs/9.x/queues#main-content), under the hood with [PHP Generators](https://www.php.net/manual/en/language.generators.overview.php), to make it works fast and efficient. +This package uses [queues](https://laravel.com/docs/9.x/queues#main-content) under the hood with [PHP Generators](https://www.php.net/manual/en/language.generators.overview.php) to make it fast and efficient. -You need first to create the `batches table` +Create the `batches table` by running ```bash php artisan queue:batches-table ``` -Then run the migration +Then, run the migration. ``` php artisan migrate ``` -After that, you need to set up the queues' configuration. -You may head into [Laravel Queues Documentation](https://laravel.com/docs/9.x/queues#main-content) to learn more. +After that, set up the queues' configuration. +Head to [Laravel Queues Documentation](https://laravel.com/docs/9.x/queues#main-content) to learn more. ## Testing @@ -204,7 +200,7 @@ Please see [CONTRIBUTING](https://github.com/ousid/.github/blob/main/CONTRIBUTIN Please review [our security policy](../../security/policy) on how to report security vulnerabilities. ## Inspiration -This Package Was Inspired by [codecourse](https://codecourse.com) video series, and if you want to learn how this package was created, make sure to take a look at this [video series](https://codecourse.com/subjects/laravel-livewire) +This Package Was Inspired by [codecourse](https://codecourse.com) video series. If you want to learn how this package was created, make sure to take a look at this [video series](https://codecourse.com/subjects/laravel-livewire) ## Credits diff --git a/config/laravel_csv.php b/config/laravel_csv.php index 54aebbb..4576ecd 100644 --- a/config/laravel_csv.php +++ b/config/laravel_csv.php @@ -1,5 +1,3 @@ - 'tailwindcss', @@ -20,11 +16,9 @@ | Max Upload File Size |-------------------------------------------------------------------------- | - | This package came with file validation for uploaded files, - | and by default the file should not be greater than 20MB. If - | you wish to increase/decrease this value, you may change the - | value below. - | Note that the value is defined by "KB". + | The default maximumum file size that can be imported by this + | package is 20MB. If you wish to increase/decrease this value, + | change the value in KB below. | */ 'file_upload_size' => 20000, diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a91953b..fb14673 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 6 paths: - src - config @@ -11,4 +11,8 @@ parameters: checkOctaneCompatibility: true checkModelProperties: true checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + ignoreErrors: + - '#Internal error: Internal error: Target class \[laravel-csv\] does not exist.#' + - '#Internal error: Internal error: Target class \[livewire\] does not exist.#' diff --git a/src/Http/Livewire/HandleImports.php b/src/Http/Livewire/HandleImports.php index c912f05..7715978 100644 --- a/src/Http/Livewire/HandleImports.php +++ b/src/Http/Livewire/HandleImports.php @@ -4,6 +4,9 @@ use function Coderflex\LaravelCsv\csv_view_path; use Coderflex\LaravelCsv\Models\Import; +use Illuminate\Contracts\View\Factory; +use Illuminate\Contracts\View\View; +use Illuminate\Database\Eloquent\Collection; use Livewire\Component; class HandleImports extends Component @@ -16,12 +19,12 @@ class HandleImports extends Component 'imports.refresh' => '$refresh', ]; - public function mount(string $model) + public function mount(string $model): void { $this->model = $model; } - public function getImportsProperty() + public function getImportsProperty(): Collection { /** @var \Illuminate\Foundation\Auth\User */ $user = auth()->user(); @@ -34,7 +37,7 @@ public function getImportsProperty() ->get(); } - public function render() + public function render(): View|Factory { return view( csv_view_path('handle-imports') diff --git a/src/Jobs/ImportCsv.php b/src/Jobs/ImportCsv.php index dc8dada..d89e046 100644 --- a/src/Jobs/ImportCsv.php +++ b/src/Jobs/ImportCsv.php @@ -38,7 +38,7 @@ public function handle() $affectedRows = $this->model::upsert( $this->chunk, ['id'], - collect($this->columns)->diff('id')->keys()->toArray(), + collect($this->columns)->diff(['id'])->keys()->toArray(), ); $this->import->increment('processed_rows', $affectedRows); diff --git a/src/LaravelCsvDirectives.php b/src/LaravelCsvDirectives.php index 61309ec..f1d9463 100644 --- a/src/LaravelCsvDirectives.php +++ b/src/LaravelCsvDirectives.php @@ -4,21 +4,38 @@ class LaravelCsvDirectives { - public static function csvStyles() + /** + * Get CSV Styles + * + * @return string + */ + public static function csvStyles(): string|null { if (config('laravel_csv.layout') == 'tailwindcss') { return self::getTailwindStyle(); } + + return self::getTailwindStyle(); } - public static function csvScripts() + /** + * Get CSV Scripts + * + * @return string + */ + public static function csvScripts(): string { return <<<'HTML' HTML; } - protected static function getTailwindStyle() + /** + * Get Tailwind Style Path + * + * @return string + */ + protected static function getTailwindStyle(): string { return <<<'HTML' diff --git a/src/LaravelCsvServiceProvider.php b/src/LaravelCsvServiceProvider.php index 31fcd8c..eaec972 100644 --- a/src/LaravelCsvServiceProvider.php +++ b/src/LaravelCsvServiceProvider.php @@ -60,7 +60,10 @@ protected function configureComponents(): void */ protected function registerLivewireComponents(): void { + /** @phpstan-ignore-next-line */ Livewire::component('csv-importer', CsvImporter::class); + + /** @phpstan-ignore-next-line */ Livewire::component('handle-imports', HandleImports::class); } diff --git a/src/Utilities/ChunkIterator.php b/src/Utilities/ChunkIterator.php index 5ed7233..1a6012c 100644 --- a/src/Utilities/ChunkIterator.php +++ b/src/Utilities/ChunkIterator.php @@ -2,6 +2,7 @@ namespace Coderflex\LaravelCsv\Utilities; +use Generator; use Iterator; /** @@ -35,8 +36,10 @@ public function __construct(Iterator $iterator, int $chunkSize) /** * Chunk the given data + * + * @return Generator */ - public function get() + public function get(): Generator { $chunk = []; diff --git a/src/helpers.php b/src/helpers.php index 78e4e9e..8cdb1d9 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -9,7 +9,7 @@ * @param string|null $view * @return string */ - function csv_view_path($view): string + function csv_view_path(string|null $view): string { return 'laravel-csv::livewire.'.config('laravel_csv.layout').'.'.$view; }