asko/flatmatter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

asko/flatmatter

最新稳定版本:v1.0.2

Composer 安装命令:

composer require asko/flatmatter

包简介

A flat data serialization language, and library.

README 文档

README

builds.sr.ht status coverage

A flat data serialization language, and library. The language aims to be YAML-like, but instead of nesting with indentation, it nests data with punctuation. Additionally, it supports boolean functions to manipulate data which can also be piped.

Install

composer require asko/flatmatter

Usage

The most basic example looks like this:

use Asko\FlatMatter\FlatMatter;

$config = file_get_contents('config.flatmatter');
$parsed = (new FlatMatter($config))->toArray();

But, in order to add functions to FlatMatter, you need to add classes that implement the Asko\FlatMatter\FlatMatterFunction interface to the functions property, like so:

use Asko\FlatMatter\FlatMatter;

$config = file_get_contents('config.flatmatter');
$fm = new FlatMatter($config);
$fm->functions = [new MyFunction];
$parsed = $fm->toArray();

Creating functions

Like mentioned above, functions in FlatMatter have to extend the Asko\FlatMatter\FlatMatterFunction interface, like so:

use Asko\FlatMatter\FlatMatterFunction;
use Asko\FlatMatter\Name;

#[Name('test')]
class TestFn implements FlatMatterFunction
{
    public function compute(...$args): mixed
    {
        if (!empty($args)) {
            return $args[0];
        }

        return null;
    }
}

And, like you can see from the example, you need to also attribute the class with the Name attribute so that the parser would know what class the function name used in the language corresponds to.

Specification

This is just the PHP port of FlatMatter, but I do encourage anyone who wants to port it to whatever language they want.

Example FlatMatter file:

some-var: "some value"
some-int: 12345
some.nested.value: "...."
some-var-using-boolean-fn: (boolean-fn "some-value")
some-var-using-threaded-boolean-fns: "some-value" / boolean-fn / (another-fn "some-arg")

This should create a data structure that looks like this in JSON:

{
  "some-var": "some value",
  "some-int": 12345,
  "some": {
    "nested": {
      "value": "...."
    }
  },
  "some-var-using-boolean-fn": "[result of boolean-fn function]",
  "some-var-using-threaded-boolean-fns": "[result of \"some-value\" piped into boolean-fn, piped into another-fn function]"
}

Built-in functions are up to the library implementor to make, and I don't think there should be any spec on mandatory functions. Ultimately the goal is for the user to define their own functions that they then pass to the library.

Threading is done using the / separator so that we could use YAML syntax highlighting and / passes as a valid value. Threading is always done with the value going as the first argument of a function, so if you pass any additional arguments in the threading, the computed result of the previous threading goes first, so in the above case it would work like this:

  1. "some-value" is the initial value
  2. It gets passed to the boolean-fn as the first (in this case only) argument
  3. The result of boolean-fn gets then passed as the first argument to another-fn
  4. another-fn also takes in a second argument "some-arg"
  5. The result of all this compute becomes the value of the var.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-03