yrizos/bucket

一组基本容器,以及带有 onSet 和 onGet 钩子的高级容器。

0.2.0 2014-07-21 22:09 UTC

This package is auto-updated.

Last update: 2024-08-23 23:42:51 UTC


README

Bucket 是一组容器对象的集合。

基本容器

有四个基本容器特质

  • ContainerTrait: 实现 \Countable, \IteratorAggregate
  • ArrayContainerTrait: 添加 \ArrayAccess
  • MagicContainerTrait: 如果您更喜欢魔法获取器和设置器而不是 \ArrayAccess
  • MagicArrayContainerTrait: 名字说明了这一点,不是吗?

您可以将特质附加到自己的类中,或使用提供的具体类。以下是一个快速示例

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

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

echo $myContainer["label"];

Bucket

Bucket 有点更有趣,增加了钩子。

$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

设置

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

Build Status