Skip to content

Commit

Permalink
Added readme information.
Browse files Browse the repository at this point in the history
Operator class is final now.
Simplify regular expression for version validation.
  • Loading branch information
fractalzombie committed Dec 18, 2018
1 parent edcb49f commit 7598f7c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
55 changes: 52 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
#COMING SOON
Code is good commented.
# The package helps to work with semantic versions.

[![Coverage Status](https://coveralls.io/repos/github/fractalzombie/semver/badge.svg?branch=master)](https://coveralls.io/github/fractalzombie/semver?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fractalzombie/semver/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fractalzombie/semver/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/fractalzombie/semver/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/fractalzombie/semver/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/fractalzombie/semver/badges/build.png?b=master)](https://scrutinizer-ci.com/g/fractalzombie/semver/build-status/master)
[![Code Intelligence Status](https://scrutinizer-ci.com/g/fractalzombie/semver/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)
[![Build Status](https://travis-ci.org/fractalzombie/semver.svg?branch=master)](https://travis-ci.org/fractalzombie/semver)
[![Build Status](https://travis-ci.org/fractalzombie/semver.svg?branch=master)](https://travis-ci.org/fractalzombie/semver)

## Features

* Simple package for semantic version manipulation.
* Compare semantic version.
* Validates semantic version.
* Throws exceptions if Versions are not instance of each other.

## Usage

#### Fractal\SemVer\SemanticVersion class:
```php
<?php

use Fractal\SemVer\SemanticVersion;

## Create instance of SemVer
## If SemVer not valid, throws \Fractal\SemVer\Exceptions\ParseVersionException
$version = SemanticVersion::fromString('1.0.0'); # \Fractal\SemVer\Contracts\VersionInterface
$other = SemanticVersion::fromString('0.1.0'); # \Fractal\SemVer\Contracts\VersionInterface

## Version can compare each other
## If compare operator not valid, throws \Fractal\SemVer\Exceptions\InvalidOperatorException
## If version and other version are not instance of each other, throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
$version->eq($other); # false
$version->gte($other); # true
```

#### Fractal\SemVer\Comparator class:
```php
<?php

use Fractal\SemVer\SemanticVersion;
use Fractal\SemVer\Comparator;

## Create instance of SemVer
## If SemVer not valid, throws \Fractal\SemVer\Exceptions\ParseVersionException
$version = SemanticVersion::fromString('1.0.0'); # \Fractal\SemVer\Contracts\VersionInterface
$other = SemanticVersion::fromString('0.1.0'); # \Fractal\SemVer\Contracts\VersionInterface

## Compare versions with comparator
## If compare operator not valid, throws \Fractal\SemVer\Exceptions\InvalidOperatorException
## If version and other version are not instance of each other, throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
Comparator::eq($version, $other); # false
Comparator::gte($version, $other); # true
```

## License

The SemVer package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion src/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @author Mikhail Shtanko <fractalzombie@gmail.com>
*/
class Operator
final class Operator
{
/**
* Greater than operator.
Expand Down
22 changes: 18 additions & 4 deletions src/SemanticVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ class SemanticVersion implements VersionInterface
*
* @var string
*/
protected const REGEX_PATTERN = '/^([0-9]+)\.([0-9]+)\.([0-9]+)/i';
protected const REGEX_PATTERN = '/(?:\d+\.?){3}/i';

/**
* Semantic version template.
*
* @const string
*/
protected const VERSION_TEMPLATE = 'major.minor.patch';

/**
* Semantic version delimiter.
*
* @const string
*/
protected const VERSION_DELIMITER = '.';

/**
* @var int
Expand Down Expand Up @@ -64,10 +78,10 @@ protected function __construct(int $major, int $minor, int $patch)
public static function fromString(string $version): VersionInterface
{
if (!preg_match(static::REGEX_PATTERN, $version)) {
throw new ParseVersionException('major.minor.patch', $version);
throw new ParseVersionException(static::VERSION_TEMPLATE, $version);
}

[$major, $minor, $patch] = explode('.', $version);
[$major, $minor, $patch] = explode(static::VERSION_DELIMITER, $version);

return new static((int)$major, (int)$minor, (int)$patch);
}
Expand All @@ -81,7 +95,7 @@ public static function fromString(string $version): VersionInterface
*/
public function version(bool $withPatch = true): string
{
return ($withPatch)
return $withPatch
? "{$this->major}.{$this->minor}.{$this->patch}"
: "{$this->major}.{$this->minor}";
}
Expand Down

0 comments on commit 7598f7c

Please sign in to comment.