You are here

public function VersionParser::normalize in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/composer/semver/src/VersionParser.php \Composer\Semver\VersionParser::normalize()

Normalizes a version string to be able to perform comparisons on it.

Parameters

string $version:

string $fullVersion optional complete version string to give more context:

Return value

string

Throws

\UnexpectedValueException

2 calls to VersionParser::normalize()
VersionParser::normalizeBranch in vendor/composer/semver/src/VersionParser.php
Normalizes a branch name to be able to perform comparisons on it.
VersionParser::parseConstraint in vendor/composer/semver/src/VersionParser.php

File

vendor/composer/semver/src/VersionParser.php, line 91

Class

VersionParser
Version parser.

Namespace

Composer\Semver

Code

public function normalize($version, $fullVersion = null) {
  $version = trim($version);
  if (null === $fullVersion) {
    $fullVersion = $version;
  }

  // strip off aliasing
  if (preg_match('{^([^,\\s]+) +as +([^,\\s]+)$}', $version, $match)) {
    $version = $match[1];
  }

  // strip off build metadata
  if (preg_match('{^([^,\\s+]+)\\+[^\\s]+$}', $version, $match)) {
    $version = $match[1];
  }

  // match master-like branches
  if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
    return '9999999-dev';
  }
  if ('dev-' === strtolower(substr($version, 0, 4))) {
    return 'dev-' . substr($version, 4);
  }

  // match classical versioning
  if (preg_match('{^v?(\\d{1,5})(\\.\\d+)?(\\.\\d+)?(\\.\\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) {
    $version = $matches[1] . (!empty($matches[2]) ? $matches[2] : '.0') . (!empty($matches[3]) ? $matches[3] : '.0') . (!empty($matches[4]) ? $matches[4] : '.0');
    $index = 5;

    // match date(time) based versioning
  }
  elseif (preg_match('{^v?(\\d{4}(?:[.:-]?\\d{2}){1,6}(?:[.:-]?\\d{1,3})?)' . self::$modifierRegex . '$}i', $version, $matches)) {
    $version = preg_replace('{\\D}', '-', $matches[1]);
    $index = 2;
  }

  // add version modifiers if a version was matched
  if (isset($index)) {
    if (!empty($matches[$index])) {
      if ('stable' === $matches[$index]) {
        return $version;
      }
      $version .= '-' . $this
        ->expandStability($matches[$index]) . (!empty($matches[$index + 1]) ? $matches[$index + 1] : '');
    }
    if (!empty($matches[$index + 2])) {
      $version .= '-dev';
    }
    return $version;
  }

  // match dev branches
  if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) {
    try {
      return $this
        ->normalizeBranch($match[1]);
    } catch (\Exception $e) {
    }
  }
  $extraMessage = '';
  if (preg_match('{ +as +' . preg_quote($version) . '$}', $fullVersion)) {
    $extraMessage = ' in "' . $fullVersion . '", the alias must be an exact version';
  }
  elseif (preg_match('{^' . preg_quote($version) . ' +as +}', $fullVersion)) {
    $extraMessage = ' in "' . $fullVersion . '", the alias source must be an exact version, if it is a branch name you should prefix it with dev-';
  }
  throw new \UnexpectedValueException('Invalid version string "' . $version . '"' . $extraMessage);
}