Skip to content

Instantly share code, notes, and snippets.

@Pierozi
Created December 19, 2017 13:33
Show Gist options
  • Save Pierozi/13665f7671e384aeeeabdd001c5b06eb to your computer and use it in GitHub Desktop.
Save Pierozi/13665f7671e384aeeeabdd001c5b06eb to your computer and use it in GitHub Desktop.
ValueObject of Sementic Versioning
<?php
namespace My\Vendor;
/**
* Class SemverObject
*/
final class SemverObject
{
protected $major;
protected $minor;
protected $patch;
protected $label;
/**
* SemverObject constructor.
* @param int $major
* @param int $minor
* @param int $patch
* @param int $label
*/
private function __construct(int $major, int $minor, int $patch, string $label = '')
{
$this->major = $major;
$this->minor = $minor;
$this->patch = $patch;
$this->label = $label;
}
/**
* Create a new Sementic Versionning Object
* by specify exactly each version
*
* @param int $major
* @param int $minor
* @param int $patch
* @param int $label
* @return SemverObject
*/
public static function create(int $major, int $minor, int $patch, string $label = '') : self
{
return new self($major, $minor, $patch, $label);
}
/**
* Create a new Sementic Versionning Object
* by specify version in string representation
*
* @param string $version
* @return SemverObject
*/
public static function createFromString(string $version) : self
{
@list($major, $minor, $patch) = explode('.', $version);
$label = '';
if (1 === preg_match('/\d+-(\w+)/', $patch, $matches)) {
$label = $matches[1];
}
$major = (int)($major ?? 0);
$minor = (int)($minor ?? 0);
$patch = (int)($patch ?? 0);
return new self($major, $minor, $patch, $label);
}
/**
* @return string
*/
public function __toString() :string
{
$version = "{$this->major}.{$this->minor}.{$this->patch}";
if ($this->label) {
$version .= "-{$this->label}";
}
return $version;
}
/**
* @param $key
* @return mixed
* @throws \Exception
*/
public function __get($key)
{
if (!property_exists($this, $key)) {
throw new \Exception("Property $key not exist");
}
return $this->$key;
}
/**
* @param $key
* @param $value
* @throws \Exception
*/
public function __set($key, $value)
{
throw new \Exception('Assignation is not allowed in ValueObject');
}
public function greaterThan(SemverObject $left): bool
{
if ($this->major > $left->major) {
return true;
}
if ($this->major < $left->major) {
return false;
}
if ($this->minor > $left->minor) {
return true;
}
if ($this->minor < $left->minor) {
return false;
}
if ($this->patch > $left->patch) {
return true;
}
return false;
}
public function greaterThanOrEqualTo(SemverObject $left): bool
{
if (true === $this->equalTo($left)) {
return true;
}
return $this->greaterThan($left);
}
public function lowerThan(SemverObject $left): bool
{
if ($this->major < $left->major) {
return true;
}
if ($this->major > $left->major) {
return false;
}
if ($this->minor < $left->minor) {
return true;
}
if ($this->minor > $left->minor) {
return false;
}
if ($this->patch < $left->patch) {
return true;
}
return false;
}
public function equalTo(SemverObject $left): bool
{
return $this->__toString() === $left->__toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment