A minimal library that defines primitive building blocks of PHP code.

Overview

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
   
     */
   
    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
   
     */
   
    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
   
     */
   
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')));
You might also like...
This plugin adds 95% of 1.16 blocks & items and their functionality
This plugin adds 95% of 1.16 blocks & items and their functionality

INether This plugin adds 95% of 1.16 blocks & items and their functionality Implemented blocks Ancient Debris All types of Basalt Crimson & Warped Fun

Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates
Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates

Magento 2 Image Resizer Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates Installation $ composer require

Silverstripe module allowing editors to create newsletters using elemental blocks and export them to a sendy instance

Silverstripe Sendy Silverstripe module allowing editors to create newsletters using elemental blocks and export them to a sendy instance. Introduction

PocketMine-MP plugin that adds new blocks to the server!

CustomBlockLoader PocketMine-MP plugin that adds new blocks to the server! Reference This plugin is experimental. We are not responsible for any probl

A virion for PocketMine-MP to create and manage fake blocks

🧊 fakeblocks Create and manage fakeblocks Description: A virion for PocketMine-MP to create and manage fake blocks. This virion indicates to the clie

A Magento 2 module that allows admins to duplicate CMS blocks and pages from their respective grids and en masse.
A Magento 2 module that allows admins to duplicate CMS blocks and pages from their respective grids and en masse.

element119 | CMS Duplicator 📝 Features ✔️ Allows merchants to duplicate CMS blocks and pages from the Action column in the admin grid ✔️ Allows merch

Samsui is a factory library for building PHP objects useful for setting up test data in your applications.

#Samsui Samsui is a factory library for building PHP objects useful for setting up test data in your applications. It is mainly inspired by Rosie for

Releases(v1.2.0)
Owner
Jungi
A set of useful components designed for PHP
Jungi
Converts any PocketMine-MP 3.0 extended blocks into PM4 native blocks!

ExtendedBlocksConverter Converts any PocketMine-MP 3.0 extended blocks into PM4 native blocks! Yes, you heard right, this plugin can convert any lefto

Covered123 6 Jun 4, 2022
Share value objects that contain the same primitive value as a singleton

sharable-value-objects Share value objects that contain the same primitive value as a singleton. Singletons are kept under WeakReference objects. Inst

mpyw 5 Nov 14, 2021
Highlight code blocks with league/commonmark and Shiki

Highlight code blocks with league/commonmark and Shiki This package contains a block renderer for league/commonmark to highlight code blocks using Shi

Spatie 55 Aug 27, 2022
🧬 Nano is a zero-config, no skeleton, minimal Hyperf distribution that allows you to quickly build a Hyperf application with just a single PHP file.

Nano is a zero-config, no skeleton, minimal Hyperf distribution that allows you to quickly build a Hyperf application with just a single PHP file.

Hyperf 273 Jan 4, 2023
Minimal HTML login page that uses a json file as a database

JSONlogin Minimal HTML login page that uses a json file as a database Minimal login system that requires a new user to input username, password and th

null 12 Jul 5, 2022
A plugin to add more blocks to PocketMine

This plugin aims to add all blocks not included in PocketMine. As of right now the ExtendedBlocks plugin is required to add blocks with IDs above 255.

xSuper 26 Dec 19, 2022
Provide blocks which allow positioning content within them in layouts.

Mini layouts Provide blocks which allow positioning content within them in layouts. Backdrop Installation Install and enable the module as usual. Go t

Backdrop CMS contributed projects 5 Dec 17, 2021
A quick naked theme to demonstrate how easy it is to support Gutenberg using ACF blocks

ACF Gutenberg Demo Theme A quick naked theme to demonstrate how easy it is to support Gutenberg using ACF blocks demo.mp4 Files I have found a useful

Stirtingale 1 Oct 28, 2021
Allow SVG images to be used in Magento CMS blocks and pages via the TinyMCE Wysiwyg Editor.

Hyvä Themes - SVG support for the Magento CMS Wysiwyg Editor Allow SVG images to be used in CMS blocks and pages via the TinyMCE Wysiwyg Editor. hyva-

Hyvä 14 Dec 15, 2022
PocketMine-MP Plugin for converting items & blocks name to your language.

ConvertName PocketMine-MP Plugin for converting items & blocks name to your language. Usage Due to license issue, I don't put the language files direc

null 6 Aug 25, 2022