Skip to content

Commit

Permalink
Merge pull request #19 from dlundgren/14-change-version-processing
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
dlundgren authored Aug 12, 2024
2 parents 5846489 + ac9940f commit 260fe6a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 53 deletions.
51 changes: 51 additions & 0 deletions src/Concern/GeneratesDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @file
* Contains Phagrancy\Concern\GeneratesDefinition
*/

namespace Phagrancy\Concern;

use Phagrancy\Model\Entity;
use Psr\Http\Message\UriInterface;

/**
* Box JSON definition generator
*
* @package Phagrancy\Concern
*/
trait GeneratesDefinition
{
public function __construct(Entity\Box $box, UriInterface $uri)
{
parent::__construct($this->generateDefinition($box, $uri));
}

public function generateDefinition(Entity\Box $box, UriInterface $uri)
{
$json = [
$this->nameKey => $box->path(),
'versions' => []
];

foreach ($box->versions() as $version => $providers) {
if (empty($providers)) {
continue;
}
$versionedProviders = [];
foreach ($providers as $provider) {
$versionedProviders[] = [
'name' => (string)$provider,
'url' => (string)$uri->withPath($this->resolveUriPath($box, $version, $provider))
];
}
$json['versions'][] = [
'version' => (string)$version,
'providers' => $versionedProviders
];
}

return $json;
}
}
33 changes: 10 additions & 23 deletions src/Http/Response/Api/BoxDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Phagrancy\Http\Response\Api;

use Phagrancy\Concern\GeneratesDefinition;
use Phagrancy\Http\Response\Json;
use Phagrancy\Model\Entity;
use Psr\Http\Message\UriInterface;

/**
* Box JSON definition response
Expand All @@ -19,28 +19,15 @@
class BoxDefinition
extends Json
{
public function __construct(Entity\Box $box, UriInterface $uri)
use GeneratesDefinition;

/**
* @var string The key name for the definition. API uses tag, non-API uses name
*/
protected $nameKey = 'tag';

protected function resolveUriPath(Entity\Box $box, $version, $provider)
{
$json = [
'tag' => $box->path(),
'versions' => []
];
foreach ($box->versions() as $version => $providers) {
if (empty($providers)) {
continue;
}
$vpbs = [];
foreach ($providers as $provider) {
$vpbs[] = [
'name' => $provider,
'url' => (string)$uri->withPath("/api/v1/box/{$box->path()}/version/{$version}/provider/{$provider}")
];
}
$json['versions'][] = [
'version' => $version,
'providers' => $vpbs
];
}
parent::__construct($json);
return "/api/v1/box/{$box->path()}/version/{$version}/provider/{$provider}";
}
}
34 changes: 10 additions & 24 deletions src/Http/Response/BoxDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace Phagrancy\Http\Response;

use Phagrancy\Concern\GeneratesDefinition;
use Phagrancy\Model\Entity;
use Psr\Http\Message\UriInterface;

/**
* Box JSON definition response
Expand All @@ -18,29 +18,15 @@
class BoxDefinition
extends Json
{
public function __construct(Entity\Box $box, UriInterface $uri)
{
$json = [
'name' => (string)$box->path(),
'versions' => []
];
foreach ($box->versions() as $version => $providers) {
if (empty($providers)) {
continue;
}
$vpbs = [];
foreach ($providers as $provider) {
$vpbs[] = [
'name' => (string)$provider,
'url' => (string)$uri->withPath("/{$box->path()}/{$version}/{$provider}")
];
}
$json['versions'][] = [
'version' => strval($version),
'providers' => $vpbs
];
}
use GeneratesDefinition;

/**
* @var string The key name for the definition. API uses tag, non-API uses name
*/
protected $nameKey = 'name';

parent::__construct($json);
protected function resolveUriPath(Entity\Box $box, $version, $provider)
{
return "/{$box->path()}/{$version}/{$provider}";
}
}
5 changes: 3 additions & 2 deletions src/Model/Input/ValidatesVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ trait ValidatesVersion
*/
public function validateVersion()
{
$error = "Version must be of the format x.y.z where x, y, and z are all integers, or follow SemVer 2.0.0";;
$error = "Version must be of the format x.y.z where x, y, and z are all integers";
return function ($value) use ($error) {
if (!preg_match('/^v?(0|[1-9]\d*)(?:\.(0|[1-9]\d*)){0,2}(?:(\.|-)[\da-z+_-]+)*$/i', $value)) {
// we support a subset of SemVer
if (!preg_match('/^(\d+)(?:\.(\d+)){0,2}(?:-[\da-z]+)*$/', $value)) {
return $error;
}

Expand Down
15 changes: 11 additions & 4 deletions tests/unit/Model/Input/ValidatesVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ public function provideGoodVersions()
['20180609'],
['20180609.13425'],
['1.2.3'],
['1.alpha_a'],
['1.2.3.alpha'],
['1.2.3.0+b'],
['1.2.3-alpha.10.beta.0+build.unicorn.rainbow'],
['1.2.3-alpha'],
['1.2.3-alpha-build'],
['1-beta'],
['1.2-charlie'],
['0.1']
];
}

public function provideBadVersions()
{
return [
['1.-1'],
['v5'],
['1.alpha_a'],
['1.2.3.alpha'],
['1.2.3.0+b'],
['1.2.3-alpha build'],
['1.2.3-alpha.10.beta.0+build.unicorn.rainbow'],
];
}

Expand Down

0 comments on commit 260fe6a

Please sign in to comment.