dynoser/helml

无需依赖的 HELML 类 PHP 实现

v1.0.2 2023-12-29 18:49 UTC

This package is auto-updated.

Last update: 2024-09-09 06:47:18 UTC


README

helml-logo

PHP 包

实现

安装(PHP)

要安装 HELML,只需使用 composer

composer require dynoser/helml

或者,您可以复制并直接使用文件

use dynoser\HELML\HELML;  # it means that the HELML class is in the namespace "dynoser\HELML"
use dynoser\HELML\fileHELMLsect; # it means that the fileHELMLsect class is in the namespace "dynoser\HELML"
require_once "src/HELML.php"; // specify the correct path to file
require_once 'src/fileHELMLsect.php';// or use "autoload.php" from composer to autoload

用法

此包包含两个独立的类

  • class HELML - HELML 格式的编码/解码器
  • class fileHELMLsect - 从 HELML 文件中加载选择性的数据段

class HELML

以下是一个如何使用 HELML 类的快速示例

use dynoser\HELML\HELML;
require_once 'src/HELML.php'; // path to HELML.php file, or require "autoload.php"

# Example data structure

$data = [
    "key1" => "value1",
    "key2" => [1, 2, 3],
    "key3" => [
        "nested_key" => "nested_value"
    ]
];

# Encode the data structure into a HELML string
$encoded_data = HELML::encode($data);
print_r($encoded_data)

# Decode the HELML string back into a data structure
$decoded_data = HELML::decode($encoded_data);

编码后的数据

key1: value1

key2:
 :--:  1
 :--:  2
 :--:  3

key3
 :nested_key: nested_value

功能

将数据数组编码/解码为/从 HELML。

API

HELML::encode($arr, $url_mode=False)

将数据数组编码为 HELML 字符串。

  • $arr 要编码的输入数据数组。
  • $url_mode (bool, optional): 一个布尔值,表示是否应使用 URL 模式。默认为 False。

返回

  • string: 编码的 HELML 字符串。

HELML::decode($src_rows)

将 HELML 格式的字符串或字符串数组解码为嵌套字典。

  • $src_rows HELML 输入作为字符串或字符串数组。

返回

  • Array: 解码后的嵌套数组。

Class fileHELMLsect

此类实现了从 HELML 格式的文件中选择性加载段。

use dynoser\HELML\fileHELMLsect;

require_once 'src/fileHELMLsect.php';// path to file, or require "autoload.php"

/*
For example, we have file "testdata.helml" contained this:

A:
 :X: 1
 :Y: 2
# Comment before section
B
 :X: 5
 :Y: 6

Core
 :Test: data
 :Nested key: value
C:
 # This is a Comment string
 :Other: data

DD: DD-Value
D: D-value
E: is E
F:
 :nested:
  ::--: First
  ::--: Second

*/

fileHELMLsect::$add_section_comments = false; // switch off auto-comments

$encoded_data = fileHELMLsect::Load('testdata.helml', ['B:', 'C', 'D']);

print_r($encoded_data)

结果字符串

B
:X: 5
:Y: 6
C:
:Other: data
D: D-value

结果中只从 'B'、'C' 和 'D' 段获取数据,没有注释和空行

以这种方式,从根级别获取段非常方便。

您可以使用完全相同的方式获取嵌套键,但应记住,我们将获取这些结构,而不获取它们所在的那些结构。

例如,我们可以从上一个示例文件中获取 ':nested' 键,并得到

$encoded_data = fileHELMLsect::Load('testdata.helml', [':nested']);

print_r($encoded_data);

结果

:nested:
::--: First
::--: Second

此样本的解析如下

(
    [nested] => Array
        (
            [0] => First
            [1] => Second
        )

)

注意

  • 在段名称末尾指定 ":" 是可选的。这些冒号内部被删除。
  • 如果我们要获取非根级别的段,则必须指定所有级别的冒号。
  • 参数 $only_first_occ,允许您获取文件中列出的所有段的所有出现,如果文件中有多个段

$only_first_occ 参数设置为 false,您可以从某些嵌套键的值中提取所有变体。例如,让我们从上面的示例中获取嵌套键 X 的所有值

$encoded_data = fileHELMLsect::Load('testdata.helml', [':Y'], false);

print_r($encoded_data);

结果

:Y: 2
:Y: 6

独立性

  • 请注意,类 HELMLfileHELMLsect 都没有依赖项,并且可以独立于对方使用。

另请参阅

许可证

本项目采用 Apache-2 许可证。