Share value objects that contain the same primitive value as a singleton

Overview

sharable-value-objects

Build Status Coverage Status

Share value objects that contain the same primitive value as a singleton.

Singletons are kept under WeakReference objects.

Installing

composer require mpyw/sharable-value-objects

Usage

class ScreenName
{
    // 1. Mixin Sharable trait family
    use SharableString;

    // 2. Write your instantiation logic here
    public static function create(string $value): static
    {
        // Validation/Assertion
        if (!preg_match('/\A@\w{4,15}\z/', $value)) {
            throw new \InvalidArgumentException("invalid screen_name: $value");
        }

        // ** Call static::acquire() to get instance **
        return static::acquire($value);
    }

    // 3. Write your raw presentation logic here
    public function value(): string
    {
        // ** Call $this->getOriginalValue() to retrieve original value **
        return $this->getOriginalValue();
    }
}
class ScreenNameTest extends TestCase
{
    public function testSame(): void
    {
        // Same parameters yield the same instance
        $this->assertSame(
            ScreenName::create('@mpyw'),
            ScreenName::create('@mpyw'),
        );
    }

    public function testDifferent(): void
    {
        // Different parameters yield different instances
        $this->assertNotSame(
            ScreenName::create('@mpyw'),
            ScreenName::create('@twitter'),
        );
    }
}
You might also like...
Shiki is a beautiful syntax highlighter powered by the same language engine that many code editors use.
Shiki is a beautiful syntax highlighter powered by the same language engine that many code editors use.

Shiki is a beautiful syntax highlighter powered by the same language engine that many code editors use. This package allows you to use Shiki from PHP.

Making multiple identical function calls has the same effect as making a single function call.

Making multiple identical function calls has the same effect as making a single function call.

A layout field that forces translations to have the same structure.
A layout field that forces translations to have the same structure.

Kirby translatedlayout plugin A layout field that forces translations to have anidentical structure to the one of the default language. This is an exp

Alerts users in the SilverStripe CMS when multiple people are editing the same page.

Multi-User Editing Alert Alerts users in the SilverStripe CMS when multiple people are editing the same page. Maintainer Contact Julian Seidenberg ju

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.
A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

Private groups to share messages, photos, videos, links with friends and family.
Private groups to share messages, photos, videos, links with friends and family.

A truly private space for you and your friends What is Zusam ? Zusam (/tsuˈzam/) is a free and open-source way to self-host private forums for groups

Super simple share buttons for WordPress. No images, no css, no javascript

Developer Share Buttons A super lightweight social sharing solution using either the Web Share API or simple sharing links. Description A simple, cust

📦 An easy way to share the data from your backend to the JavaScript.

Laravel Shared Data ✨ Introduction Laravel Shared Data provides an easy way to share the data from your backend to the JavaScript. 🚀 Quick start Inst

Optional package for Laravel to generate social share links.

Laravel Share Share links exist on almost every page in every project, creating the code for these share links over and over again can be a pain in th

Releases(v1.0.2)
Owner
mpyw
mpyw
A minimal library that defines primitive building blocks of PHP code.

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.

Jungi 28 Jul 15, 2022
Hi Im L, I found a box that I believe it's contain Kira's real ID. for open that box we need to find three keys. let's start looking for them

DeathNote ctf Description are you smart enaugh to help me capturing the three keys for open the box that contain the real ID of kira? Let's start solv

Hamza Elansari 4 Nov 28, 2022
PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.

?? Yell PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects. Requirement

Zeeshan Ahmad 20 Dec 8, 2018
Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.DeepEqual().

Deeper Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.De

Joubert RedRat 4 Feb 12, 2022
Collection of value objects that represent the types of the PHP type system

sebastian/type Collection of value objects that represent the types of the PHP type system. Installation You can add this library as a local, per-proj

Sebastian Bergmann 1.1k Dec 29, 2022
A PHP 7 value objects helper library.

valueobjects Requirements Requires PHP >= 7.1 Installation Through Composer, obviously: composer require funeralzone/valueobjects Extensions This lib

Funeral Zone 56 Dec 16, 2022
Parse DSN strings into value objects to make them easier to use, pass around and manipulate

DSN parser Parse DSN strings into value objects to make them easier to use, pass around and manipulate. Install Via Composer composer require nyholm/d

Tobias Nyholm 77 Dec 13, 2022
A bunch of general-purpose value objects you can use in your Laravel application.

Laravel Value Objects A bunch of general-purpose value objects you can use in your Laravel application. The package requires PHP ^8.0 and Laravel ^9.7

Michael Rubél 136 Jan 4, 2023
Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Mohammed Manssour 8 Jun 8, 2023