decodelabs/gadgets

Useful tools for building PHP libraries

v0.8.0 2022-03-09 11:48 UTC

This package is auto-updated.

Last update: 2022-08-10 11:42:36 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Useful tools for building PHP libraries.

Installation

Install via Composer:

composer require decodelabs/gadgets

PHP version

Please note, the final v1 releases of all Decode Labs libraries will target PHP8 or above.

Current support for earlier versions of PHP will be phased out in the coming months.

Usage

Method chaining

The Then interface for structured method chaining has been moved to its own project, Fluidity.

Constraints

namespace DecodeLabs\Gadgets\Constraint;

interface Disableable
{
    public function isDisabled(): bool;
    public function setDisabled(bool $disabled): Disableable;
}

interface Nullable
{
    public function isNullable(): bool;
    public function setNullable(bool $nullable): Nullable;
}

interface Immutable
{
    public function isImmutable(): bool;
    public function setImmutable(bool $immutable): Immutable;
}

interface Requirable
{
    public function isRequired(): bool;
    public function setRequired(bool $required): Requirable;
}

Add common constraints to your objects.

Sanitizer

Add basic input value sanitizing to your objects by returning a Sanitizer.

use DecodeLabs\Gadgets\Sanitizer;

$test = new class {

    public $value = 'my value';

    public function sanitizeValue(bool $required=true): Sanitizer
    {
        return new Sanitizer($this->value, $required);
    }
}



$myString = $test->sanitizeValue()->asString(); // All good
$myInt = $test->sanitizeValue()->asInt(); // Will try to convert to int

Licensing

Gadgets is licensed under the MIT License. See LICENSE for the full license text.