superruzafa/settings

一组可选择的自定义元素

1.1.0 2014-10-12 13:53 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:29:03 UTC


README

Build Status

Settings 是一个用于管理元素集合的库。每个元素都包含一些元数据并包含在集合中。这些元数据可以在以后用于列出元素子集。元素通过选择器进行选择。

内置选择器

Settings 包含一些预定义的选择器

标签选择器

此类选择器允许将元素与某些关联的标签一起存储。有两种标签类型

  • 必需标签: 'required''necessary'
  • 可选标签(括号内): '[optional]''[redundant]'

必需标签和可选标签都有特殊含义,具体取决于标签选择器的类型。可选标签通常用于消除歧义。

严格标签选择器

此选择器仅选择那些元标签与选择器标签相交的项目

<?php

use Superruzafa\Settings\Collection;
use Superruzafa\Settings\Selector\StrictTagSelector;

$collection = new Collection();
$collection->add('item1', array('tag1', '[tag2]', 'tag3'));
$collection->add('item2', array('tag1', '[tag2]'));
$collection->add('item3', array('[tag1]', 'tag2', 'foo'));
$collection->add('item4', array('[tag1]', 'tag2', '[bar]'));

// The selector would select those items containing both `tag1` and `tag2` tags.
// If the item defines any other required tag that is not matched then the item is discarded.
$selector = new StrictTagSelector('tag1', 'tag2');
$selected = $collection->setSelector($selector)->select();
// $selected = ['item2', 'item4']

延迟标签选择器

此选择器选择那些元标签是选择器标签的超集的项目

<?php

use Superruzafa\Settings\Collection;
use Superruzafa\Settings\Selector\LazyTagSelector;

$collection = new Collection();
$collection->add('item1', array('tag1', '[tag2]', 'tag3'));
$collection->add('item2', array('tag1', '[tag2]'));
$collection->add('item3', array('[tag1]', 'tag2', 'foo'));
$collection->add('item4', array('[tag1]', 'foo', '[bar]'));

// The selector would select those items containing at least both `tag1` and `tag2` tags.
// Other tags (required or optional) doesn't affect.
$selector = new LazyTagSelector('tag1', 'tag2');
$selected = $collection->setSelector($selector)->select();
// $selected = ['item1', 'item2', 'item3']

域名选择器

此选择器使用域名在集合中选择元素。

<?php

use Superruzafa\Settings\Collection;
use Superruzafa\Settings\Selector\DomainSelector;

$collection = new Collection();
$collection->add('item1', 'github.com');
$collection->add('item2', 'www.github.com');
$collection->add('item3', 'example.com');
$collection->add('item4', 'ftp.example.com');

// The selector would select those items associated with github.com or *.github.com.
$selector = new DomainSelector('github.com');
$selected = $collection->setSelector($selector)->select();
// $selected = ['item1', 'item2']

其他选择器

AlwaysSelector

此选择器始终选择集合中的每个元素。

$selector = new AlwaysSelector();
$selected = $collection->setSelector($selector)>select();
// $selected = all elements in the collection

NeverSelector

与 AlwaysSelector 相反,此选择器不选择集合中的任何元素。

$selector = new NeverSelector();
$selected = $collection->setSelector($selector)>select();
// $selected = array()

选择方法

每个选择器都附带以下操作之一

  • select() 选择由选择器选择的元素。
  • selectOne() 选择由选择器选择的第一个元素。
  • discard() 选择由选择器未选择的元素。
  • discardOne() 选择由选择器未选择的第一个元素。