clytras/lyx

Lyx 是一组 PHP 类和函数的库

v1.2 2019-05-29 16:45 UTC

This package is auto-updated.

Last update: 2024-09-29 05:21:37 UTC


README

Lyx 是一组 PHP 类和函数的库。这绝对不是一个“完美”的 PHP 实现;它是我多年来编写的一系列类和函数的集合,其起源可以追溯到 PHP 5.2,因此代码中仍有部分需要重写、修改或完全删除。发布它的主要原因是因为我需要一个快速的方式来使用 composer 安装它。

要求

PHP 至少需要 PHP 7.1.0 版本。我开始构建它时是从 PHP 5.2 开始的,但是支持旧版本的 PHP(< 7.1)没有意义。代码的一些部分可能使用较旧的 PHP 代码风格/方法,但我会修复所有这些库部分,也许会有其他感兴趣或认为它有用的人帮忙。

安装

使用 composer

composer require clytras/lyx

下载(不使用自动加载器)

仅包含一次 src/autoload.php

测试

尚未实现测试用例,但将使用 PHPUnit 测试库进行详细和完整的特性测试。

示例

示例位于 \examples 目录中。目前覆盖的示例用例不多,但将来会添加更多。

  • Lyx\Strings\Str::format($text, $params = [], $options = false)
    Str::format 参数支持单数组、关联嵌套数组和常量。单数组参数通过表示元素索引的数字支持,例如 {0}{1} 等。关联数组参数可以嵌套并命名,例如 {param.arg1}。它支持数字零填充,如 {0,5} 在传递数字 123 时将产生 00123。它还支持浮点数精度,如 {0,.4} 在传递数字 123.45 时将产生 123.4500。填充和浮点数精度可以结合使用,如 {0,5.4} 在传递 123.45 时将产生 00123.4500。目前尚未实现浮点数精度舍入方法,但将来会添加。如果我们想使用千位分隔符分隔数字,我们在零填充数字后添加一个斜杠,如 {0,/.2} 将使用数字 10000 产生 10,000.00。它还使用 X(或 x 用于小写)将数字转换为十六进制,O 用于八进制,B 用于二进制,如 {0:X} 在传递 0xfe04064 时将产生 FE0111111100000 使用 {0:B}。数字转换器还支持零填充,如 {0:x,6} 在传递 4064 时将产生 000fe0
// Simple Str::format example

Lyx\Strings\Str::format("This is a simple {0} for {1} ('{0}', '{1}')", [
  'format',
  'testing'
]));

// This is a simple format for testing ('format', 'testing')
// Using assosiative arrays and constants

use Lyx\Strings\Str;

define("C_TEST", 'Const test!');

lyx_print(Str::format(
  "This is an assoc keys {example} for {purpose} [{C_TEST}] ('{example}', '{purpose}')",
  [
    'example' => 'Case',
    'purpose' => 'Testing'
  ]
));

// This is an assoc keys Case for Testing [Const test!] ('Case', 'Testing')
// Using assosiative nested arrays

use Lyx\Strings\Str;

lyx_print(Str::format(
  "This is an assoc keys {example.arg1} for {purpose.a1} ('{example.arg2}', '{purpose.a2}')",
  [
    'example' => [
      'arg1' => 'Example - Arg 1',
      'arg2' => 'Ex - Arg 2'
    ],
    'purpose' => [
      'a1' => 'Testing 1',
      'a2' => 'Test 2'
    ]
  ]
));

// This is an assoc keys Example - Arg 1 for Testing 1 ('Ex - Arg 2', 'Test 2')
// Numbers zero padding, decimal point precision and converters

use Lyx\Strings\Str;

$nums = [123, 1.23, 19000321, 0xEE, 0xA5F];
$numCases = [
  'single_numbers' => [
    'format' => "{0} / {1} / {2}"
  ],
  'zero_padding/dec_point = ./thousand_sep = ,' => [
    'format' => "{0,.3} / {1,3.4} / {2,/.2}",
    'options' => [
      'dec_point' => ',',
      'thousands_sep' => '.'
    ]
  ],
  'zero_padding/dec_point = ,/thousand_sep = .' => [
    'format' => "{0,.3} / {1,3.4} / {2,/.2}"
  ],
  'base/hex' => [
    'format' => "{3:x} / 0x{3:X,6} / {4:x} / 0x{4:X,4}"
  ],
  'base/oct' => [
    'format' => "{3:o} / 0{3:o,5} / {4:o} / 0{4:o,4}"
  ],
  'base/bin' => [
    'format' => "{3:b} / 0b{3:b,8} / {4:b} / 0b{4:b,16}"
  ]
];

foreach($numCases as $name => $case) {
  echo "{$name}: ";
  lyx_println(Str::format($case['format'], $nums, $case['options'] ?? []));
}
输出
> single_numbers: 123 / 1.23 / 19000321
> zero_padding/dec_point = ./thousand_sep = ,: 123,000 / 001,2300 / 19.000.321,00
> zero_padding/dec_point = ,/thousand_sep = .: 123.000 / 001.2300 / 19,000,321.00
> base/hex: ee / 0x0000EE / a5f / 0x0A5F
> base/oct: 356 / 000356 / 5137 / 05137
> base/bin: 11101110 / 0b11101110 / 101001011111 / 0b0000101001011111
  • Lyx\System\Terminal
    Terminal 类可以用于向终端输出带颜色的格式化文本。它使用一个非常简单的格式来设置前景色、背景色和属性。着色格式的格式为 <f:[颜色名称]> 用于前景色,<b:[颜色名称]> 用于背景色,<a:[属性名称]> 用于设置属性。颜色是分层的,这意味着它们以分层的方式打开和关闭;属性仅使用属性名称前的减号 - 来设置和取消设置。例如,<f:yellow,b:blue,a:underline> 将前景色设置为黄色并将背景色设置为蓝色;我们可以打开另一个标签来向堆栈添加颜色,<b:cyan> 将背景色设置为青色,当我们调用 <b> 时,它将返回到之前设置为青色的蓝色;我们可以只关闭一个标签来重置或返回;<f> 将返回一个前景色,<b> 将返回一个背景色,<a:-underline> 将清除下划线属性,<a> 将重置所有属性,而 <> 将将所有内容重置为默认值。
// Using simple color formating or print removing the formating tags

use Lyx\System\Terminal;

$str = 'Test: <f:red,b:blue>This<f> <b:red>and<b> <f:green,a:underline>more<> <f:light-green>colors<f> <a:reverse:underline>test<a>';

// Print the text with terminal color formating
Terminal::println($str);

// Print the raw text without color formating tags
Terminal::printlnRaw($str);
输出

Example

// Using templates to set color formating with regular expressions

use Lyx\System\Terminal;

$html = <<<HTML
<html lang="en">
 <head>
  <title>
    A Simple HTML Document
  </title>
 </head>
 <body bgcolor="#E6E6FA">
  <p align="center">This is a very simple HTML document</p>
  <p>It only has two paragraphs</p>
 </body>
</html>
HTML;

Terminal::printTmpl($html, [
  // Use green to color all the tags
  '/<(.|\\n)*?>/' => 'f:green',

  // Use yellow to collor tags attribute values
  '/(?:\<\!\-\-(?:(?!\-\-\>)\r\n?|\n|.)*?-\-\>)|(?:<(\S+)\s+(?=.*>)|(?<=[=\s])\G)(?:((?:(?!\s|=).)*)\s*?=\K\s*?[\"\']?((?:(?<=\")(?:(?<=\\\\)\"|[^\"])*|(?<=\')(?:(?<=\\\\)\'|[^\'])*)|(?:(?!\"|\')(?:(?!\/>|>|\s).)+))[\"\']?\s*)/m' => 'f:yellow',
]);
输出

Example

API 列表

目前只有一个包含所有函数和类的 API 列表。也许将来会有一个更详细且经过文档化的导航页面。

许可证

BSD-3-Clause

版权所有 2019 Lytras Christos

在满足以下条件的情况下,允许重新分发和使用源代码和二进制形式,无论是否修改:

  1. 源代码的重新分发必须保留上述版权声明、本条件列表和以下免责声明。

  2. 二进制形式的重新分发必须在使用提供的文档和其他材料中复制上述版权声明、本条件列表和以下免责声明。

  3. 未经版权所有者或其贡献者事先书面许可,不得使用其名称或其贡献者的名称来认可或推广由此软件衍生出的产品。

本软件由版权所有者和贡献者“按原样”提供,并不承担任何明示或暗示的保证,包括但不限于适销性和针对特定目的的适用性的暗示保证。在任何情况下,版权所有者或贡献者不应对任何直接、间接、偶然、特殊、示范性或后果性的损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)承担责任,无论此类损害是否因使用本软件而引起,即使已告知此类损害的可能性。