From 2743032d3d0a8ce42d8df19a2bcb8095bc506867 Mon Sep 17 00:00:00 2001 From: s-damian Date: Sun, 13 Oct 2024 11:23:34 +0300 Subject: [PATCH] Optimize Code --- README.md | 88 ++++++++++++++++++++++----------------- src/HtmlRenderer.php | 10 ++--- src/Pagination.php | 78 +++++++++++++++++----------------- src/RendererGenerator.php | 21 +++++++--- 4 files changed, 108 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index d5d55ed..f80a191 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

-# Laravel Pagination for manual SELECT queries +# Laravel Pagination for Manual SELECT Queries [![Tests](https://github.com/s-damian/laravel-man-pagination/actions/workflows/tests.yml/badge.svg)](https://github.com/s-damian/laravel-man-pagination/actions/workflows/tests.yml) [![Static analysis](https://github.com/s-damian/laravel-man-pagination/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/s-damian/laravel-man-pagination/actions/workflows/static-analysis.yml) @@ -14,7 +14,7 @@ ## Laravel Manual Pagination - Laravel Man Pagination -### Introduction - Laravel Man Pagination package +### Introduction - Laravel Man Pagination This package is particularly useful for implementing pagination with manual SELECT queries using `DB::select()`. @@ -24,11 +24,21 @@ With this pagination, you will have no limit in a Laravel project to manage pagi This pagination also allows you to generate a **per page**. This will generate a form HTML tag with a select HTML tag and clickable options. -> Paginate easily without limit 🚀 +### Key Features + +- Simple Laravel library for **pagination**. +- Customizable **per page** options. +- Support for multiple **languages**. +- Compatible with **Bootstrap 5**. + +### Basic Example + +> Paginate easily without limits 🚀 + ```php paginate($totalElements); // $totalElements: result of an SQL COUNT query @@ -45,25 +55,26 @@ $offset = $pagination->offset(); ### Author -This package is developed by [Stephen Damian](https://github.com/s-damian) +This package is developed by [Stephen Damian](https://github.com/s-damian). -> ✨ If you find this package useful, please **star it** on my GitHub repository +> ✨ If you find this package useful, please **star it** on the GitHub repository. ### Requirements -* PHP 8.0 || 8.1 || 8.2 || 8.3 -* Laravel 8 || 9 || 10 || 11 +- PHP 8.0 || 8.1 || 8.2 || 8.3 +- Laravel 8 || 9 || 10 || 11 ## Summary -* [Installation](#installation) -* [Customization with "vendor:publish"](#customization-with-vendorpublish) -* [Pagination instance methods](#pagination-instance-methods) -* [Examples](#examples) -* [Differences with Laravel integrated pagination](#differences-with-Laravel-integrated-pagination) -* [Support](#support) -* [License](#license) +- [Installation](#installation) +- [Customization with "vendor:publish"](#customization-with-vendorpublish) +- [Pagination instance methods](#pagination-instance-methods) +- [Examples](#examples) +- [Instance Options](#instance-options) +- [Differences with Laravel integrated pagination](#differences-with-Laravel-integrated-pagination) +- [Support](#support) +- [License](#license) ## Installation @@ -75,7 +86,7 @@ composer require s-damian/laravel-man-pagination ``` -## Customization with "vendor:publish" +## Customization With "vendor:publish" ### Custom Config and Lang and CSS @@ -87,15 +98,13 @@ php artisan vendor:publish --provider="SDamian\LaravelManPagination\ManPaginatio The `vendor:publish` command will generate these files: -* `config/man-pagination.php` - -* `lang/vendor/man-pagination/{lang}/pagination.php` - -* `public/vendor/man-pagination/css/pagination.css` +- `config/man-pagination.php` +- `lang/vendor/man-pagination/{lang}/pagination.php` +- `public/vendor/man-pagination/css/pagination.css` You can of course customize these files. -### "vendor:publish" with "--tag" argument +### "vendor:publish" With "--tag" Argument Publish only `config` file: @@ -123,7 +132,7 @@ php artisan vendor:publish --provider="SDamian\LaravelManPagination\ManPaginatio | void | __construct(array $options = []) | Constructor. | | void | paginate(int $total) | (To use in the Controller) Activate the pagination. | | null or int | limit() | (To use in the Controller) LIMIT: Number of items to retrieve. | -| null or int | offset() | (To use in the Controller) OFFSET: From where start the LIMIT. | +| null or int | offset() | (To use in the Controller) OFFSET: Starting point for the LIMIT. | | int | total() | Determine the total number of matching items in the data store. | | int | count() | Get the number of items for the current page. | | int | firstItem() | Get the result number of the first item in the results. | @@ -149,13 +158,15 @@ php artisan vendor:publish --provider="SDamian\LaravelManPagination\ManPaginatio ## Examples -### Concrete example with a manual SELECT query "DB::select" +### Concrete Example With a Manual SELECT Query "DB::select" When doing "complex" SQL queries, sometimes you prefer to do it without Eloquent. Here is an example of SQL query where this library is really useful: ```php +use SDamian\LaravelManPagination\Pagination; + $pagination = new Pagination(); $pagination->paginate($total); @@ -220,7 +231,7 @@ $ordersAndInvoices = DB::select(' [![Laravel Man Pagination](https://raw.githubusercontent.com/s-damian/medias/main/packages/laravel-man-pagination-example.webp)](https://github.com/s-damian/larasort) -### Simple example with a manual SELECT query "DB::select" +### Simple Example With a Manual SELECT Query "DB::select" Here is a simple example, following the MVC pattern, of how to use this library: @@ -283,7 +294,7 @@ class CustomerController extends Controller ``` -### Simple example of Controller with Eloquent +### Simple Example of Controller With Eloquent This is for example only. Concretely, using Eloquent, you don't need this library. Because You can use Eloquent's **paginate** method. @@ -316,11 +327,13 @@ class CustomerController extends Controller ``` -## Add argument(s) to the instance +## Instance Options ```php 50]); // Is 15 by default @@ -344,37 +357,34 @@ $pagination = new Pagination(['per_page_name' => 'per_page']); // The "per page" name is by default "pp". // To change the CSS style of the pagination (to another CSS class as default): -$pagination = new Pagination(['css_class_p' => 'name-css-class-of-pagintion']); +$pagination = new Pagination(['css_class_p' => 'name-css-class-of-pagination']); // The CSS class name is by default "pagination". // To change the CSS style of the pagination active (to another CSS class as default): -$pagination = new Pagination(['css_class_link_active' => 'name-css-class-of-pagintion']); +$pagination = new Pagination(['css_class_link_active' => 'name-css-class-of-pagination']); // The active CSS class name is by default "active". -// To change the CSS style of a per page (select) (to another id id as default): +// To change the CSS style of a per page (select) (to another id as default): $pagination = new Pagination(['css_id_pp' => 'name-css-id-of-per-page-form']); -// The CSS ID name is by default "per-page-form". +// The CSS ID name is by default "per-page-form". ``` -## Differences with Laravel integrated pagination +## Differences With Laravel Integrated Pagination In this pagination, I tried to keep the conventions and the behavior of the pagination integrated in Laravel. -### I added extra "security": +### I Added Extra "Security": If for example there are only 8 pages, and in the URL the visitor tries to go to page 9 (or to a page after page 9): -* The `onLastPage()` method will return `false` (whereas with the pagination integrated in Laravel, it returns true). - -* The `currentPage()` method will return `1` (whereas with the pagination integrated in Laravel, it returns it returns the page in the URL). +- The `onLastPage()` method will return `false` (whereas with the pagination integrated in Laravel, it returns true). +- The `currentPage()` method will return `1` (whereas with the pagination integrated in Laravel, it returns it returns the page in the URL). ## Support -### Bugs and security Vulnerabilities - -If you discover a bug or a security vulnerability, please send a message to Stephen. Thank you. +If you discover a **bug** or a **security vulnerability**, please send a message to Stephen. Thank you. All bugs and all security vulnerabilities will be promptly addressed. diff --git a/src/HtmlRenderer.php b/src/HtmlRenderer.php index 8634cd3..54c8682 100644 --- a/src/HtmlRenderer.php +++ b/src/HtmlRenderer.php @@ -18,14 +18,14 @@ protected function open(): string { $html = ''; - $html .= '