decorator

Python style decorator for PHP

MIT License

Downloads
57
Stars
11
Committers
3

Bot releases are visible (Hide)

decorator - v6.4.2

Published by sagittaracc 6 months ago

  • Add generic support for method parameters
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
use Sagittaracc\PhpPythonDecorator\modules\validation\core\validators\ArrayOf;

#[T]
class Box
{
    use Decorator;

    #[ArrayOf(T::class)]
    public $items;

    public function addItem(#[T] $item)
    {
        $this->items[] = $item;
    }
}

$box = new Box();
$box(Pen::class); // new Box<Pen>();
call_decorator_func_array([$box, 'addItem'], [new Pencil]); // throws a GenericError
decorator - v6.4.1

Published by sagittaracc 6 months ago

  • Add console support
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\console\core\Console;

class Controller
{
    use Decorator;

    #[Console('hello')]
    function greetingPerson($name)
    {
        return "Hello, $name";
    }
}

in the command line it would be calling for example something like this:

php index.php -c hello --name Yuriy

then in index.php you should read the command and the parameters and after that call it like this:

(new Console('hello'))->setParameters(['name' => 'Yuriy'])->getMethod(Controller::class)->run();
decorator - v6.4.0 Latest Release

Published by sagittaracc 6 months ago

Done generics refactor

decorator - v6.3.0

Published by sagittaracc 6 months ago

Done generics refactor

decorator - v6.2.1

Published by sagittaracc 6 months ago

  • I am still working on Generics
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;

#[T]
class PaymentInfo
{
    use Decorator;

    public string $id;

    public int $amount;

    #[T]
    public $currency;
}

$paymentInfo = new PaymentInfo();
$paymentInfo(Number::class); // new PaymentInfo<Number>();
set_decorator_prop($paymentInfo, 'currency', 'rubles'); // throws a GenericError
decorator - v6.1.1

Published by sagittaracc 7 months ago

  • Add Generics
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\T;

#[T]
class Box
{
    use Decorator;

    #[T]
    public array $items;
}

$box = new Box();
$box(Pencil::class); // new Box<Pencil>();

$pencil = new Pencil();
$pen = new Pen();
$box->_items = [$pencil, $pen]; // throws a GenericError
decorator - v6.1.0

Published by sagittaracc 7 months ago

  • Выпилил PhpDecorator
decorator - v6.0.0

Published by sagittaracc 7 months ago

decorator - v5.0.0

Published by sagittaracc 7 months ago

Pass the arguments of a method into the decorator wrappers

decorator - v4.3.3

Published by sagittaracc about 1 year ago

  • add call_decorator_func_array method to call a method with its decorators
decorator - v4.3.2

Published by sagittaracc about 1 year ago

Validator refactor

class Request
{
    use Decorator;

    #[Length(8)]
    public string $name;

    #[Length(32)]
    public string $caption;

    #[SerializeOf(Progress::class)]
    public array $progress;

    #[SerializeOf(DataTable::class)]
    public array $data;
}

class Progress
{
    use Decorator;

    #[UInt8]
    public $max;

    #[UInt8]
    #[LessThan('max')]
    public $pos;

    #[In('progress', 'finish', 'aborted')]
    public $status;

    #[Length(32)]
    public string $caption;
}

class DataTable
{
    use Decorator;

    #[ArrayOf(Str::class)]
    public array $header;

    #[Table]
    public array $table;
}

Validator examples

#[Attribute]
final class In extends Validator
{
    public array $in;

    function __construct(...$in)
    {
        $this->in = $in;
    }

    public function validation($value)
    {
        return in_array($value, $this->in);
    }
}

#[Attribute]
final class LessThan extends Validator
{
    function __construct(
        public $supreme
    )
    {}

    public function validation($value)
    {
        $supreme = $this->supreme;
        return $value <= $this->getObject()->$supreme;
    }
}

#[Attribute]
final class Table extends Validator
{
    public function validation($value)
    {
        if (isset($value['ins']) && !$this->validateIns($value['ins'])) {
            return false;
        }

        return true;
    }

    private function validateIns($ins)
    {
        if (!is_array($ins)) {
            return false;
        }

        if (!array_is_list($ins)) {
            return false;
        }

        $colCount = count($this->getObject()->header);

        foreach ($ins as $row) {
            if (!is_array($row)) {
                return false;
            }

            if (!array_is_list($row)) {
                return false;
            }

            if (count($row) !== $colCount) {
                return false;
            }
        }

        return true;
    }
}
decorator - v4.3.1

Published by sagittaracc about 1 year ago

Validation


use Sagittaracc\PhpPythonDecorator\Decorator;

class Request
{
    use Decorator;

    #[Int8]
    public $id;

    #[UInt8]
    public $uid;

    #[Str]
    #[Length(5)]
    public $method;

    #[ArrayOf(UInt8::class)]
    public array $params = [1, 2, 3];

    #[SerializeOf(User::class)]
    public array $user = ['id' => 1];

    #[SerializeArrayOf(User::class)]
    public array $userList = [['id' => 1], ['id' => 2]];
}
decorator - v4.3.0

Published by sagittaracc over 1 year ago

decorator - v4.2.2

Published by sagittaracc over 1 year ago

  • Validation support

Example:

class Request
{
    use Decorator;

    #[UInt8]
    protected $uid;
}

Validator

#[Attribute]
final class UInt8 extends Validator
{
    public function validation($value)
    {
        return $value >= 0 && $value <= 255;
    }
}

Usage

$request = new Request;
$request->uid = 255;  // it's fine
$request->uid = 512;  // throws an exception `Request::uid validation error! 512 is not UInt8!`
decorator - v4.2.1

Published by sagittaracc over 1 year ago

  • Refactoring
  • Добавил функции хелперы чтобы можно было управлять именами методов или свойств чтобы обращаться к ним с применением декораторов или без
$calc = new Calc();
$calc->{get_decor_name('sum')}(1, 2); //It will generate a decorated rule name for the sum method so it will apply its decorators
decorator - v4.2.0

Published by sagittaracc over 1 year ago

Разделение Python декораторов и Php декораторов. Последние не обязаны быть атрибутом

decorator - v4.1.0

Published by sagittaracc over 1 year ago

  • Add decorator on demand
$needExtraRoom = true;
$needWifi = true;

$room = new Booking();
$price = $room->getPrice(); // 1000

if ($needExtraRoom) {
    $price = (new ExtraRoom)->decorate($price); // (2000) Having an extra room doubles the price
}

if ($needWifi) {
    $price = (new Wifi)->decorate($price); // (20000) Having wifi in the room makes it ten times expensive
}
decorator - v4.0.0

Published by sagittaracc over 1 year ago

Переработан PhpAttribute (Удалены методы runIn, getFrom, equalTo)
old syntax: (new Route('/hello'))->runIn(Controller::class)
new syntax: (new Route('/hello'))->getMethod(Controller::class)->run()

old syntax: (new Primary)->getFrom(Controller::class)
new syntax: (new Primary)->getProperty(Controller::class)

rename equalTo -> matchTo

decorator - v3.0.2

Published by sagittaracc over 1 year ago

  • В цепочке атрибутов для проперти только самый первый атрибут отнаследованный НЕ ОТ PythonDecorator может быть просвоен этому проперти как инстанс
decorator - v3.0.1

Published by sagittaracc over 1 year ago

  • Refactor
Package Rankings
Top 22.71% on Packagist.org