5 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
7 class ComposerVersionNormalizer
{
10 * Ensures there is a dash in between the version and the stability suffix.
14 * - 1.23alpha => 1.23-alpha
15 * - 1.23alpha3 => 1.23-alpha3
16 * - 1.23-beta => 1.23-beta
18 * @param string $version
21 * @throws InvalidArgumentException
23 public function normalizeSuffix( $version ) {
24 if ( !is_string( $version ) ) {
25 throw new InvalidArgumentException( '$version must be a string' );
28 return preg_replace( '/^(\d[\d\.]*)([a-zA-Z]+)(\d*)$/', '$1-$2$3', $version, 1 );
32 * Ensures the version has four levels.
33 * Version suffixes are supported, as long as they start with a dash.
37 * - 1.19.2.3 => 1.19.2.3
38 * - 1.19-alpha => 1.19.0.0-alpha
39 * - 1337 => 1337.0.0.0
41 * @param string $version
44 * @throws InvalidArgumentException
46 public function normalizeLevelCount( $version ) {
47 if ( !is_string( $version ) ) {
48 throw new InvalidArgumentException( '$version must be a string' );
51 $dashPosition = strpos( $version, '-' );
53 if ( $dashPosition !== false ) {
54 $suffix = substr( $version, $dashPosition );
55 $version = substr( $version, 0, $dashPosition );
58 $version = implode( '.', array_pad( explode( '.', $version ), 4, '0' ) );
60 if ( $dashPosition !== false ) {