Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify modeling of the extension parts (pre-release and build) #14

Merged
merged 1 commit into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Exception/InvalidIdentifierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Version\Exception;

use DomainException;
use Version\Extension\BaseExtension;

/**
* @author Nikola Posa <posa.nikola@gmail.com>
*/
class InvalidIdentifierException extends DomainException implements ExceptionInterface
{
public static function forExtensionIdentifier(BaseExtension $extension, string $identifier) : self
{
return new self(sprintf(
'%s identifier: %s is not valid; it must comprise only ASCII alphanumerics and hyphen',
get_class($extension),
$identifier
));
}
}
12 changes: 0 additions & 12 deletions src/Exception/InvalidIdentifierValueException.php

This file was deleted.

58 changes: 58 additions & 0 deletions src/Extension/BaseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Version\Extension;

use Version\Exception\InvalidIdentifierException;

abstract class BaseExtension
{
protected const IDENTIFIERS_SEPARATOR = '.';

/**
* @var array
*/
private $identifiers;

protected function __construct(string ...$identifiers)
{
foreach ($identifiers as $identifier) {
$this->validate($identifier);
}

$this->identifiers = $identifiers;
}

/**
* @param string $identifier
* @throws InvalidIdentifierException
* @return void
*/
abstract protected function validate(string $identifier) : void;

public static function fromIdentifiers(string $firstIdentifier, string ...$identifiers)
{
return new static(...array_merge([$firstIdentifier], $identifiers));
}

public static function fromIdentifiersString(string $identifiers)
{
return new static(...explode(self::IDENTIFIERS_SEPARATOR, $identifiers));
}

public function getIdentifiers() : array
{
return $this->identifiers;
}

public function isEmpty() : bool
{
return empty($this->identifiers);
}

public function __toString() : string
{
return implode(self::IDENTIFIERS_SEPARATOR, $this->identifiers);
}
}
17 changes: 17 additions & 0 deletions src/Extension/Build.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Version\Extension;

use Version\Exception\InvalidIdentifierException;

class Build extends BaseExtension
{
protected function validate(string $identifier) : void
{
if (! preg_match('/^[0-9A-Za-z\-]+$/', $identifier)) {
throw InvalidIdentifierException::forExtensionIdentifier($this, $identifier);
}
}
}
13 changes: 13 additions & 0 deletions src/Extension/NoBuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Version\Extension;

class NoBuild extends Build
{
public function __construct()
{
parent::__construct(...[]);
}
}
13 changes: 13 additions & 0 deletions src/Extension/NoPreRelease.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Version\Extension;

class NoPreRelease extends PreRelease
{
public function __construct()
{
parent::__construct(...[]);
}
}
54 changes: 54 additions & 0 deletions src/Extension/PreRelease.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Version\Extension;

use Version\Exception\InvalidIdentifierException;

class PreRelease extends BaseExtension
{
protected function validate(string $identifier) : void
{
if (! preg_match('/^[0-9A-Za-z\-]+$/', $identifier)) {
throw InvalidIdentifierException::forExtensionIdentifier($this, $identifier);
}
}

public function compareTo(PreRelease $preRelease) : int
{
$firstPreReleaseIdentifiers = array_values($this->getIdentifiers());
$secondPreReleaseIdentifiers = array_values($preRelease->getIdentifiers());

$pr1Count = count($firstPreReleaseIdentifiers);
$pr2Count = count($secondPreReleaseIdentifiers);

$limit = min($pr1Count, $pr2Count);

for ($i = 0; $i < $limit; $i++) {
if ($firstPreReleaseIdentifiers[$i] === $secondPreReleaseIdentifiers[$i]) {
continue;
}

return $this->compareIdentifiers($firstPreReleaseIdentifiers[$i], $secondPreReleaseIdentifiers[$i]);
}

return $pr1Count - $pr2Count;
}

private function compareIdentifiers($identifier1, $identifier2) : int
{
$pr1IsAlpha = ctype_alpha($identifier1);
$pr2IsAlpha = ctype_alpha($identifier2);

if ($pr1IsAlpha xor $pr2IsAlpha) {
return $pr1IsAlpha ? 1 : -1;
}

if (ctype_digit($identifier1) && ctype_digit($identifier2)) {
return (int) $identifier1 - (int) $identifier2;
}

return strcmp($identifier1, $identifier2);
}
}
57 changes: 0 additions & 57 deletions src/Identifier/BaseIdentifier.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Identifier/BuildIdentifier.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Identifier/Identifier.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Identifier/PreReleaseIdentifier.php

This file was deleted.

Loading