ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Version.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Data;
22
27{
28 private const REGEXP = '(?<major>\d+)([.](?<minor>\d+)([.](?<patch>\d+))?)?';
29
30 protected int $major;
31 protected int $minor;
32 protected int $patch;
33
34 public function __construct(string $version)
35 {
36 $match = [];
37 if (!preg_match("/" . self::REGEXP . "/", $version, $match)) {
38 throw new \InvalidArgumentException(
39 "Expected version string '$version' to match this regular expression: " . self::REGEXP
40 );
41 }
42 $this->major = (int) $match["major"];
43 $this->minor = (int) ($match["minor"] ?? 0);
44 $this->patch = (int) ($match["patch"] ?? 0);
45 }
46
47 public function getMajor(): int
48 {
49 return $this->major;
50 }
51
52 public function getMinor(): int
53 {
54 return $this->minor;
55 }
56
57 public function getPatch(): int
58 {
59 return $this->patch;
60 }
61
62 public function equals(Version $other): bool
63 {
64 return
65 $this->major === $other->major
66 && $this->minor === $other->minor
67 && $this->patch === $other->patch;
68 }
69
70 public function isGreaterThan(Version $other): bool
71 {
72 return version_compare((string) $this, (string) $other, '>');
73 }
74
75 public function isGreaterThanOrEquals(Version $other): bool
76 {
77 return $this->equals($other) || $this->isGreaterThan($other);
78 }
79
80 public function isSmallerThan(Version $other): bool
81 {
82 return $other->isGreaterThan($this);
83 }
84
85 public function isSmallerThanOrEquals(Version $other): bool
86 {
87 return $other->isGreaterThan($this) || $this->equals($other);
88 }
89
90 public function __toString()
91 {
92 return "$this->major.$this->minor.$this->patch";
93 }
94}
$version
Definition: plugin.php:24
A version number that consists of three numbers (major, minor, patch).
Definition: Version.php:27
equals(Version $other)
Definition: Version.php:62
isSmallerThanOrEquals(Version $other)
Definition: Version.php:85
isGreaterThanOrEquals(Version $other)
Definition: Version.php:75
isSmallerThan(Version $other)
Definition: Version.php:80
__construct(string $version)
Definition: Version.php:34
isGreaterThan(Version $other)
Definition: Version.php:70