Skip to content

Commit

Permalink
feat: add semantic version parser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Jan 18, 2021
1 parent 1ce4e3b commit 7b5a9fd
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/SemanticVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace ConventionalChangelog;

class SemanticVersion
{
public const RELEASE_MAJOR = 'major';
public const RELEASE_MINOR = 'minor';
public const RELEASE_PATCH = 'patch';
public const RELEASE_RC = 'rc';
public const RELEASE_BETA = 'beta';
public const RELEASE_ALPHA = 'alpha';

/**
* @var string
*/
protected $version;

/**
* Constructor.
*
* @param $version
*/
public function __construct($version)
{
$this->setVersion($version);
}

/**
* Bump version.
*
* @param string $mode
*/
public function bump($mode): string
{
$version = $this->getVersion();

$newVersion = [0, 0, 0];
$version = preg_replace('#^v#i', '', $version);

// Generate new version code
$split = explode('-', $version);
$extra = !empty($split[1]) ? $split[1] : '';
$parts = explode('.', $split[0]);

foreach ($parts as $key => $value) {
$newVersion[$key] = (int)$value;
}

$extraModes = [self::RELEASE_RC, self::RELEASE_BETA, self::RELEASE_ALPHA];

if (in_array($mode, $extraModes)) {
$partsExtra = explode('.', $extra);
$extraName = $partsExtra[0];
$extraVersion = !empty($partsExtra[1]) ? $partsExtra[1] : 0;
if (is_numeric($extraName) && (empty($partsExtra[1]) || !is_numeric($partsExtra[1]))) {
$extraVersion = $partsExtra[0];
} elseif ($extraName !== $mode) {
$extraVersion = 0;
}
$extraVersion++;
$extra = "{$mode}.{$extraVersion}";
}

if ($mode === self::RELEASE_MAJOR) {
$newVersion[0]++;
} elseif ($mode === self::RELEASE_MINOR) {
$newVersion[1]++;
} elseif ($mode === self::RELEASE_PATCH) {
$newVersion[2]++;
}

// Recompose semver
$version = implode('.', $newVersion) . (!empty($extra) ? '-' . $extra : '');

$this->setVersion($version);

return $version;
}

public function getVersion(): string
{
return $this->version;
}

public function setVersion(string $version): SemanticVersion
{
$this->version = $version;

return $this;
}
}

0 comments on commit 7b5a9fd

Please sign in to comment.