PHP code example of kelunik / builders

1. Go to this page and download the library: Download kelunik/builders library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kelunik / builders example snippets




namespace Example;

class User
{
    /** @var int|null */
    private $id;
    /** @var string|null */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setId(?string $id): void
    {
        $this->id = $id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): void
    {
        $this->name = $name;
    }
}



namespace Example;

use Example;

class UserBuilderMethods implements \Kelunik\Builders\Builder
{
    private $entity;

    public function __construct()
    {
        $this->entity = new Example\User;
    }

    final public function withId(?string $value)
    {
        $this->entity->setId($value);

        return $this;
    }

    final public function withName(?string $value)
    {
        $this->entity->setName($value);

        return $this;
    }

    final public function build(): Example\User
    {
        return $this->entity;
    }
}



namespace Example;

class UserBuilder extends UserBuilderMethods
{
    public function root()
    {
        return $this->withId(1)->withName('root');
    }
}



namespace Example;

function user() {
    return new UserBuilder;
}



use Example\User;
use function Example\user;

r_dump($user instanceof User);
var_dump($user);