psrly/huggables

PSR-8 compliant implementation of Huggable Interfaces

1.0.1 2018-08-25 08:13 UTC

This package is auto-updated.

Last update: 2024-04-29 03:53:57 UTC


README

PSR-8 compliant implementation of Huggable Interfaces

Install

composer require psrly/huggables ^1

Usage

Using existing Hugger implementations

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Psrly\Hugger;

// all hugger implementations can be given an optional hug limit and name

// simple silent hugger (no side effects, no state change)
$silent = new Hugger\Silent;

// simple verbose hugger (writes to output on hugging)
$verbose = new Hugger\Verbose;

$verbose->hug($silent);
// -> "X is being hugged by Y"
// -> "X is hugging back Y"

// simple satisfiable hugger (increments satisfaction on hugging)
$satisfactionThreshold = 3; // satisfied after hugging three times
$satisfiable = Satisfiable($satisfactionThreshold);

// hugged 0 times = not satisfied 
$satisfiable->isSatisfied(); // false

$satisfiable->hug($silent);

// hugged 1 time = not satisfied
$satisfiable->isSatisfied(); // false

$satisfiable->hug($silent);
$satisfiable->hug($silent);

// hugged 3 times = satisfied
$satisfiable->isSatisfied(); // true

Creating your own Hugger implementation

<?php

use Psrly\Hugger;

final class MyHugger extends Hugger
{
    protected function onBeingHuggedBy(Huggable $h): void
    {
        // change state / cause side effects on being hugged
    }

    protected function onHuggingBack(Huggable $h): void
    {
        // change state / cause side effects on hugging back
    }
}