承接 pedrokeilerbatistarojo/smartfilter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pedrokeilerbatistarojo/smartfilter

最新稳定版本:v1.0.4

Composer 安装命令:

composer require pedrokeilerbatistarojo/smartfilter

包简介

This Laravel package provides a streamlined solution for filtering and listing data. It accepts an input array, applies customizable filters, and returns a structured JSON response. Ideal for projects requiring dynamic data filtering with minimal setup, the package ensures consistency in API respons

README 文档

README

SmartFilter is a Laravel package designed to provide robust and flexible filtering capabilities for APIs. It allows developers to apply filters, sorting, and pagination to data retrieval seamlessly while returning structured JSON responses.

Installation

  1. Require the package via Composer:

    composer require pedrokeilerbatistarojo/smartfilter
  2. Publish the configuration (optional):

    php artisan vendor:publish --provider="Pedrokeilerbatistarojo\Smartfilter\SmartfilterServiceProvider"
  3. Usage: Include the package in your controller or service to start applying filters.

Features

  • Apply filters with various operators (e.g., like, =, etc.).
  • Select specific columns to retrieve.
  • Include relationships for eager loading.
  • Sort results by any field.
  • Paginate results with customizable parameters.
  • Structured JSON responses with metadata.

Usage Example

Sample Request

GET /api/users?filters[0][0]=name&filters[0][1]=like&filters[0][2]=Owner&columns[]=id&columns[]=name&columns[]=email&includes[]=role&sortField=created_at&sortType=asc&itemsPerPage=8&currentPage=1

Query Params

filters:[["name", "like", "Owner", "and"],["email", "like","owner@example.com", "and"],["name", "like", "Owner", "and", "role"]]
columns:["id", "name", "email"]
includes:["role"]
sortField:created_at
sortType:asc
itemsPerPage:8
currentPage:1

Controller Example

use Pedrokeilerbatistarojo\Smartfilter\Services\FilterService;
use Pedrokeilerbatistarojo\Smartfilter\Helpers\ResponseHelper;
use Illuminate\Http\Request;
use App\Models\User;
use Exception;

class UserController extends Controller
{

    public function __construct(
        private readonly FilterService $filterService
    ){
    }

    /**
     * @throws Exception
     */
    public function __invoke(Request $request)
    {
       try {
          $response = $this->filterService->execute(User::class, $request->all());
          return ResponseHelper::sendResponse($response);
       }
       catch(Exception $ex){
          return ResponseHelper::sendError($ex->getMessage());
       } 
    }
}

Expected JSON Response

{
    "success": true,
    "message": "Search completed successfully",
    "errors": null,
    "payload": {
        "items": [
            {
                "id": 1,
                "name": "Owner",
                "email": "owner@example.com"
            },
            {
                "id": 2,
                "name": "Admin",
                "email": "admin@example.com"
            }
        ],
        "metadata": {
            "currentPage": 1,
            "lastPage": 5,
            "itemsPerPage": 8,
            "total": 40
        },
        "total": 40
    }
}

Testing

  1. Run the test suite:

    php artisan test
  2. Example Test:

    public function test_filter_with_filters(): void
    {
        $filters = [
            ['name', 'like', 'Owner', 'and'],
            ['email', 'like', 'owner@example.com', 'and'],
            ['name', 'like', 'Owner', 'and', 'role']
        ];
    
        $columns = ['id', 'name', 'email'];
        $includes = ['role'];
    
        $params = [
            'filters' => $filters,
            'columns' => $columns,
            'includes' => $includes,
            'sortField' => 'created_at',
            'sortType' => 'asc',
            'itemsPerPage' => 8,
            'currentPage' => 1
        ];
    
        $queryString = http_build_query($params);
        $endpoint = "/api/users?{$queryString}";
    
        $response = $this->get($endpoint);
        $response->assertStatus(200);
        $response->assertJsonStructure([
            'success',
            'message',
            'errors',
            'payload' => [
                'items' => [
                    '*' => [
                        'id',
                        'name',
                        'email'
                    ]
                ],
                'metadata' => [
                    'currentPage',
                    'lastPage',
                    'itemsPerPage',
                    'total'
                ],
                'total'
            ]
        ]);
    }

Contributing

Feel free to fork this repository and submit pull requests. Ensure that all tests pass and maintain code quality standards.

License

SmartFilter is open-source software licensed under the MIT License.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-05