Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
amouhzi committed Oct 18, 2014
0 parents commit ae83c73
Show file tree
Hide file tree
Showing 13 changed files with 823 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/vendor/

/composer.lock
/*.iml

# IDE files
/.idea/

# Eclipse files
*.pydevproject
.metadata
.gradle
/tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Version Helper for PHP projects
===============================

This module normalize versions as Composer do. Parse constraints as Composer do too.
And tests if a version match a constraint.

Example:

```php
<?php

$parser = new \Version\VersionParser();

echo $parser->parseStability('1.2-RC2'); // RC
echo $parser->parseStability('2.0b'); // beta
echo $parser->parseConstraints('1.0'); // stable

echo $parser->normalize('2.0b1'); // 2.0.0.0-beta1

$c = $parser->parseConstraints('>=1.2.5,<2.0');
echo $c->match('1.2.0'); // false
echo $c->match('1.5'); // true
echo $c->match('2.0'); // false
?>
```
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "version/version",
"description": "A library for creating, editing, and comparing semantic versioning numbers.",
"keywords": ["semantic", "version"],
"homepage": "http://github.com/php-mod/version",
"license": "MIT",
"authors": [
{
"name": "Hassan Amouhzi",
"email": "[email protected]",
"homepage": "http://anezi.net"
}
],
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "4.3.*"
},
"autoload": {
"psr-0": {
"Version": "src/"
}
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="true"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
strict="true"
verbose="false">
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="Version Test Suite">
<directory phpVersion="5.3.0"
phpVersionOperator=">="
suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 8 additions & 0 deletions src/Version/Constraint/AbstractConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Version\Constraint;

abstract class AbstractConstraint
{

}
16 changes: 16 additions & 0 deletions src/Version/Constraint/EmptyConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Version\Constraint;

class EmptyConstraint extends AbstractConstraint
{
public function __toString()
{
return '*';
}

public function match()
{
return true;
}
}
34 changes: 34 additions & 0 deletions src/Version/Constraint/MultiConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Version\Constraint;

class MultiConstraint
{
public function __construct(array $minMax, $and = true)
{
$this->minMax = $minMax;
$this->and = $and;
}

public function __toString()
{
return implode($this->and ? ',' : '|', $this->minMax);
}

public function match($version)
{
if($this->and) {
foreach($this->minMax as $c) {
if(!$c->match($version))
return false;
}
return true;
} else {
foreach($this->minMax as $c) {
if($c->match($version))
return true;
}
return false;
}
}
}
33 changes: 33 additions & 0 deletions src/Version/Constraint/VersionConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Version\Constraint;

use Version\VersionParser;

class VersionConstraint extends AbstractConstraint
{
private $operator;
private $version;

public function __construct($operator, $version)
{
$this->operator = $operator;
if(!is_string($version)) {
throw new \InvalidArgumentException('Arg 2 must be a string');
}
$this->version = $version;
}

public function __toString()
{
return $this->operator .
$this->version;
}

public function match($version)
{
$parser = new VersionParser();
$version = $parser->normalize($version);
return version_compare($version, $parser->normalize($this->version), $this->operator);
}
}
Loading

0 comments on commit ae83c73

Please sign in to comment.