承接 dsentker/numeric-range-parser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

dsentker/numeric-range-parser

最新稳定版本:0.1.2

Composer 安装命令:

composer require dsentker/numeric-range-parser

包简介

A parser for reading indexed ranges, e.g. page numbers

README 文档

README

A PHP library that provides functionality to parse numeric ranges from a given input string. The library is suitable for user input that wants to select specific page numbers or fetch a custom range of items.

Installation

Install using Composer:

composer require dsentker/numeric-range-parser

Usage

$parser = new DefaultNumericRangeParser();
$result = $parser->parse('1-3;5');
dump($result->toNormalizedArray()); // [1, 2, 3, 5]


// $result is traversable, iterating over indexes is possible too
foreach ($result as $index) {
    echo $index . PHP_EOL;
}

As $result is an instance of an AppendIterator it is possible to append more indexes if required. Also, the getIteratorIndex() method returns the index of the current block (a block is separated by semicolon)

$parser = new DefaultNumericRangeParser();
$result = $parser->parse('4-6; 10'); // two blocks defined here

$result->append(new \ArrayIterator([1-2])); // another block here

foreach ($result as $index) {
    // $result->getIteratorIndex() will count up to 2 (0,1,2)
    printf("Block #%d: Index: #%d", $result->getIteratorIndex(), $result->current());
}

Configure options

__construct(string $rangeSeparator = '-', string $blockSeparator = ';')

The constructor allows you to set custom range and block separators. By default, the range separator is '-' and the block separator is ';'.

$instance = new \DSentker\DefaultNumericRangeParser('..', '/');
$result = $instance->parse('1..4 / 6..10');

Strict parsing

While the DefaultNumericRangeParser is a little more lax on user input, the StrictNumericRangeParser parser is stricter. A RangeException is thrown if the input is incorrect:

$default = new \DSentker\DefaultNumericRangeParser();
$strict = new \DSentker\StrictNumericRangeParser();

$default->parse('10-8'); // No error
$strict->parse('10-8'); // First index is greater than second, RangeException is thrown

$default->parse('8-10;;11'); // No error
$strict->parse('8-10;;11'); // Missing range, RangeException is thrown

$default->parse('8a;10;12'); // No error
$strict->parse('8a;10;12'); // Invalid character, RangeException is thrown

Testing

with PHPUnit:

./vendor/bin/phpunit

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub.

If you have general questions or want to implement a feature, you are welcome to collaborate.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固