Skip to content

Commit ae83c73

Browse files
committed
first commit
0 parents  commit ae83c73

File tree

13 files changed

+823
-0
lines changed

13 files changed

+823
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/vendor/
2+
3+
/composer.lock
4+
/*.iml
5+
6+
# IDE files
7+
/.idea/
8+
9+
# Eclipse files
10+
*.pydevproject
11+
.metadata
12+
.gradle
13+
/tmp/
14+
*.tmp
15+
*.bak
16+
*.swp
17+
*~.nib
18+
local.properties
19+
.settings/
20+
.loadpath
21+
22+
# External tool builders
23+
.externalToolBuilders/
24+
25+
# Locally stored "Eclipse launch configurations"
26+
*.launch
27+
28+
# CDT-specific
29+
.cproject
30+
31+
# PDT-specific
32+
.buildpath
33+
34+
# sbteclipse plugin
35+
.target
36+
37+
# TeXlipse plugin
38+
.texlipse

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Version Helper for PHP projects
2+
===============================
3+
4+
This module normalize versions as Composer do. Parse constraints as Composer do too.
5+
And tests if a version match a constraint.
6+
7+
Example:
8+
9+
```php
10+
<?php
11+
12+
$parser = new \Version\VersionParser();
13+
14+
echo $parser->parseStability('1.2-RC2'); // RC
15+
echo $parser->parseStability('2.0b'); // beta
16+
echo $parser->parseConstraints('1.0'); // stable
17+
18+
echo $parser->normalize('2.0b1'); // 2.0.0.0-beta1
19+
20+
$c = $parser->parseConstraints('>=1.2.5,<2.0');
21+
echo $c->match('1.2.0'); // false
22+
echo $c->match('1.5'); // true
23+
echo $c->match('2.0'); // false
24+
25+
?>
26+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "version/version",
3+
"description": "A library for creating, editing, and comparing semantic versioning numbers.",
4+
"keywords": ["semantic", "version"],
5+
"homepage": "http://github.com/php-mod/version",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Hassan Amouhzi",
10+
"email": "[email protected]",
11+
"homepage": "http://anezi.net"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3.3"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "4.3.*"
19+
},
20+
"autoload": {
21+
"psr-0": {
22+
"Version": "src/"
23+
}
24+
}
25+
}

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="true"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
stopOnError="false"
7+
stopOnFailure="false"
8+
stopOnIncomplete="false"
9+
stopOnSkipped="false"
10+
strict="true"
11+
verbose="false">
12+
<filter>
13+
<whitelist>
14+
<directory suffix=".php">src/</directory>
15+
</whitelist>
16+
</filter>
17+
<testsuites>
18+
<testsuite name="Version Test Suite">
19+
<directory phpVersion="5.3.0"
20+
phpVersionOperator=">="
21+
suffix="Test.php">tests/</directory>
22+
</testsuite>
23+
</testsuites>
24+
</phpunit>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Version\Constraint;
4+
5+
abstract class AbstractConstraint
6+
{
7+
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Version\Constraint;
4+
5+
class EmptyConstraint extends AbstractConstraint
6+
{
7+
public function __toString()
8+
{
9+
return '*';
10+
}
11+
12+
public function match()
13+
{
14+
return true;
15+
}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Version\Constraint;
4+
5+
class MultiConstraint
6+
{
7+
public function __construct(array $minMax, $and = true)
8+
{
9+
$this->minMax = $minMax;
10+
$this->and = $and;
11+
}
12+
13+
public function __toString()
14+
{
15+
return implode($this->and ? ',' : '|', $this->minMax);
16+
}
17+
18+
public function match($version)
19+
{
20+
if($this->and) {
21+
foreach($this->minMax as $c) {
22+
if(!$c->match($version))
23+
return false;
24+
}
25+
return true;
26+
} else {
27+
foreach($this->minMax as $c) {
28+
if($c->match($version))
29+
return true;
30+
}
31+
return false;
32+
}
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Version\Constraint;
4+
5+
use Version\VersionParser;
6+
7+
class VersionConstraint extends AbstractConstraint
8+
{
9+
private $operator;
10+
private $version;
11+
12+
public function __construct($operator, $version)
13+
{
14+
$this->operator = $operator;
15+
if(!is_string($version)) {
16+
throw new \InvalidArgumentException('Arg 2 must be a string');
17+
}
18+
$this->version = $version;
19+
}
20+
21+
public function __toString()
22+
{
23+
return $this->operator .
24+
$this->version;
25+
}
26+
27+
public function match($version)
28+
{
29+
$parser = new VersionParser();
30+
$version = $parser->normalize($version);
31+
return version_compare($version, $parser->normalize($this->version), $this->operator);
32+
}
33+
}

0 commit comments

Comments
 (0)