Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement JsonSerializable interface #5

Merged
merged 3 commits into from
Jun 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ nbproject
.project
vendor
phpunit.xml
composer.lock
composer.lock
.idea
55 changes: 32 additions & 23 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Version;

use JsonSerializable;
use Version\Metadata\PreRelease;
use Version\Metadata\Build;
use Version\Exception\InvalidVersionElementException;
Expand All @@ -19,7 +20,7 @@
/**
* @author Nikola Posa <posa.nikola@gmail.com>
*/
final class Version
final class Version implements JsonSerializable
{
/**
* @var int
Expand Down Expand Up @@ -232,28 +233,6 @@ public function isBuild()
return !$this->build->isEmpty();
}

/**
* @return string
*/
public function getVersionString()
{
return
$this->major
. '.' . $this->minor
. '.' . $this->patch
. ($this->isPreRelease() ? '-' . (string) $this->preRelease : '')
. ($this->isBuild() ? '+' . (string) $this->build : '')
;
}

/**
* @return string
*/
public function __toString()
{
return $this->getVersionString();
}

/**
* @param self|string $version
* @return int (1 if $this > $version, -1 if $this < $version, 0 if equal)
Expand Down Expand Up @@ -379,4 +358,34 @@ public function withBuild($build)
{
return self::fromAllElements($this->major, $this->minor, $this->patch, $this->preRelease, $build);
}

/**
* @return string
*/
public function getVersionString()
{
return
$this->major
. '.' . $this->minor
. '.' . $this->patch
. ($this->isPreRelease() ? '-' . (string) $this->preRelease : '')
. ($this->isBuild() ? '+' . (string) $this->build : '')
;
}

/**
* @return string
*/
public function __toString()
{
return $this->getVersionString();
}

/**
* @return string
*/
public function jsonSerialize()
{
return $this->getVersionString();
}
}
44 changes: 26 additions & 18 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,6 @@ public static function versionStrings()
];
}

/**
* @dataProvider printedVersionStrings
*/
public function testVersionToStringConversion($output, Version $version)
{
$this->assertEquals($output, (string) $version);
}

public static function printedVersionStrings()
{
return [
['2.1.0', Version::fromPatch(2, 1, 0)],
['1.0.0+20150919', Version::fromString('1.0.0+20150919')],
['1.0.0+exp.sha.5114f85', Version::fromString('1.0.0+exp.sha.5114f85')],
['1.0.0-alpha.1+exp.sha.5114f85', Version::fromString('1.0.0-alpha.1+exp.sha.5114f85')],
];
}

public function testCreationFailsInCaseOfInvalidMajorVersion()
{
$this->setExpectedException(InvalidVersionElementException::class);
Expand Down Expand Up @@ -155,4 +137,30 @@ public function testCreationFromStringFailsInCaseInvalidCorePart()

Version::fromString('1.5.2.4.4');
}

/**
* @dataProvider printedVersionStrings
*/
public function testVersionToStringConversion($output, Version $version)
{
$this->assertEquals($output, (string) $version);
}

/**
* @dataProvider printedVersionStrings
*/
public function testJsonSerialization($output, Version $version)
{
$this->assertEquals('"' . $output . '"', json_encode($version));
}

public static function printedVersionStrings()
{
return [
['2.1.0', Version::fromPatch(2, 1, 0)],
['1.0.0+20150919', Version::fromString('1.0.0+20150919')],
['1.0.0+exp.sha.5114f85', Version::fromString('1.0.0+exp.sha.5114f85')],
['1.0.0-alpha.1+exp.sha.5114f85', Version::fromString('1.0.0-alpha.1+exp.sha.5114f85')],
];
}
}