Skip to content

Commit

Permalink
Made fixes from PR !116.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulin-evgeny committed May 6, 2024
1 parent 4dad7b0 commit 572ea95
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Contracts/VersionEnumContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace RonasIT\Support\Contracts;

interface VersionEnumContract {
interface VersionEnumContract
{
public static function values(): array;

public static function toString(string $separator = ','): string;
Expand Down
25 changes: 15 additions & 10 deletions src/HelpersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@ class HelpersServiceProvider extends ServiceProvider
{
public function boot()
{
$router = $this->app['router'];

$router->prependMiddlewareToGroup('web', SecurityMiddleware::class);
$router->prependMiddlewareToGroup('api', SecurityMiddleware::class);

$this->extendValidator();

app(ExcelServiceProvider::class, ['app' => app()])->boot();

$this->loadViewsFrom(__DIR__ . '/Stubs', 'ronasit');
$this->extendRouter();

/**
* Specify that the route version must be in the range of given values inclusive.
Expand Down Expand Up @@ -101,4 +92,18 @@ protected function extendValidator()
->exists();
});
}

protected function extendRouter()
{
$router = $this->app['router'];

$router->prependMiddlewareToGroup('web', SecurityMiddleware::class);
$router->prependMiddlewareToGroup('api', SecurityMiddleware::class);

$this->extendValidator();

app(ExcelServiceProvider::class, ['app' => app()])->boot();

$this->loadViewsFrom(__DIR__ . '/Stubs', 'ronasit');
}
}
19 changes: 10 additions & 9 deletions src/Support/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@

namespace RonasIT\Support\Support;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use RonasIT\Support\Contracts\VersionEnumContract;

class Version
{
public static function current(): string
public static function current($pathParamName = 'version'): string
{
$route = Route::getRoutes()->match(request());

return $route->parameters()['version'] ?? str_replace('/v', '', $route->getPrefix());
return Arr::get($route->parameters(), $pathParamName, str_replace('/v', '', $route->getPrefix()));
}

public static function is(VersionEnumContract $checkedVersion): bool
public static function is(VersionEnumContract $expectedVersion): bool
{
return $checkedVersion->value === self::current();
return version_compare($expectedVersion->value, self::current(), '=');
}

public static function between(VersionEnumContract $from, VersionEnumContract $to): bool
{
$version = self::current();

return $version >= $from->value && $version <= $to->value;
return version_compare($version, $from->value, '>=') && version_compare($version, $to->value, '<=');
}

public static function from(VersionEnumContract $from): bool
public static function gte(VersionEnumContract $from): bool
{
return self::current() >= $from->value;
return version_compare(self::current(), $from->value, '>=');
}

public static function to(VersionEnumContract $to): bool
public static function lte(VersionEnumContract $to): bool
{
return self::current() <= $to->value;
return version_compare(self::current(), $to->value, '<=');
}
}
7 changes: 0 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ public function callRawRequest(string $method, string $uri, $content, array $hea
return $this->call($method, $uri, [], [], [], $server, $content);
}

public function json($method, $uri, array $data = [], array $headers = [], ?VersionEnumContract $apiVersion = null): TestResponse
{
$apiVersion = (empty($apiVersion)) ? last(VersionEnumContract::values()) : $apiVersion->value;

return parent::json($method, "/v{$apiVersion}{$uri}", $data, $headers);
}

protected function dontWrapIntoTransaction(): void
{
$this->rollbackTransaction();
Expand Down

0 comments on commit 572ea95

Please sign in to comment.