Skip to content

Commit

Permalink
update: alias names of format definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonej committed Nov 3, 2021
1 parent 2457fe8 commit eac1a13
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Formatting/Attributes/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class Format
{
public function __construct(
Expand Down
16 changes: 12 additions & 4 deletions src/Formatting/FormatDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@

namespace Sourcetoad\EnhancedResources\Formatting;

use Illuminate\Support\Collection;
use ReflectionAttribute;
use ReflectionMethod;
use Sourcetoad\EnhancedResources\Formatting\Attributes\Format;

class FormatDefinition
{
protected Format $format;
protected Collection $formats;
protected ReflectionMethod $reflection;

public function __construct(ReflectionMethod $reflection)
{
$this->reflection = $reflection;

/** @noinspection PhpFieldAssignmentTypeMismatchInspection */
$this->format = $this->reflection->getAttributes(Format::class)[0]->newInstance();
$this->formats = (new Collection($this->reflection->getAttributes(Format::class)))
->map(fn(ReflectionAttribute $attribute) => $attribute->newInstance());
}

public function name(): string
{
return $this->format->name() ?? $this->reflection->getName();
return $this->names()->first();
}

public function names(): Collection
{
return $this->formats->map(fn(Format $format) => $format->name() ?? $this->reflection->getName())
->unique();
}
}
8 changes: 7 additions & 1 deletion tests/Unit/Formatting/FormatDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function nameDetectionProvider(): array
#[Format('bar')]
public function barFormat() {}

#[Format]
#[Format, Format('fooAlias')]
public function foo() {}
};

Expand All @@ -47,6 +47,12 @@ public function foo() {}
$this->assertSame('bar', $definition->name());
}
],
'alias' => [
'method' => new ReflectionMethod($subject, 'foo'),
'assertions' => function (FormatDefinition $definition) {
$this->assertContains('fooAlias', $definition->names());
}
],
];
}

Expand Down

0 comments on commit eac1a13

Please sign in to comment.