Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage
  • Advanced Usage

Was this helpful?

Edit on GitHub
  1. Components

Version

The Version component allows you an easy way to parse and compare versions. It supports both Semversion and package manager syntax.

Installation

composer require sonsofphp/version

Usage

The base Version supports semver standards.

<?php
use SonsOfPHP\Component\Version\Version;

// You can create versions two different ways
$version = new Version('1.2.3');
$version = Version::from('1.2.3');

echo $version; // prints "1.2.3"
$versionString = $version->toString(); // $versionString === "1.2.3"

// Easy API
$major = $version->getMajor(); // $major === 1
$minor = $version->getMinor(); // $minor === 2
$patch = $version->getPatch(); // $patch === 3

// Comparing Versions is easy too
$currentVersion = new Version('1.1.1');
$latestVersion  = new Version('1.1.2');

// if $latestVersion > $currentVersion
if ($latestVersion->isGreaterThan($currentVersion)) {
    // run upgrade
}
// Also supports "isLessThan" and "isEqualTo"

Advanced Usage

You can also use Pre-release and Build Metadata with your versions.

<?php
use SonsOfPHP\Component\Version\Version;

// Create a new Version that includes Pre-release and/or Build Metadata
$version = new Version('1.2.3-RC1+buildMetaData');

// You can grab the info as well
$preRelease = $version->getPreRelease(); // $preRelease === "RC1"
$build = $version->getBuild(); // $build === "RC1"
PreviousState MachineNextContributing Overview

Last updated 8 months ago

Was this helpful?

📦