icmbio/validate-xpath-expression 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

icmbio/validate-xpath-expression

最新稳定版本:v1.0.1

Composer 安装命令:

composer require icmbio/validate-xpath-expression

包简介

README 文档

README

Português do Brasil

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, and null
  • Arithmetic: +, -, *, div, mod
  • Comparison: =, !=, <, <=, >, >=
  • Boolean operators: and, or
  • Parentheses
  • Function calls registered in FunctionRegistry

Built-in functions

  • selected
  • string-length
  • string_length
  • int
  • floor
  • ceiling
  • number
  • string
  • contains
  • starts-with
  • normalize-space
  • choose
  • not
  • true
  • false
  • uuid
  • format-date-time
  • substring-after
  • substring-before

Architecture overview

The library is intentionally split into small objects:

Documentation

The repository includes deeper documentation in two languages.

English:

Portuguese (Brazil):

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:

  • phpstan for static analysis
  • PHPUnit for automated tests
  • coverage generation when pcov or xdebug is 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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固