{"id":26190936,"url":"https://github.com/gregpriday/php-version","last_synced_at":"2026-02-16T11:32:14.285Z","repository":{"id":280989648,"uuid":"943793772","full_name":"gregpriday/php-version","owner":"gregpriday","description":"A simple, powerful PHP class for parsing, validating, and comparing semantic version strings.","archived":false,"fork":false,"pushed_at":"2025-03-06T13:18:22.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-11-23T13:19:54.438Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gregpriday.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-06T09:22:43.000Z","updated_at":"2025-03-06T13:17:54.000Z","dependencies_parsed_at":"2025-03-06T11:46:43.650Z","dependency_job_id":"26c61500-f8af-4cec-9140-5db7deebef49","html_url":"https://github.com/gregpriday/php-version","commit_stats":null,"previous_names":["gregpriday/php-version"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/gregpriday/php-version","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gregpriday%2Fphp-version","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gregpriday%2Fphp-version/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gregpriday%2Fphp-version/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gregpriday%2Fphp-version/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gregpriday","download_url":"https://codeload.github.com/gregpriday/php-version/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gregpriday%2Fphp-version/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29506767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-16T09:05:14.864Z","status":"ssl_error","status_checked_at":"2026-02-16T08:55:59.364Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-12T00:58:45.925Z","updated_at":"2026-02-16T11:32:14.269Z","avatar_url":"https://github.com/gregpriday.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Version\n\nA simple yet powerful library for parsing, validating, comparing, and manipulating semantic version strings in PHP. It also includes flexible support for version constraints (e.g., `^1.2.3`, `\u003e=1.0.0 \u003c2.0.0`) to check whether a particular version satisfies one or more complex conditions.\n\n## Installation\n\n```bash\ncomposer require gregpriday/php-version\n```\n\n## Overview\n\nThis library offers:\n\n- **Strict or Loose Parsing** of version strings (e.g. `\"1.2.3\"`, `\"v1.2.3\"`, `\"1.2\"`, `\"1\"`).\n- **Version Object** to access and modify version components (major, minor, patch, pre-release, and build metadata).\n- **SemVer Checks** to see if a version is stable or a pre-release.\n- **Version Bumping/Lowering** (increment/decrement major, minor, patch) including preserving or clearing pre-release/build metadata.\n- **Constraint Parsing and Evaluation** using a fluent, chainable syntax with logical **AND** and **OR** conditions.\n\n## Basic Usage Example\n\nBelow is a quick snapshot of how you might use the library to parse a version, check its properties, bump the version, and validate it against constraints.\n\n```php\nuse GregPriday\\Version\\Version;\nuse GregPriday\\Version\\Constraint\\VersionConstraintParser;\n\n// 1. Create a version object (strict mode by default).\n$version = new Version('1.2.3-beta');\n\n// 2. Inspect the version\necho \"Major: \" . $version-\u003egetMajor() . \"\\n\";       // 1\necho \"Minor: \" . $version-\u003egetMinor() . \"\\n\";       // 2\necho \"Patch: \" . $version-\u003egetPatch() . \"\\n\";       // 3\necho \"Pre-release: \" . $version-\u003egetPreRelease() . \"\\n\"; // beta\n\n// 3. Check stability\nif ($version-\u003eisStable()) {\n    echo \"Version is stable.\\n\";\n} else {\n    echo \"Version is not stable.\\n\";\n}\n\n// 4. Bump the version (bump minor, reset patch to 0, remove pre-release)\n$bumped = $version-\u003ebumpMinor();\necho \"Bumped Version: \" . $bumped-\u003egetExtraInfo()['version'] . \"\\n\"; // \"1.3.0\"\n\n// 5. Parse constraints and check if a version satisfies them\n$parser = new VersionConstraintParser();\n$rangeSet = $parser-\u003eparseConstraints('\u003e=1.0.0 \u003c2.0.0 || ^3.0.0');\n\nif ($rangeSet-\u003eisSatisfiedBy($bumped)) {\n    echo $bumped-\u003egetExtraInfo()['version'].\" satisfies the constraint.\\n\";\n} else {\n    echo $bumped-\u003egetExtraInfo()['version'].\" does not satisfy the constraint.\\n\";\n}\n```\n\n---\n\n## Creating and Inspecting Versions\n\n### Strict vs. Loose Parsing\n\n- **Strict mode** (default): Expects full `major.minor.patch` (optionally `-preRelease` and/or `+buildMetadata`). Examples of valid strict versions:\n    - `1.0.0`\n    - `0.9.5-alpha+build.1`\n- **Loose mode**: More lenient. Accepts shorter forms and can include a leading `v`. Examples of acceptable loose versions:\n    - `v1.2.3`\n    - `1.2` (interpreted as `1.2.0`)\n    - `1` (interpreted as `1.0.0`)\n\n```php\n// Strict mode (throws InvalidArgumentException if invalid)\n$strictVersion = new Version('1.2.3'); \n\n// Loose mode\n$looseVersion = new Version('v1.2', null, false);\n// Internally becomes 1.2.0\n```\n\n### Accessing Components\n\n```php\n$version = new Version('1.2.3-alpha+build.123');\n\n// Basic components\necho $version-\u003egetMajor();      // 1\necho $version-\u003egetMinor();      // 2\necho $version-\u003egetPatch();      // 3\n\n// Pre-release and build metadata\necho $version-\u003egetPreRelease();    // alpha\necho $version-\u003egetBuildMetadata(); // build.123\n\n// Extra info array (includes stability check)\n$info = $version-\u003egetExtraInfo();\nprint_r($info);\n/*\nArray\n(\n    [version] =\u003e 1.2.3-alpha+build.123\n    [major] =\u003e 1\n    [minor] =\u003e 2\n    [patch] =\u003e 3\n    [pre_release] =\u003e alpha\n    [build_metadata] =\u003e build.123\n    [is_stable] =\u003e \n)\n*/\n```\n\n### Stability and Pre-Release Checks\n\n```php\n$version = new Version('1.0.0-rc1');\nif ($version-\u003eisStable()) {\n    // Major \u003e= 1 and no pre-release\n    echo \"Stable release.\\n\";\n} else {\n    echo \"Not stable.\\n\"; // This will run in this example\n}\n\nif ($version-\u003eisPreRelease()) {\n    echo \"It's a pre-release!\\n\"; // True for \"1.0.0-rc1\"\n}\n```\n\n---\n\n## Bumping and Lowering Version Numbers\n\nThe `VersionBumpingTrait` gives you methods to increment or decrement specific parts of a version. Each method returns a **new** `Version` instance. By default, these operations **clear** pre-release and build metadata, but you can preserve them if you wish.\n\n### Bumping (Incrementing)\n\n```php\n$version = new Version('1.2.3-beta+build.123');\n\n// Bump Major: becomes 2.0.0 (clears pre-release \u0026 build by default)\n$bumpedMajor = $version-\u003ebumpMajor();\necho $bumpedMajor-\u003egetExtraInfo()['version']; // 2.0.0\n\n// Bump Minor but preserve pre-release and build metadata\n$bumpedMinor = $version-\u003ebumpMinor(true, true);\necho $bumpedMinor-\u003egetExtraInfo()['version']; // 1.3.0-beta+build.123\n\n// Bump Patch: 1.2.4 (default clears pre-release and build)\n$bumpedPatch = $version-\u003ebumpPatch();\necho $bumpedPatch-\u003egetExtraInfo()['version']; // 1.2.4\n\n// Bump (or set) Pre-release: \n// - If no pre-release, sets the given identifier. \n// - If pre-release is something like \"beta.1\", it increments the last number.\n$newPre = $version-\u003ebumpPreRelease('alpha', true);\necho $newPre-\u003egetExtraInfo()['version']; // \"1.2.3-beta.1+build.123\"\n```\n\n### Lowering (Decrementing)\n\nYou can similarly reduce major, minor, or patch. By default, these also clear pre-release/build metadata, unless you preserve them.\n\n```php\n$version = new Version('2.3.4-beta+build.456');\n\n// Lower Major, resetting minor/patch, discarding pre-release/build\n$loweredMajor = $version-\u003elowerMajor(true, true);\necho $loweredMajor-\u003egetExtraInfo()['version']; // \"1.0.0\"\n\n// Lower Minor, preserving pre-release\n$loweredMinor = $version-\u003elowerMinor(false, true);\necho $loweredMinor-\u003egetExtraInfo()['version']; // \"2.2.4-beta\"\n\n// Lower Patch, preserving build metadata\n$loweredPatch = $version-\u003elowerPatch(false, true);\necho $loweredPatch-\u003egetExtraInfo()['version']; // \"2.3.3+build.456\"\n```\n\n\u003e **Note**: Lowering a `0` major/minor/patch throws an exception, since negative version segments are invalid.\n\n---\n\n## Parsing and Evaluating Constraints\n\nThe library can parse powerful **OR** and **AND** constraints using the `VersionConstraintParser`. This enables checks such as `\u003e=1.0.0 \u003c2.0.0 || ^3.0.0`.\n\n### Operators\n\n- **Basic**: `\u003e`, `\u003e=`, `\u003c`, `\u003c=`, `=`, `==`, `!`, `!=`\n- **Caret** `^`: e.g. `^1.2.3` means `\u003e=1.2.3` and `\u003c2.0.0` (for 1.x versions)\n- **Tilde** `~`: e.g. `~1.2.3` means `\u003e=1.2.3` and `\u003c1.3.0`\n\n### Combining Constraints\n\n- **AND**: Use space or commas. For example:  \n  `\u003e=1.0.0 \u003c2.0.0`  \n  `\u003e=1.0.0, \u003c2.0.0`  \n  This means the version must satisfy **both** constraints.\n- **OR**: Split with `||`. For example:  \n  `^1.0.0 || ^2.0.0`  \n  Means the version must satisfy **either** `^1.0.0` **or** `^2.0.0`.\n\n### Example: Parsing Constraints\n\n```php\nuse GregPriday\\Version\\Constraint\\VersionConstraintParser;\nuse GregPriday\\Version\\Version;\n\n$parser = new VersionConstraintParser();\n\n// Single constraint\n$rangeSet = $parser-\u003eparseConstraints('\u003e=1.2.3');\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('1.2.3'))); // true\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('1.2.2'))); // false\n\n// Multiple (AND) constraints\n$rangeSet = $parser-\u003eparseConstraints('\u003e=1.0.0 \u003c2.0.0');\n// This means: version \u003e= 1.0.0 AND version \u003c 2.0.0\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('1.5.0'))); // true\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('2.1.0'))); // false\n\n// OR constraints\n$rangeSet = $parser-\u003eparseConstraints('^1.0.0 || ~2.0.0');\n// This means: (version in ^1.0.0) OR (version in ~2.0.0)\n// ^1.0.0 = \u003e=1.0.0 \u003c2.0.0\n// ~2.0.0 = \u003e=2.0.0 \u003c2.1.0\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('1.9.9'))); // true\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('2.0.5'))); // true\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('2.1.0'))); // false\n\n// Negation examples\n$rangeSet = $parser-\u003eparseConstraints('!=1.0.0');\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('1.0.0'))); // false\nvar_dump($rangeSet-\u003eisSatisfiedBy(new Version('1.0.1'))); // true\n```\n\n---\n\n## Testing\n\nTo run the test suite, clone this repository (or have it locally) and install dev dependencies:\n\n```bash\ncomposer install\n```\n\nThen run:\n\n```bash\nvendor/bin/phpunit\n```\n\nThis runs the PHPUnit tests under `tests/`.\n\n### Code Formatting\n\nA [Laravel Pint](https://github.com/laravel/pint) configuration is included for formatting. To format the code:\n\n```bash\ncomposer format\n```\n\n---\n\n## License\n\nThis project is open source under the [MIT license](LICENSE).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgregpriday%2Fphp-version","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgregpriday%2Fphp-version","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgregpriday%2Fphp-version/lists"}