owlycode/interlacing

v1.0.0 2019-11-09 19:08 UTC

This package is auto-updated.

Last update: 2024-09-10 06:01:22 UTC


README

它是一个由GalaxyKate的GalaxyKate的Tracery启发的PHP替代品,它从Tracery那里获得了很多灵感。它用于根据语法生成程序化文本。

如何使用它?

https://github.com/OwlyCode/interlacing/releases获取phar包,或者使用composer将其作为项目依赖安装:composer req owlycode/interlacing

首先声明一个语法

# grammar.yaml
content:
    root: "the {{ adjective }} {{ animal }}"
    animal: ["warthog", "hedgehog", "ocelot"]
    adjective: ["warty", "hoary", "oneiric"]

然后通过运行interlacing.phar grammar.yaml或在你的代码中调用它来使用它

use OwlyCode\Interlacing\Interlacing;

// index.php
$i = Interlacing::fromFile(__DIR__.'/grammar.yaml');

echo $i->resolve('root'); // Will output things like "the hoary ocelot".

语法文件由一组占位符及其可能的关联值组成。您可以使用{{ }}分隔符在内容中放置占位符。每当interlacing遇到占位符时,它都会从语法中查找可能的值并随机选择一个。

通常使用root占位符作为入口点。

使用变体和解析器

您可以通过在占位符前面添加一个前缀|来应用变体。变体可以链式应用。

解析器不是手动应用的,它们只是影响占位符被值替换的方式。每次使用占位符时,Interlacing都会运行所有已知的解析器,并返回第一个非空结果的值。否则,它会在语法中查找可能的值并选择一个。

内置的变体和解析器

复数化

将单词变为复数。您可以在语法中更改区域设置(默认为英语)

content:
    root: "{{ animal|s }}"
    name: ["cat", "dog", "mouse"] # cats, dogs, mice
locale: fr
content:
    root: "{{ animal|s }}"
    name: ["chat", "chien", "cheval"] # chats, chiens, chevaux

首字母大写

将首字母大写。

content:
    proverb: "{{ animal|capitalize }} can eat {{ animal }}, as they say."
    name: ["the lion", "the mouse", "the cat", "the cow"]

可能产生:老鼠可以吃牛,就像他们说的那样。

内存

允许在执行过程中动态构建占位符。

  • store: 存储显示的占位符值。
  • storeAll: 存储占位符的所有可能值。
  • storeOthers: 存储占位符的所有可能值(不包括显示的值)。
  • push: 将显示的占位符值添加到已存储的值中。
  • pop: 从内存中删除已存储的显示值。

示例

content:
    root: ["We pick {{ value|store(selected_value) }} and we can show it later: {{ selected_value }}"]
    value: ['A', 'B', 'C']
content:
    root: ["To make a cake you need {{ ingredient|storeOthers(i) }}, {{ i|pop }} and {{ i }}"]
    ingredient: ['eggs', 'flour', 'water']
content:
    root: ["{{ duo }} went on an adventure. {{ adventurer|pop }} survived, but not {{ adventurer }}."]
    duo: ['{{ name|storeOthers(c)|push(adventurer) }} and {{ c|push(adventurer) }}']
    name: ['Alice', 'Peter', 'John', 'Frantz', 'Albert', 'Mark']

警告:如果您用内存值覆盖基于语法的占位符,它将完全替换它。

静音

防止占位符渲染,通常与内存变体一起使用:它允许配对一些结果。

content:
    root: ["Fishes are {{ level }} at {{ skill }}."]
    skill: [ "swimming{{ good|store(level)|silence }}", "flying{{ bad|store(level)|silence }}"]
    good: ["good", "skilled"]
    bad: ["bad", "unskilled"]

将产生鱼擅长游泳。鱼不擅长飞行。,但不会产生鱼擅长飞行。

创建您自己的变体和解析器

您可以通过实现OwlyCode\Interlacing\Plugin\AlterationInterface接口来创建自己的变体

namespace Custom\Plugin;

class Prepend implements AlterationInterface
{
    // An alteration always receive the placeholder name, the actual resolved value and the args provided to the alteration
    public function prepend(string $placeholder, string $input, array $args): string
    {
        return $args[0] . $input;
    }

    // Returns the list of alterations by name.
    public function getAlterations(): array
    {
        return [
            'prepend' => [$this, 'prepend'], // Usage: {{ placeholder|prepend("prefix") }}
        ];
    }
}

您可以通过实现OwlyCode\Interlacing\Plugin\ResolverInterface接口来创建解析器

namespace Custom\Plugin;

class Now implements ResolverInterface
{
    public function resolve($name): ?string
    {
        if ($name === 'now') {
            return (new \Datetime())->format('Y-m-d H:i:s');
        }

        return null;
    }
}

现在让我们使用之前声明的变体和解析器

plugins:
    - Custom\Plugin\Now
    - Custom\Plugin\Prepend
content:
    root: "{{ now|prepend("time: ") }}"

这将显示:时间:2019-11-08 22:44:00,其中日期和时间设置为当前时间。