Skip to content
Merged
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ echo $generator->dumpFile([
'',

$generator->dumpAttribute('Example\Attributes\Something'),
$generator->dumpAttribute('Example\Attributes\Single', ['value: "Hello, World!"']),
$generator->dumpAttribute('Example\Attributes\Multiple', ['value: "Hello, World!"', 'other: "Other value"']),
sprintf(
'final readonly class %s extends %s',
$generator->import('Example\Demo'),
Expand Down Expand Up @@ -140,13 +142,20 @@ declare(strict_types=1);
namespace Example\Demo;

use DateTimeImmutable;
use Example\Attributes\Multiple;
use Example\Attributes\Single;
use Example\Attributes\Something;
use Example\Demo;
use Example\Parent;

// Auto-generated example file

#[Something]
#[Single(value: "Hello, World!")]
#[Multiple(
value: "Hello, World!",
other: "Other value",
)]
final readonly class Demo extends Parent
{
public function __construct(
Expand Down Expand Up @@ -183,8 +192,8 @@ echo $generator->dumpFile(function () use ($generator) {
yield '';

// Class with attributes
yield $generator->dumpAttribute('Example\Attributes\Entity');
yield $generator->dumpAttribute('Example\Attributes\Table');
yield from $generator->dumpAttribute('Example\Attributes\Entity');
yield from $generator->dumpAttribute('Example\Attributes\Table');
yield sprintf(
'final class DemoClass extends %s implements %s, %s',
$generator->import('Example\BaseClass'),
Expand Down
2 changes: 2 additions & 0 deletions examples/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'',

$generator->dumpAttribute('Example\Attributes\Something'),
$generator->dumpAttribute('Example\Attributes\Single', ['value: "Hello, World!"']),
$generator->dumpAttribute('Example\Attributes\Multiple', ['value: "Hello, World!"', 'other: "Other value"']),
sprintf(
'final readonly class %s extends %s',
$generator->import('Example\Demo'),
Expand Down
4 changes: 2 additions & 2 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
yield '';

// Class with attributes
yield $generator->dumpAttribute('Example\Attributes\Entity');
yield $generator->dumpAttribute('Example\Attributes\Table');
yield from $generator->dumpAttribute('Example\Attributes\Entity');
yield from $generator->dumpAttribute('Example\Attributes\Table');
yield sprintf(
'final class DemoClass extends %s implements %s, %s',
$generator->import('Example\BaseClass'),
Expand Down
22 changes: 19 additions & 3 deletions src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,26 @@ public function importByParent(Importable | string $name) : string

/**
* Generates a PHP attribute string for the given fully qualified class name
*
* @param CodeLines $args
* @return Generator<CodeLine>
*/
public function dumpAttribute(FullyQualified | string $fqcn) : string
{
return sprintf('#[%s]', $this->import($fqcn));
public function dumpAttribute(
FullyQualified | string $fqcn,
array | Closure | Generator | string $args = [],
bool $addCommaAfterEachArgument = true,
) : Generator {
$args = self::resolveIterable($args);

if ($args === []) {
yield sprintf('#[%s]', $this->import($fqcn));
} elseif (count($args) === 1) {
yield from $this->wrap(sprintf('#[%s(', $this->import($fqcn)), $args, ')]');
} else {
yield sprintf('#[%s(', $this->import($fqcn));
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args);
yield ')]';
}
}

/**
Expand Down
15 changes: 14 additions & 1 deletion tests/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,23 @@ public function testImportByParentWithDeepSubNamespace() : void

public function testDumpAttribute() : void
{
self::assertSame(
$this->assertDump(
'#[Required]',
$this->generator->dumpAttribute('App\\Attributes\\Required'),
);
$this->assertDump(
'#[Required(true)]',
$this->generator->dumpAttribute('App\\Attributes\\Required', ['true']),
);
$this->assertDump(
<<<'PHP'
#[Required(
true,
false,
)]
PHP,
$this->generator->dumpAttribute('App\\Attributes\\Required', ['true', 'false']),
);
}

public function testDumpClassReference() : void
Expand Down