mymedia/php-argument-builder

Abstract argument builder library

1.1.0 2019-01-22 14:46 UTC

This package is auto-updated.

Last update: 2024-11-23 20:36:57 UTC


README

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.