diff --git a/CHANGELOG.md b/CHANGELOG.md index a5f20dc..88e98cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [[v0.4.1](https://github.com/The-FireHub-Project/Runtime/compare/v0.4.0...v0.4.1)] – 2026-10-30 + +### Added +* Add `Filter` class for PHP runtime filtering utilities in `Runtime` by @dieselxxx + ## [[v0.4.0](https://github.com/The-FireHub-Project/Runtime/compare/v0.3.3...v0.4.0)] – 2026-10-29 ### Added diff --git a/README.md b/README.md index 87a1ac4..f9fc763 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ PHP Runtime Foundation
release version @@ -22,25 +22,46 @@ /> -[![PHPStan](https://github.com/The-FireHub-Project/Runtime/actions/workflows/PHPStan.yml/badge.svg?branch=develop)](https://github.com/The-FireHub-Project/Runtime/actions/workflows/PHPStan.yml) -[![PHPUnit](https://github.com/The-FireHub-Project/Runtime/actions/workflows/PHPUnit.yml/badge.svg?branch=develop)](https://github.com/The-FireHub-Project/Runtime/actions/workflows/PHPUnit.yml) -[![Codecov](https://codecov.io/gh/The-FireHub-Project/Runtime/branch/develop/graph/badge.svg?token=XW2YEONF51)](https://app.codecov.io/gh/The-FireHub-Project/Runtime/tree/develop) -

- + + GitHub created at + GitHub last commit (branch) - + GitHub activity (branch) - + + GitHub contributors + GitHub commit difference between two branches + + GitHub top language + + GitHub repo size + + GitHub code size

@@ -54,47 +75,55 @@ Its role is to provide a consistent execution foundation between native PHP capa --- -## FireHub Icon Development Branch +## FireHub Icon Repository Purpose -⚠️ **This is the `develop` branch** +This repository provides a lightweight PHP runtime abstraction layer that wraps native PHP functionality into clean, consistent, and deterministic APIs. -- Unstable -- APIs may change without notice -- Not intended for production use +Its purpose is to reduce boilerplate and standardize common execution patterns across the FireHub ecosystem by offering ergonomic helper classes for arrays, strings, files, environment access, and other low-level runtime operations. -## FireHub Icon Branch Purpose +It is strictly focused on execution-level concerns and does not contain domain logic, business rules, or framework-level orchestration. It exists to improve developer productivity while preserving native PHP behavior and ensuring zero framework coupling. -The `develop` branch is the **primary integration branch** for all ongoing development. +## FireHub Icon Security -It serves as the staging area where: -- Feature branches are merged -- Bug fixes are integrated -- Experimental work is stabilized -- Code is prepared for upcoming releases +For security reporting and guidelines, please refer to our [SECURITY.md](https://github.com/The-FireHub-Project/.github/blob/master/.github/SECURITY.md) file. -All **release branches** are created **from `develop`**. +## FireHub Icon Community -## Stability Guarantee +![GitHub watchers](https://img.shields.io/github/watchers/The-FireHub-Project/Runtime?style=social) +![GitHub Repo stars](https://img.shields.io/github/stars/The-FireHub-Project/Runtime?style=social) +![GitHub forks](https://img.shields.io/github/forks/The-FireHub-Project/Runtime?style=social) -❌ No backward compatibility guarantee -❌ APIs may change without notice -❌ Behavior may be incomplete or inconsistent -❌ Breaking changes are expected +- Be sure to review the [Code of Conduct](https://github.com/The-FireHub-Project/.github/blob/master/.github/CODE_OF_CONDUCT.md). +- Read a [Contributing](https://github.com/The-FireHub-Project/.github/blob/master/.github/CONTRIBUTING.md) file to see how you can contribute to this project. +- Read our [Security policy](https://github.com/The-FireHub-Project/.github/blob/master/.github/SECURITY.md) file to see how you can report a vulnerability. +- Review the project [Governance model](https://github.com/The-FireHub-Project/.github/blob/master/.github/GOVERNANCE.md), including roles, decision-making, RFC process, and releases. -This branch is intended **only for contributors and advanced testers**. +## FireHub Icon Support -## FireHub Icon Composer Usage (Not Recommended) +

+ + GitHub in-progress issues + + GitHub total issues + + GitHub opened pull requests +

-For internal testing only: +For help with FireHub products, please refer to the following resources: -```json -{ - "require": { - "the-firehub-project/runtime": "dev-develop" - } -} -``` -⚠️ Never use dev-develop in production. +- **General questions & discussions**: [GitHub Discussions](https://github.com/orgs/The-FireHub-Project/discussions) +- **Bug reports & issues**: [Support](https://github.com/The-FireHub-Project/Runtime/issues) +- **Documentation & guides**: [FireHub Docs](https://the-firehub-project.github.io) +- **Commercial support** (Pro/Enterprise): contact your account manager ## FireHub Icon Authors and Contributors diff --git a/src/Filter.php b/src/Filter.php new file mode 100644 index 0000000..b88799c --- /dev/null +++ b/src/Filter.php @@ -0,0 +1,222 @@ + + * @copyright 2026-present The FireHub Project - All rights reserved + * @license https://opensource.org/license/Apache-2-0 Apache License, Version 2.0 + * + * @php-version >=8.2 + * @package Runtime + */ + +namespace FireHub\Runtime; + +use FireHub\Core\Boundary\Runtime\NativeRuntime; + +use function filter_has_var; +use function filter_id; +use function filter_input; +use function filter_input_array; +use function filter_list; +use function filter_var; +use function filter_var_array; + +/** + * ### PHP Runtime Filter Utilities + * + * Provides low-level wrappers for native PHP filtering functions. + * + * Supports validation, sanitization, and inspection of filter types. + * @since 1.0.0 + */ +final readonly class Filter extends NativeRuntime { + + /** + * ### Filters a variable with a specified filter + * @since 1.0.0 + * + * @param mixed $value

+ * Value to filter. + *

+ * @param int $filter [optional]

+ * The filter to apply. Can be a validation filter by using one of the FILTER_VALIDATE_* constants, a + * sanitization filter by using one of the FILTER_SANITIZE_* or FILTER_UNSAFE_RAW, or a custom filter by using + * FILTER_CALLBACK. + *

+ * @param array|int $options [optional]

+ * Either an associative array of options, or a bitmask of filter flag constants FILTER_FLAG_*. + * + * If the filter accepts options, flags can be provided by using the "flags" field of array. + *

+ * + * @return mixed On success returns the filtered data. On failure false is returned, unless the + * FILTER_NULL_ON_FAILURE flag is used, in which case null is returned. + */ + public static function value (mixed $value, int $filter = FILTER_UNSAFE_RAW, array|int $options = 0):mixed { + + return filter_var($value, $filter, $options); + + } + + /** + * ### Gets multiple variables and optionally filters them + * @since 1.0.0 + * + * @param array $array

+ * An associative array containing the data to filter. + *

+ * @param array|int $options [optional]

+ * Either an associative array of options, or the filter to apply to each entry, which can either be a validation + * filter by using one of the FILTER_VALIDATE_* constants, or a sanitization filter by using one of the + * FILTER_SANITIZE_* constants. + * + * The option array is an associative array where the key corresponds to a key in the input array and the + * associated value is either the filter to apply to this entry, or an associative array describing how and which + * filter should be applied to this entry. + * + * The associative array describing how a filter should be applied must contain the 'filter' key whose associated + * value is the filter to apply, which can be one of the FILTER_VALIDATE_*, FILTER_SANITIZE_*, FILTER_UNSAFE_RAW, + * or FILTER_CALLBACK constants. It can optionally contain the 'flags' key which specifies any flags that apply + * to the filter, and the 'options' key which specifies any options that apply to the filter. + *

+ * @param bool $add_empty [optional]

+ * Add missing keys as null to the return value. + *

+ * + * @return array|false|null On success, an array containing the values of the requested + * variables. On failure, false is returned. Missing entries from the input array are added to the returned array + * as null if add_empty is true, and are omitted entirely if it is false. An entry of the returned array will be + * false if the filter fails, unless the FILTER_NULL_ON_FAILURE flag is used, in which case it will be null. With + * the FILTER_FORCE_ARRAY flag, that failure value is wrapped in a one element array like any other result. + * + * @phpstan-ignore-next-line + */ + public static function values (array $array, array|int $options = FILTER_UNSAFE_RAW, bool $add_empty = true):array|false|null { + + return filter_var_array($array, $options, $add_empty); + + } + + /** + * ### Returns the filter ID belonging to a named filter + * @since 1.0.0 + * + * @param string $name

+ * Name of a filter to get. + *

+ * + * @return int|false D of a filter on success or false if filter doesn't exist. + */ + public static function id (string $name):int|false { + + return filter_id($name); + + } + + /** + * ### Gets a specific external variable by name and optionally filters it + * @since 1.0.0 + * + * @param 0|1|2|4|5 $type

+ * One of the INPUT_* constants. + *

+ * @param string $name

+ * Name of a variable to filter inside the corresponding type superglobal. + *

+ * @param int $filter [optional]

+ * The filter to apply. Can be a validation filter by using one of the FILTER_VALIDATE_* constants, a + * sanitization filter by using one of the FILTER_SANITIZE_* or FILTER_UNSAFE_RAW, or a custom filter by using + * FILTER_CALLBACK. + *

+ * @param array|int $options [optional]

+ * Either an associative array of options, or a bitmask of filter flag constants FILTER_FLAG_*. + * + * If the filter accepts options, flags can be provided by using the "flags" field of array. + *

+ * + * @return mixed Value of the requested variable on success, false if the filter fails, or null if the var_name + * variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns false if the variable is not set + * and null if the filter fails. + */ + public static function input (int $type, string $name, int $filter = FILTER_UNSAFE_RAW, array|int $options = 0):mixed { + + return filter_input($type, $name, $filter, $options); + + } + + /** + * ### Gets external variables and optionally filters them + * @since 1.0.0 + * + * @param 0|1|2|4|5 $type

+ * One of the INPUT_* constants. + *

+ * @param array|int $options [optional]

+ * Either an associative array of options, or the filter to apply to each entry, which can either be a + * validation filter by using one of the FILTER_VALIDATE_* constants, or a sanitization filter by using one of + * the FILTER_SANITIZE_* constants. + * + * The option array is an associative array where the key corresponds to a key in the input array and the + * associated value is either the filter to apply to this entry, or an associative array describing how and which + * filter should be applied to this entry. + * + * The associative array describing how a filter should be applied must contain the 'filter' key whose associated + * value is the filter to apply, which can be one of the FILTER_VALIDATE_*, FILTER_SANITIZE_*, FILTER_UNSAFE_RAW, + * or FILTER_CALLBACK constants. It can optionally contain the 'flags' key which specifies any flags that apply + * to the filter, and the 'options' key which specifies any options that apply to the filter. + *

+ * @param bool $add_empty [optional]

+ * Add missing keys as null to the return value. + *

+ * + * @return array|false|null On success, an array containing the values of the requested + * variables. On failure, false is returned. If the input array designated by type is not populated, null is + * returned instead. Missing entries from the input array are added to the returned array as null if add_empty is + * true, and are omitted entirely if it is false. Unlike filter_input(), the FILTER_NULL_ON_FAILURE flag does not + * change this: a missing entry is always null. An entry of the returned array will be false if the filter fails, + * unless the FILTER_NULL_ON_FAILURE flag is used, in which case it will be null. With the FILTER_FORCE_ARRAY + * flag, that failure value is wrapped in a one element array like any other result. + * + * @phpstan-ignore-next-line + */ + public static function inputs (int $type, array|int $options = FILTER_UNSAFE_RAW, bool $add_empty = true):array|false|null { + + return filter_input_array($type, $options, $add_empty); + + } + + /** + * ### Checks if a variable of the specified type exists + * @since 1.0.0 + * + * @param int $type

+ * One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. + *

+ * @param string $name

+ * Name of a variable to check. + *

+ * + * @return bool Returns true on success or false on failure. + */ + public static function has (int $type, string $name):bool { + + return filter_has_var($type, $name); + + } + + /** + * ### Returns a list of all supported filters + * @since 1.0.0 + * + * @return list Returns an array of names of all supported filters, empty array if there are no such + * filters. Indexes of this array are not filter IDs, they can be obtained with filter_id() from a name instead. + */ + public static function all ():array { + + return filter_list(); + + } + +} \ No newline at end of file