diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:12:12 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:12:12 -0400 |
commit | c9aa36da061816dee256a979c2ff8d2ee41824d9 (patch) | |
tree | 29f7002b80ee984b488bd047dbbd80b36bf892e9 /includes/composer/ComposerVersionNormalizer.php | |
parent | b4274e0e33eafb5e9ead9d949ebf031a9fb8363b (diff) | |
parent | d1ba966140d7a60cd5ae4e8667ceb27c1a138592 (diff) |
Merge branch 'archwiki'
# Conflicts:
# skins/ArchLinux.php
# skins/ArchLinux/archlogo.gif
Diffstat (limited to 'includes/composer/ComposerVersionNormalizer.php')
-rw-r--r-- | includes/composer/ComposerVersionNormalizer.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/includes/composer/ComposerVersionNormalizer.php b/includes/composer/ComposerVersionNormalizer.php new file mode 100644 index 00000000..a0d31cf2 --- /dev/null +++ b/includes/composer/ComposerVersionNormalizer.php @@ -0,0 +1,66 @@ +<?php + +/** + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroendedauw@gmail.com > + */ +class ComposerVersionNormalizer { + + /** + * Ensures there is a dash in between the version and the stability suffix. + * + * Examples: + * - 1.23RC => 1.23-RC + * - 1.23alpha => 1.23-alpha + * - 1.23alpha3 => 1.23-alpha3 + * - 1.23-beta => 1.23-beta + * + * @param string $version + * + * @return string + * @throws InvalidArgumentException + */ + public function normalizeSuffix( $version ) { + if ( !is_string( $version ) ) { + throw new InvalidArgumentException( '$version must be a string' ); + } + + return preg_replace( '/^(\d[\d\.]*)([a-zA-Z]+)(\d*)$/', '$1-$2$3', $version, 1 ); + } + + /** + * Ensures the version has four levels. + * Version suffixes are supported, as long as they start with a dash. + * + * Examples: + * - 1.19 => 1.19.0.0 + * - 1.19.2.3 => 1.19.2.3 + * - 1.19-alpha => 1.19.0.0-alpha + * - 1337 => 1337.0.0.0 + * + * @param string $version + * + * @return string + * @throws InvalidArgumentException + */ + public function normalizeLevelCount( $version ) { + if ( !is_string( $version ) ) { + throw new InvalidArgumentException( '$version must be a string' ); + } + + $dashPosition = strpos( $version, '-' ); + + if ( $dashPosition !== false ) { + $suffix = substr( $version, $dashPosition ); + $version = substr( $version, 0, $dashPosition ); + } + + $version = implode( '.', array_pad( explode( '.', $version ), 4, '0' ) ); + + if ( $dashPosition !== false ) { + $version .= $suffix; + } + + return $version; + } +} |