PHP code example of monsieurluge / result

1. Go to this page and download the library: Download monsieurluge/result 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/ */

    

monsieurluge / result example snippets




// ---------------------------------------- interfaces

interface User
{
    public function sendMessage(string $text): void;
}

interface UserRepository
{
    public function userById(int $uuid): User|null;
}

// ---------------------------------------- implementation

$userRepository = new MySqlUserRepository();

$user = $userRepository->userById(1234);

if (true === is_null($user)) {
    // error handling
} else {
    $user->sendMessage('Hi!');
}



// ---------------------------------------- interfaces

interface User
{
    public function sendMessage(string $text): void;
}

interface UserRepository
{
    public function userById(int $uuid): Result<User>;
}

// ---------------------------------------- implementation

$userRepository = new MySqlUserRepository();

$userRepository
    ->userById(1234)
    ->then(function (User $user) {
        $user->sendMessage('Hi!');
    })
    ->else(function (Error $error) {
        // error handling
    });



// ---------------------------------------- interfaces

interface User
{
    public function name(): string;
}

interface UserRepository
{
    public function userById(int $uuid): User|null;
}

// ---------------------------------------- implementation

class UserNameController
{
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function handle(int $uuid): Response {
        $user = $this->userRepository->userById($uuid);

        if (true === is_null($user)) {
            $response = new Response(
                sprintf('the user %s does not exist', $uuid),
                404
            );
        } else {
            $response = new Response($user->name(), 200);
        }

        return $response;
    }
}



// ---------------------------------------- interfaces

interface User
{
    public function name(): string;
}

interface UserRepository
{
    public function userById(int $uuid): Result<User>;
}

// ---------------------------------------- implementation

class UserNameController
{
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function handle(int $uuid): Response {
        return $this->userRepository
            ->userById($uuid)
            ->map(function (User $user) {
                return new Response($user->name(), 200);
            })
            ->getOr(function (Error $error) use ($uuid) {
                return new Response(
                    sprintf('the user %s does not exist', $uuid),
                    404
                );
            });
    }
}



// ---------------------------------------- interfaces

interface User
{
    public function name(): string;
}

interface Group
{
    public function add(User $user): void;
}

interface UserRepository
{
    public function userById(int $uuid): User|null;
}

interface GroupRepository
{
    public function groupById(int $uuid): Group|null;
}

// ---------------------------------------- implementation

$group = $this->groupRepository->groupById($groupUuid);
$user  = $this->userRepository->userById($userUuid);

if (true === is_null($user) || true === is_null($user)) {
    // error handling
} else {
    $group->add($user);
}



// ---------------------------------------- interfaces

interface User
{
    public function name(): string;
}

interface Group
{
    public function add(User $user): void;
}

interface UserRepository
{
    public function userById(int $uuid): Result<User>;
}

interface GroupRepository
{
    public function groupById(int $uuid): Result<Group>;
}

// ---------------------------------------- implementation

(new Combined([
    $this->groupRepository->groupById($groupUuid),
    $this->userRepository->userById($userUuid),
]))
    ->then(function (Group $group, User $user) {
        $group->add($user);
    })
    ->else(function (Error $error) {
        /** error handling */
    });



// ---------------------------------------- implementation

$this->groupRepository
    ->groupById($groupUuid)
    ->join($this->userRepository->userById($userUuid))
    ->then(function (Group $group, User $user) {
        $group->add($user);
    })
    ->else(function (Error $error) {
        /** error handling */
    });