sikessem/type

Customize Object-Oriented PHP Variable Types

0.0.0 2022-09-13 02:04 UTC

This package is auto-updated.

Last update: 2024-05-13 05:36:33 UTC


README

SIKessEm logo

Customize Object-Oriented PHP Variable Types

This package allows to define variable types or to declare PHP variables from already existing object types.

Installation

Using composer, you can install Type with the following command:

composer require sikessem/type

Usage

Define a variable type:

<?php

namespace App\Types;

use Sikessem\Type\Statement;

class MyType implements Statement {
    public function __construct(protected string $value) {
        $this->setValue($value);
    }

    protected string $value;

    public function setValue(string $value): self {
        $this->value = $value;
        return $this;
    }

    public function getValue(): string {
        return $this->value;
    }

    public function dump(): void {
        exit($this->value);
    }

    // The method to define to parse a value
    public static function parse(mixed $value): self {
        return new self((string)$value);
    }
}

Declare a variable with the defined type:

<?php

use App\Types\MyType;

$myVar = let(MyType::class, 'Hello');
$myVar->dump(); // Exit with the string 'Hello'

License

This library is distributed under the License

Security Reports

Please send any sensitive issue to ske@sikessem.com. Thanks!