Skip to content

Commit

Permalink
Used named capture groups in semver regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Feb 3, 2020
1 parent d67885a commit c458d45
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ public function __toString() : string
*/
public function setVersion(string $version) : self
{
$semverRegex = '/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-.]+))?(?:\+([0-9A-Za-z-.]+)?)?$/';
$semverRegex = '/^v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<pre_release>[0-9A-Za-z-.]+))?(?:\+(?<build>[0-9A-Za-z-.]+)?)?$/';

if (! preg_match($semverRegex, $version, $matches)) {
throw new InvalidVersionException('Invalid Semantic Version string provided');
}

$this->major = (int) $matches[1];
$this->minor = (int) $matches[2];
$this->patch = (int) $matches[3];
$this->preRelease = $matches[4] ?? null;
$this->build = $matches[5] ?? null;
$this->major = (int) $matches['major'];
$this->minor = (int) $matches['minor'];
$this->patch = (int) $matches['patch'];
$this->preRelease = $matches['pre_release'] ?? null;
$this->build = $matches['build'] ?? null;

return $this;
}
Expand Down

0 comments on commit c458d45

Please sign in to comment.