Skip to content

Commit

Permalink
Added SemanticComparatorInterface and SemanticVersionInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayne committed Jul 22, 2016
1 parent 0adfab4 commit 5aa7931
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 36 deletions.
29 changes: 11 additions & 18 deletions src/SemanticComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@
* @since 1.0.0-rc.1
* @see http://semver.org
*/
class SemanticComparator
class SemanticComparator implements SemanticComparatorInterface
{
/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
* @see compare()
* @inheritdoc
*/
public function __invoke(SemanticVersion $left, SemanticVersion $right)
public function __invoke(SemanticVersionInterface $left, SemanticVersionInterface $right)
{
return $this->compare($left, $right);
}

/**
* Build metadata is ignored when determining version precedence.
*
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int `0` if both versions are equal, `< 0` if `$left` is smaller and `> 0` if `$left` is greater.
* @inheritdoc
*/
public function compare(SemanticVersion $left, SemanticVersion $right)
public function compare(SemanticVersionInterface $left, SemanticVersionInterface $right)
{
$result = $this->compareMajorMinorPatch($left, $right);

Expand All @@ -47,11 +40,11 @@ public function compare(SemanticVersion $left, SemanticVersion $right)
}

/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @param SemanticVersionInterface $left
* @param SemanticVersionInterface $right
* @return int
*/
private function compareMajorMinorPatch(SemanticVersion $left, SemanticVersion $right)
private function compareMajorMinorPatch(SemanticVersionInterface $left, SemanticVersionInterface $right)
{
$result = $left->getMajor() - $right->getMajor();

Expand All @@ -67,11 +60,11 @@ private function compareMajorMinorPatch(SemanticVersion $left, SemanticVersion $
}

/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @param SemanticVersionInterface $left
* @param SemanticVersionInterface $right
* @return int
*/
private function comparePre(SemanticVersion $left, SemanticVersion $right)
private function comparePre(SemanticVersionInterface $left, SemanticVersionInterface $right)
{
if ($left->getPre() === '') {
return $right->getPre() === '' ? 0 : 1;
Expand Down
36 changes: 36 additions & 0 deletions src/SemanticComparatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\SemanticVersioning;

/**
* Semantic Versioning Comparator.
*
* @since 1.0.0-rc.3
* @see http://semver.org
*/
interface SemanticComparatorInterface
{
/**
* @param SemanticVersionInterface $left
* @param SemanticVersionInterface $right
* @return int
* @see compare()
*/
public function __invoke(SemanticVersionInterface $left, SemanticVersionInterface $right);

/**
* Build metadata is ignored when determining version precedence.
*
* @param SemanticVersionInterface $left
* @param SemanticVersionInterface $right
* @return int `0` if both versions are equal, `< 0` if `$left` is smaller and `> 0` if `$left` is greater.
*/
public function compare(SemanticVersionInterface $left, SemanticVersionInterface $right);
}
22 changes: 10 additions & 12 deletions src/SemanticVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @since 1.0.0-rc.1
* @see http://semver.org
*/
class SemanticVersion
class SemanticVersion implements SemanticVersionInterface
{
/**
* @var int
Expand Down Expand Up @@ -81,7 +81,7 @@ public function __construct($version)
}

/**
* @return string
* @inheritdoc
*/
public function __toString()
{
Expand Down Expand Up @@ -129,65 +129,63 @@ private function buildException()
}

/**
* @return int Non-negative integer without leading zeroes.
* @inheritdoc
*/
public function getMajor()
{
return $this->major;
}

/**
* @return int Non-negative integer without leading zeroes.
* @inheritdoc
*/
public function getMinor()
{
return $this->minor;
}

/**
* @return int Non-negative integer without leading zeroes.
* @inheritdoc
*/
public function getPatch()
{
return $this->patch;
}

/**
* @return string Optional pre-release information.
* @inheritdoc
*/
public function getPre()
{
return $this->pre;
}

/**
* @return string[]
* @since 1.0.0-rc.2
* @inheritdoc
*/
public function getPreStack()
{
return $this->preStack;
}

/**
* @return string Optional metadata.
* @inheritdoc
*/
public function getMeta()
{
return $this->meta;
}

/**
* @return string[]
* @since 1.0.0-rc.2
* @inheritdoc
*/
public function getMetaStack()
{
return $this->metaStack;
}

/**
* @return string
* @inheritdoc
*/
public function getVersion()
{
Expand Down
68 changes: 68 additions & 0 deletions src/SemanticVersionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* (c) Dennis Meckel.
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\SemanticVersioning;

/**
* Interprets the semantic versioning format `MAJOR.MINOR.PATCH-PRE+META`.
*
* @since 1.0.0-rc.3
* @see http://semver.org
*/
interface SemanticVersionInterface
{
/**
* @return string
*/
public function __toString();

/**
* @return int Non-negative integer without leading zeroes.
*/
public function getMajor();

/**
* @return int Non-negative integer without leading zeroes.
*/
public function getMinor();

/**
* @return int Non-negative integer without leading zeroes.
*/
public function getPatch();

/**
* @return string Optional pre-release information.
*/
public function getPre();

/**
* @return string[]
*
* @since 1.0.0-rc.2
*/
public function getPreStack();

/**
* @return string Optional metadata.
*/
public function getMeta();

/**
* @return string[]
*
* @since 1.0.0-rc.2
*/
public function getMetaStack();

/**
* @return string
*/
public function getVersion();
}
2 changes: 2 additions & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
*/

require_once __DIR__ . '/NoSemanticVersionException.php';
require_once __DIR__ . '/SemanticComparatorInterface.php';
require_once __DIR__ . '/SemanticComparator.php';
require_once __DIR__ . '/SemanticVersionInterface.php';
require_once __DIR__ . '/SemanticVersion.php';
12 changes: 6 additions & 6 deletions tests/SemanticComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public function provideEqualVersions()

/**
* @dataProvider provideEqualVersions
* @param SemanticVersion $left
* @param SemanticVersion $right
* @param SemanticVersionInterface $left
* @param SemanticVersionInterface $right
*/
public function testEqualComparison(SemanticVersion $left, SemanticVersion $right)
public function testEqualComparison(SemanticVersionInterface $left, SemanticVersionInterface $right)
{
$comparator = new SemanticComparator();

Expand All @@ -102,10 +102,10 @@ public function testEqualComparison(SemanticVersion $left, SemanticVersion $righ

/**
* @dataProvider provideSmallerGreaterVersions
* @param SemanticVersion $smaller
* @param SemanticVersion $greater
* @param SemanticVersionInterface $smaller
* @param SemanticVersionInterface $greater
*/
public function testNotEqualComparison(SemanticVersion $smaller, SemanticVersion $greater)
public function testNotEqualComparison(SemanticVersionInterface $smaller, SemanticVersionInterface $greater)
{
$comparator = new SemanticComparator();

Expand Down

0 comments on commit 5aa7931

Please sign in to comment.