Giter VIP home page Giter VIP logo

version's Introduction

Version

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version PDS Skeleton

Value Object representing a version number that is in compliance with the Semantic Versioning specification.

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require nikolaposa/version

Usage

Creating a Version object via named constructor and accessing its values

use Version\Version;

$v = Version::fromPreRelease(2, 0, 0, 'alpha');

echo $v->getMajor(); //2
echo $v->getMinor(); //0
echo $v->getPatch(); //0
var_dump($v->getPreRelease()->getIdentifiers()); //array(1) { [0]=> string(1) "alpha" }
echo $v->getPreRelease(); //alpha

Creating a Version object from a string

use Version\Version;

$v = Version::fromString('1.10.0');

echo $v->getVersionString(); //1.10.0

Comparing Version objects

use Version\Version;

$v1 = Version::fromString('1.10.0');
$v2 = Version::fromString('2.3.3');

var_dump($v1->isLessThan($v2)); //bool(true)
var_dump($v1->isGreaterThan($v2)); //bool(false)
var_dump($v1->isEqualTo($v2)); //bool(false)

var_dump($v2->isLessThan($v1)); //bool(false)
var_dump($v2->isGreaterThan($v1)); //bool(true)

Matching Version objects against constraints

use Version\Version;

$v = Version::fromString('2.2.0');

var_dump($v->matches('2.2.0')); //bool(true)
var_dump($v->matches('=2.2.0')); //bool(true)
var_dump($v->matches('!=2.1.0')); //bool(true)
var_dump($v->matches('>=2.0.0 <2.3.0')); //bool(true)
var_dump($v->matches('>=2.0.0 <2.1.0 || 2.2.0')); //bool(true)

Modifying version

use Version\Version;

$v = Version::fromString('1.10.0');

$v1101 = $v->withPatchIncremented();
echo $v1101; //1.10.1

$v1110 = $v1101->withMinorIncremented();
echo $v1110; //1.11.0

$v2 = $v1101->withMajorIncremented();
echo $v2; //2.0.0

$v2Alpha = $v2->withPreRelease('alpha');
echo $v2Alpha; //2.0.0-alpha

$v2Alpha111 = $v2Alpha->withBuild('111');
echo $v2Alpha111; //2.0.0-alpha+111

Version-aware objects

use Version\Version;
use Version\VersionAwareInterface;
use Version\VersionAwareTrait;

class Package implements VersionAwareInterface
{
    use VersionAwareTrait;

    private $name;

    private $description;

    public function __construct($name, $description = '')
    {
        $this->name = $name;
        $this->description = $description;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getDescription()
    {
        return $this->description;
    }
}

$package = new Package('Test');
$package->setVersion(Version::fromString('2.3.3'));

Versions collection

use Version\VersionsCollection;
use Version\Version;

$versions = VersionsCollection::fromArray([
    Version::fromMajor(1),
    '1.1.0',
    '2.3.3',
]);

echo count($versions); //3

$versions->sort(VersionsCollection::SORT_DESC);

//Outputs: 2.3.3, 1.1.0, 1.0.0
foreach ($versions as $version) {
    echo (string) $version;
}

Author

Nikola Poša

Copyright and license

Copyright 2017 Nikola Poša. Released under MIT License - see the LICENSE file for details.

version's People

Contributors

nikolaposa avatar pauci avatar stof avatar mattketmo avatar

Watchers

Nikolya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.