All Projects → jungi-php → common

jungi-php / common

Licence: MIT license
A minimal library that defines primitive building blocks of PHP code.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to common

rust-error-handle
detail rust error handle
Stars: ✭ 47 (+67.86%)
Mutual labels:  option, result
ts-belt
🔧 Fast, modern, and practical utility library for FP in TypeScript.
Stars: ✭ 439 (+1467.86%)
Mutual labels:  option, result
either option
A small typed and safe library for error handling with functionnal programming concept in Dart and flutter project
Stars: ✭ 34 (+21.43%)
Mutual labels:  option, result
tiinvo
Functions for tacit programming and functional types for TypeScript and JavaScript.
Stars: ✭ 36 (+28.57%)
Mutual labels:  option, result
Swift Argument Parser
Straightforward, type-safe argument parsing for Swift
Stars: ✭ 2,430 (+8578.57%)
Mutual labels:  option
Akshare
AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
Stars: ✭ 4,334 (+15378.57%)
Mutual labels:  option
set-config-resolver
[READ-ONLY] Loads configs to you with CLI --config, -c, --set, -s or sets parameter
Stars: ✭ 50 (+78.57%)
Mutual labels:  option
akshare
AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
Stars: ✭ 5,155 (+18310.71%)
Mutual labels:  option
monas
🦋 Scala monads for javascript
Stars: ✭ 21 (-25%)
Mutual labels:  option
dart maybe
No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).
Stars: ✭ 20 (-28.57%)
Mutual labels:  option
result
A lightweight C++11-compatible error-handling mechanism
Stars: ✭ 121 (+332.14%)
Mutual labels:  result
Forbind
Functional chaining and promises in Swift
Stars: ✭ 44 (+57.14%)
Mutual labels:  result
python-pytest-harvest
Store data created during your `pytest` tests execution, and retrieve it at the end of the session, e.g. for applicative benchmarking purposes.
Stars: ✭ 44 (+57.14%)
Mutual labels:  result
Esito
Esito ambition is to be your return type for suspending functions.
Stars: ✭ 58 (+107.14%)
Mutual labels:  result
either
Elm Either
Stars: ✭ 24 (-14.29%)
Mutual labels:  result

Jungi Common

CI PHP

A minimal library that defines primitive building blocks of PHP code. It combines the advantages of functional and object-oriented programming. All of this makes code easier to understand and less prone to errors.

Primitive types:

Installation

composer require jungi/common

Documentation

GitBook

Quick insight

Result

interface Student
{
    public function id(): StudentId;
    public function isActive(): bool;
    public function name(): FullName;
}

enum ClassEnrollmentError: string {
    case InactiveStudent = 'inactive_student';
    case StudentAlreadyEnrolled = 'student_already_enrolled';
    case NoSeatsAvailable = 'no_seats_available';
}

class Class_
{
    private ClassId $id;
    private bool $finished;
    private int $numberOfSeats;
    /** @var StudentId[] */
    private array $students;
    
    /** @return Result<null, ClassEnrollmentError> */
    public function enroll(Student $student): Result
    {
        if (!$student->isActive()) {
            return err(ClassEnrollmentError::InactiveStudent);
        }
        if (in_iterable($student->id(), $this->students)) {
            return err(ClassEnrollmentError::StudentAlreadyEnrolled);
        }
        if (count($this->students) >= $this->numberOfSeats) {
            return err(ClassEnrollmentError::NoSeatsAvailable);
        }
        
        $this->students[] = $student->id();
        
        return ok();
    }
}

class ClassController
{
    // PUT /classes/{classId}/students/{studentId}
    public function enrollToClass(string $classId, string $studentId)
    {
        // ... fetch the class and the student
        return $class->enroll($student)
            ->andThen(fn() => $this->created()) // returns 201 if the result is ok
            ->getOrElse(fn(ClassEnrollmentError $error) => match ($error) {
                ClassEnrollmentError::StudentAlreadyEnrolled => $this->noContent(), // returns 204
                default => $this->badRequest($error), // returns 400 with the error
            });
    }
}

Option

interface UserRepositoryInterface
{
    /** @return Option<User> */
    public function find(string $username): Option;
}

class UserController
{
    public function __construct(private UserRepositoryInterface $userRepository) {}

    // GET /users/{username}
    public function getUser(string $username)
    {
        return $this->userRepository->find($username)
            ->andThen(fn(User $user) => $this->userData($user)) // maps the user to its resource representation
            ->getOrElse(fn() => $this->notFound()); // returns 404 response in case of the "none" option
    }
}

Equatable

/** @implements Equatable<Phone> */
class Phone implements Equatable
{
    public function __construct(private string $value) {}
    
    public function equals(self $other): bool
    {
        return $this->value === $other->value;
    }
}

assert(true === (new Phone('(321) 456-1234'))->equals(new Phone('(321) 456-1234')));
assert(false === (new Phone('(321) 456-1234'))->equals(new Phone('(454) 456-1234')));

Functions

use function Jungi\Common\equals;
use function Jungi\Common\in_iterable;
use function Jungi\Common\iterable_unique;
use function Jungi\Common\array_equals;

/** @implements Equatable<ContactInformation> */
class ContactInformation implements Equatable
{
    public function __construct(
        private Phone $phone,
        private ?Phone $mobile = null
    ) {}

    public function equals(self $other): bool
    {
        return $this->phone->equals($other->phone) && equals($this->mobile, $other->mobile);
    }
}

// equals()

$a = new ContactInformation(new Phone('(321) 456-1234'), new Phone('(886) 456-6543'));
$b = new ContactInformation(new Phone('(321) 456-1234'), new Phone('(886) 456-6543'));
assert(true === equals($a, $b);

$a = new ContactInformation(new Phone('(321) 456-1234'));
$b = new ContactInformation(new Phone('(321) 456-1234'), new Phone('(886) 456-6543'));
assert(false === equals($a, $b);

// array_equals()

$a = [new Phone('(321) 456-1234'), new Phone('(465) 799-4566')];
$b = [new Phone('(321) 456-1234'), new Phone('(465) 799-4566')];
assert(true === array_equals($a, $b));

$a = [new Phone('(321) 456-1234'), new Phone('(465) 799-4566')];
$b = [new Phone('(321) 456-1234')];
assert(false === array_equals($a, $b));

// in_iterable()

$iterable = [new Phone('(656) 456-7765'), new Phone('(321) 456-1234')];
assert(true === in_iterable(new Phone('(321) 456-1234'), $iterable));
assert(false === in_iterable(new Phone('(232) 456-1234'), $iterable));

// iterable_unique()

$unique = iterable_unique([
    new Phone('(321) 456-1234'),
    new Phone('(465) 799-4566'),
    new Phone('(321) 456-1234'),
]);
$expected = [
    new Phone('(321) 456-1234'),
    new Phone('(465) 799-4566'),
];
assert(true === array_equals($expected, $unique));
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].