perryflynn/perrys-tags

通过标签筛选数据

1.0.2 2017-06-24 18:52 UTC

This package is not auto-updated.

Last update: 2024-09-25 01:17:12 UTC


README

Build Status

PerrysTags是一个PHP库,允许根据标签筛选数据。它的设计非常通用,既可以与数据库配合,也可以与程序运行时生成的数据结构配合。

状态

  • 开发中,第一个可运行版本
  • 文档仅提供德语,需要翻译成英语

依赖

该库使用PerrysLambda,这是PHP的C# Lambda表达式实现。PerrysLambda允许非常简单地过滤和处理简单和复杂的数据结构。

PerrysLambda负责以下任务

  • 将标签字符串转换为相应的对象
  • 搜索标签集合
  • 选择数据
  • 将标签对象序列化为原始字符串

MySQL数据库工作流程

  • 通过SELECT语句从MySQL数据库读取所有可用的标签。例如 SELECT tagname FROM tags GROUP BY tagname WHERE uid IN (SELECT tag_uid FROM content)
  • 将标签导入到TagCollection对象类型
  • 按标签搜索或筛选
  • 使用额外的SQL查询列出完整的标签列表,用于筛选实际数据

请参阅cliexample.php

代码示例

<?php

// From cliexample.php

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

use PerrysTags\Tag;
use PerrysTags\TagCollection;

// Example taglist, could come from sql statement
$taglist = array(
    'language:english',
    'language:german',
    'country:germany',
    'country:united states of america',
    'country:hungary',
    'country:italy',
    'country:spain',
    'country:netherlands',
    'color:green',
    'color:red',
    'color:yellow',
    'color:gray',
    'color:blue',
    'color:white',
    'color:light orange',
    'condition:slightly used',
    'condition:used',
    'condition:new',
    'condition:broken',
);

// Create tag collection
$collection = new TagCollection();

// Import tags
foreach($taglist as $tag)
{
    $collection->add($tag);
}

// Prints result of a search
// Supports Regex
var_dump($collection->tagSearch('country~:"^united states" color~:"e$" condition:used')->getTags()->serialize());

结果

array(5) {
  [0] =>
  string(32) "country:united states of america"
  [1] =>
  string(10) "color:blue"
  [2] =>
  string(11) "color:white"
  [3] =>
  string(18) "color:light orange"
  [4] =>
  string(14) "condition:used"
}

请参阅单元测试以获取更详细的示例。