juzim / todo-txt-parser
PHP 的 todo.txt 解析和验证库
0.1.0
2016-10-20 09:43 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 5.3.*
This package is not auto-updated.
Last update: 2024-09-28 20:09:06 UTC
README
PHP 的 todo.txt 解析和验证库
安装
$ composer require "juzim/todo-txt-parser:~1.0"
用法
$parser = new TodoTxtParser();
$task = $parser->buildTaskFromString('x 2016-11-22 2016-11-20 (A) due:2016-09-05 Tell @me to write the @README for +todoTxtParser');
这将返回一个包含所有值(查看 todo.txt 规则)的 TodoTxtTask 对象
Get raw string:
$task->getOriginalText() // 'x 2016-11-22 (A) 2016-01-02 Tell @me to write the @README for +todoTxtParser due:2016-09-05'
Get clean string (only readable text):
$task->getCleanText() // 'Tell @me to write the @README for +todoTxtParser'
Get metadata:
$task->isCompleted() // starts with 'x ' -> true
$task->getCompletedAt() // YYYY-MM-DD after 'x' -> DateTime/2016-11-22
$task->getPriority() // (A-Z) -> 'A'
$task->getCreatedAt() // YYYY-MM-DD -> DateTime/2016-01-02
$task->getProjects() // + -> ['todoTxtParser']
$task->getContexts() // @ -> ['me', 'README']
$task->getAddOns() // {key}:{text} -> ['due' => '2016-09-05']
以下错误会被收集
ERROR_NO_TEXT_GIVEN -> No text given
ERROR_NO_TASK_DESCRIPTION -> Task is missing a description/only has metadata
ERROR_INVALID_DATE_CREATED_AT -> Invalid created at date
ERROR_INVALID_DATE_COMPLETED_AT -> Invalid completed at date
ERROR_COMPLETED_AT_MISSING -> Task is completed but has no completed at date
ERROR_CREATED_AT_IN_THE_FUTURE -> Created at date is in the future
ERROR_COMPLETED_AT_IN_THE_FUTURE -> Completed at date is in the future
ERROR_DUPLICATE_ADD_ON_KEY -> Duplicate add-on key
ERROR_COMPLETED_AT_BEFORE_CREATED_AT -> Task was completed before it was created
如果至少发生了一个错误,最后会抛出包含所有收集到的错误的可读格式的 TodoTxtValidationException
。可以通过 $parser->ignoreError(TodoTxtParser::ERROR_NAME) 忽略特定的错误。
注意
- 不符合任何规则的元数据成为描述的一部分
- 去除前导/尾随空白和重复空格
- 插件不是干净文本的一部分(不确定这是否正确)
- 描述文本可以是任何内容,所以请相应地进行过滤/转义
- 目前对于项目、上下文和插件没有除了正则表达式之外的规则,但可能会添加一些限制,例如有效字符或长度