Skip to content

Commit

Permalink
improve controller name
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 8, 2025
1 parent ca9be1d commit 275a13c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/ControllerName.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@

use Chevere\Action\Interfaces\ControllerInterface;
use Chevere\Action\Interfaces\ControllerNameInterface;
use InvalidArgumentException;
use Chevere\Action\Traits\ControllerNameTrait;

final class ControllerName implements ControllerNameInterface
{
public function __construct(
private string $name
) {
if (is_subclass_of($this->name, ControllerInterface::class)) {
return;
}
use ControllerNameTrait;

throw new InvalidArgumentException();
}

public function __toString(): string
private function interface(): string
{
return $this->name;
return ControllerInterface::class;
}
}
9 changes: 9 additions & 0 deletions src/Interfaces/ControllerNameInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@
*/
interface ControllerNameInterface extends Stringable
{
/**
* @return class-string ControllerInterface
*/
public function __toString(): string;

/**
* Returns a boolean if the object has this class as one of its parents or implements it.
*/
public function isSubclassOf(string $class): bool;
}
49 changes: 49 additions & 0 deletions src/Traits/ControllerNameTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Action\Traits;

use InvalidArgumentException;
use function Chevere\Message\message;

trait ControllerNameTrait
{
public function __construct(
/** @var class-string */
private string $name
) {
if ($this->isSubclassOf($this->interface())) {
return;
}

throw new InvalidArgumentException(
(string) message(
"Controller `{{ name }}` doesn't implement `{{ interface }}`",
name: $this->name,
interface: $this->interface()
)
);
}

public function __toString(): string
{
return $this->name;
}

public function isSubclassOf(string $class): bool
{
return is_subclass_of($this->name, $class, true);
}

abstract private function interface(): string;
}
5 changes: 5 additions & 0 deletions tests/ControllerNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ final class ControllerNameTest extends TestCase
public function testWrongInterface(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
<<<PLAIN
Controller `Chevere\Tests\ControllerNameTest` doesn't implement `Chevere\Action\Interfaces\ControllerInterface`
PLAIN
);
new ControllerName(self::class);
}

Expand Down

0 comments on commit 275a13c

Please sign in to comment.