Skip to content
Open
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
35 changes: 35 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Fig\Attributes;

use Attribute;

/**
* Indicates the HTTP endpoint(s) associated with the class, method, or action.
*
* ## Target audience.
*
* Implementors: Static analysis tools, API documentation generators
* Users: Developers defining controller actions or classes handling HTTP requests
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class Route
{
/** @var string[] HTTP methods like GET, POST, PUT, DELETE */
public array $methods;

/** @var string Path relative to the application root */
public string $path;

/**
* @param string|string[] $methods HTTP method(s)
* @param string $path Endpoint path
*/
public function __construct(string|array $methods, string $path)
{
$this->methods = is_array($methods) ? $methods : [$methods];
$this->path = $path;
}
}
Empty file removed tests/.gitkeep
Empty file.
31 changes: 31 additions & 0 deletions tests/Fig/Attributes/Tests/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Fig\Attributes\Tests;

use Fig\Attributes\Route;
use PHPUnit\Framework\TestCase;

final class RouteTest extends TestCase
{
/**
* @covers \Fig\Attributes\Route
*/
public function testSingleMethod(): void
{
$attribute = new Route('GET', '/users');
$this->assertSame(['GET'], $attribute->methods);
$this->assertSame('/users', $attribute->path);
}

/**
* @covers \Fig\Attributes\Route
*/
public function testMultipleMethods(): void
{
$attribute = new Route(['GET', 'POST'], '/users');
$this->assertSame(['GET', 'POST'], $attribute->methods);
$this->assertSame('/users', $attribute->path);
}
}