jclaveau/php-fluent-trait

Traits gathering fluent syntax common methods

1.3.0 2019-11-06 14:58 UTC

This package is auto-updated.

Last update: 2024-11-05 11:49:34 UTC


README

These traits gather some commons methods of classes implementing fluent syntax.

Quality

Build Status Scrutinizer Code Quality

Installation

php-fluent-traits is installable via Composer

composer require jclaveau/php-fluent-traits

Usage

class FluentObject
{
    use JClaveau\Traits\Fluent\New_;
    use JClaveau\Traits\Fluent\Clone_;
    use JClaveau\Traits\Fluent\DefineAs;
    use JClaveau\Traits\Fluent\DefineCloneAs;
    use JClaveau\Traits\Fluent\Dump;
    
    protected $name;
    
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
}

$instance = FluentObject::new_()
    ->setName('Foo')
    ->dump()                // FluentObject #1 Foo
    ->defineAs($fooObject1)
    ->clone_()
    ->dump()                // FluentObject #2 Foo
    ->defineCloneAs($barObject2)
    ->setName('Bar')
    ->dump()                // FluentObject #2 Bar
    ;
    
$fooObject1->dump(); // FluentObject #1 Foo
$barObject2->dump(); // FluentObject #3 Foo
$instance->dump();   // FluentObject #2 Bar