Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/ optional Public archive

A PHP lib to help to hande optional parameters.

License

Notifications You must be signed in to change notification settings

monsieurluge/optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Optional

logo

The goal of the Optional library is to handle elegantly optional parameters in order to avoid control structures like if or switch, and to avoid multiple return types.

The code also becomes more declarative.

Examples

Sending user data to a template

Context: We want to display user data but the phone number is an optional one.

As usually seen

<?php

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

interface PhoneNumber
{
    public function toString(): string;
}

interface User
{
    public function firstname(): string;
    public function lastname(): string;
    public function phone(): PhoneNumber|null;
}

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

$this->templateEngine->render(
    'template/file/path/',
    [
        'firstname' => $user->firstname(),
        'lastname'  => $user->lastname(),
        'phone'     => is_null($user->phone())
                         ? '--.--.--.--.--'
                         : $user->phone()->toString(),
    ]
);

With Optional

<?php

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

interface PhoneNumber
{
    public function toString(): string;
}

interface User
{
    public function firstname(): string;
    public function lastname(): string;
    public function phone(): Maybe; // Maybe<PhoneNumber>
}

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

$this->templateEngine->render(
    'template/file/path/',
    [
        'firstname' => $user->firstname(),
        'lastname'  => $user->lastname(),
        'phone'     => $user->phone()
                            ->map(function (PhoneNumber $phone) { return $phone->toString(); })
                            ->getOr('--.--.--.--.--'),
    ]
);

About

A PHP lib to help to hande optional parameters.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages