mymedia/php-argument-builder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mymedia/php-argument-builder

Composer 安装命令:

composer require mymedia/php-argument-builder

包简介

Abstract argument builder library

README 文档

README

Master
Build Status
Coverage Status
Quality Status

Argument Builder Library

AbstractArgumentBuilder class is used to build a query string from pre-defined validatable parameters. It can also be used to the reverse so that the application uses only the desired validated parameters. Generates its own property getters and setters and unset via automated magic function.

Installation

First, install the dependency:

$ composer require mymedia/php-argument-builder

Usage examples

Basic usage

AbstractArgumentBuilder

AbstractArgumentBuilder implements ArgumentBuilderInterface which provides only one method: build(). Magic function __call() provides access to getters and setters, and unset, without the need to generate them manually. It also provides us with __toString() function that returns http query string.

$builder
    ->setSearch('foobar')
    ->setFilter('color', 'iridescent')
    ->setFilter('size', 'height', 2)
    ->setFilter('size', 'width', 10);

Using the provided functionality, the presented code will generate query arguments like: http://example.com/?search=foobar&filter[color]=iridescent&filter[size][height]=2&filter[size][width]=10. However this requires additional classes to extend the AbstractArgumentBuilder that we will define in the next section.

Extending AbstractArgumentBuilder

Argument Types

AbstractArgumentBuilder defines following constants, that are used in field validation:

    const ARGUMENT_TYPE_MIXED = 0;
    const ARGUMENT_TYPE_ARGUMENT_BUILDER = 1;
    const ARGUMENT_TYPE_NUMERIC = 2;
    const ARGUMENT_TYPE_ENUM = 3;
    const ARGUMENT_TYPE_BOOLEAN = 4;

Classes

class SearchArgumentBuilder extends AbstractArgumentBuilder
{
    protected $fields = [
        'search' => self::ARGUMENT_TYPE_MIXED,
        'filter' => SearchFilterArgumentBuilder::class,
    ];
}
class SearchFilterArgumentBuilder extends AbstractArgumentBuilder
{
    protected $fields = [
        'color' => self::ARGUMENT_TYPE_MIXED,
        'size' => SearchFilterSizeArgumentConverter::class, 
    ];
}
class SearchFilterSizeArgumentBuilder extends AbstractArgumentBuilder
{
    protected $fields = [
        'height' => self::ARGUMENT_TYPE_MIXED,
        'width' => self::ARGUMENT_TYPE_MIXED,
    ];
}

Field Validation

A simple way to provide field validation, it will fail if the defined condition is not met:

class SearchFilterPriceArgumentBuilder extends AbstractArgumentBuilder
{
    protected function load()
    {
        $this->fields = array(
            'min' => array(
                'type' => self::ARGUMENT_TYPE_MIXED,
                'validator' => function ($value) {
                    return $value >= 0 && $value <= 1000;
                }
            ),
            'max' => array(
                'type' => self::ARGUMENT_TYPE_MIXED,
                'validator' => function ($value) {
                    return $value >= 0 && $value <= 1000;
                }
            ),
        );
    }
}

Code license

You are free to use the code in this repository under the terms of the MIT license. LICENSE contains a copy of this license.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固