yrizos/bucket

A set of basic containers, and a more complicated one with onSet and onGet hooks.

0.2.0 2014-07-21 22:09 UTC

This package is auto-updated.

Last update: 2024-10-24 00:00:12 UTC


README

Bucket is a collection of container objects.

Basic containers

There are four basic container traits:

  • ContainerTrait: implements \Countable, \IteratorAggregate
  • ArrayContainerTrait: adds \ArrayAccess to the mix
  • MagicContainerTrait: if you prefer magic getter & setters to \ArrayAccess
  • MagicArrayContainerTrait: the name is revealing, isn't it?

You can either attach the traits to your own classes, or use the concrete classes provided. Here's a quick example:

class MyContainer implements Bucket\Container\MagicArrayContainerInterface
{
    use Bucket\Container\MagicArrayContainerTrait;
}

$myContainer        = new MyContainer();
$myContainer->label = "hello world";

echo $myContainer["label"];

Bucket

Bucket is a bit more interesting, adding hooks to the mix.

$bucket = new Bucket\Bucket();

$bucket->onGet("name", function ($value) {
    return trim($value);
});

$bucket->onSet("email", function ($value) {

    if (!filter_var($value, FILTER_VALIDATE_EMAIL))
        throw new  \InvalidArgumentException();

    return filter_var($value, FILTER_SANITIZE_EMAIL);
});


$bucket->name = "    Yannis    ";
var_dump($bucket->name); // string 'Yannis' (length=6)

$bucket->email = 42; // will fail

Setup

require: "yrizos/bucket": "dev-master"

Build Status