icmbio/validate-xpath-expression
Composer 安装命令:
composer require icmbio/validate-xpath-expression
包简介
README 文档
README
A small PHP library for validating and evaluating a focused XPath-like expression dialect against a value and an optional context array.
This project was inspired by the API style of the Python library znc-sistemas/xpath_validator. The PHP implementation in this repository is independent, but the public usage style follows the same idea of validating expressions against runtime values.
Why this project exists
This repository isolates XPath-style validation behavior that was previously embedded in another project. The goal is to keep expression validation small, testable, reusable, and safe to evolve on its own.
Features
- Static convenience API via
Xpath::validate(...) - Instance-based execution via
new Xpath(...)->execute() - Value placeholder support with
. - Context interpolation with
${name} - Built-in XPath-like functions mapped to PHP handlers
- Custom expression parser and evaluator
- No
eval() - Positive and rejection-oriented test coverage
Installation
If the package is published to Packagist, you can install it with:
composer require icmbio/validate-xpath-expression
If you are consuming the repository directly before publication, add it as a VCS repository in your composer.json and require the package from there.
Quick start
<?php use Icmbio\ValidateXpathExpression\Xpath; $isValid = Xpath::validate('. >= ${min} and . <= ${max}', 10, [ 'min' => 1, 'max' => 100, ]); var_dump($isValid); // true
Returning the raw evaluated result instead of a boolean:
<?php use Icmbio\ValidateXpathExpression\Xpath; $length = Xpath::validate('string-length(.)', 'abacate', [], false); var_dump($length); // 7
If you prefer a function-style API similar to the Python-inspired usage style, the package also exposes namespaced helpers:
<?php use function Icmbio\ValidateXpathExpression\validate; $isValid = validate('. >= ${min} and . <= ${max}', 10, [ 'min' => 1, 'max' => 100, ]);
Supported expression model
This library intentionally supports a focused subset instead of full XPath.
Supported pieces include:
- Numbers and quoted strings
true,false, andnull- Arithmetic:
+,-,*,div,mod - Comparison:
=,!=,<,<=,>,>= - Boolean operators:
and,or - Parentheses
- Function calls registered in
FunctionRegistry
Built-in functions
selectedstring-lengthstring_lengthintfloorceilingnumberstringcontainsstarts-withnormalize-spacechoosenottruefalseuuidformat-date-timesubstring-aftersubstring-before
Architecture overview
The library is intentionally split into small objects:
Xpath: public entry point and orchestratorExpressionPreparer: normalizes placeholders and operatorsExpressionTokenizer: converts the prepared expression into tokensExpressionEvaluator: parses and evaluates tokensFunctionRegistry: maps XPath-like function names to handler classessrc/Functions: executable function handlers
Documentation
The repository includes deeper documentation in two languages.
English:
- Overview
- Architecture
- Built-in Functions
- Xpath object
- FunctionRegistry object
- ExpressionPreparer object
- ExpressionTokenizer object
- ExpressionEvaluator object
Portuguese (Brazil):
- Visão geral
- Arquitetura
- Funções nativas
- Objeto Xpath
- Objeto FunctionRegistry
- Objeto ExpressionPreparer
- Objeto ExpressionTokenizer
- Objeto ExpressionEvaluator
These pages are also suitable as a base for a GitHub Wiki.
Development
Useful commands:
composer repl
composer test
composer test:coverage
composer stan
composer check
composer test:coverage requires a local coverage driver such as pcov or xdebug.
Local guard rails
This repository includes a few guard rails for safer development:
phpstanfor static analysis- PHPUnit for automated tests
- coverage generation when
pcovorxdebugis available locally - versioned Git hooks under
.githooks - GitHub Actions CI under
.github/workflows/ci.yml
To enable the local Git hooks:
git config core.hooksPath .githooks
REPL
The repository ships with a simple REPL for trying expressions against the loaded project runtime:
composer repl
Inside the REPL you can call:
validate(...)escape_expression(...)Xpath::validate(...)new Xpath(...)->execute()
Special commands:
:help:clear:quit
If readline is available, the REPL also provides command history and basic autocomplete.
License
MIT
icmbio/validate-xpath-expression 适用场景与选型建议
icmbio/validate-xpath-expression 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 icmbio/validate-xpath-expression 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 icmbio/validate-xpath-expression 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 59
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-02