Skip to content

Commit

Permalink
added Literal::new() [Closes #130]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 29, 2023
1 parent 163c5e9 commit 72cf9e2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
26 changes: 20 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,13 @@ new Literal('substr(?, ?)', [$a, $b]);
// generates, for example: substr('hello', 5);
```

The literal representing the creation of a new object is easily generated by the `new` method:

```php
Literal::new(Demo::class, [$a, 'foo' => $b]);
// generates, for example: new Demo(10, foo: 20)
```


Attributes
----------
Expand All @@ -590,10 +597,15 @@ You can add PHP 8 attributes to all classes, methods, properties, constants, enu

```php
$class = new Nette\PhpGenerator\ClassType('Demo');
$class->addAttribute('Deprecated');
$class->addAttribute('Table', [
'name' => 'user',
'constraints' => [
Literal::new('UniqueConstraint', ['name' => 'ean', 'columns' => ['ean']]),
],
]);

$class->addProperty('list')
->addAttribute('WithArguments', [1, 2]);
->addAttribute('Deprecated');

$method = $class->addMethod('count')
->addAttribute('Foo\Cached', ['mode' => true]);
Expand All @@ -607,16 +619,18 @@ echo $class;
Result:

```php
#[Deprecated]
#[Table(name: 'user', constraints: [new UniqueConstraint(name: 'ean', columns: ['ean'])])]
class Demo
{
#[WithArguments(1, 2)]
#[Deprecated]
public $list;


#[Foo\Cached(mode: true)]
public function count(#[Bar] $items)
{
public function count(
#[Bar]
$items,
) {
}
}
```
Expand Down
9 changes: 9 additions & 0 deletions src/PhpGenerator/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
*/
class Literal
{
/**
* Creates a literal representing the creation of an object using the new operator.
*/
public static function new(string $class, array $args = []): self
{
return new self('new ' . $class . '(...?:)', [$args]);
}


public function __construct(
private string $value,
/** @var ?mixed[] */
Expand Down
7 changes: 6 additions & 1 deletion tests/PhpGenerator/ClassType.attributes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ $class
->addComment('Description of class.')
->addAttribute('ExampleAttribute')
->addAttribute('WithArgument', [new Literal('Foo::BAR')])
->addAttribute('NamedArguments', ['foo' => 'bar', 'bar' => [1, 2, 3]]);
->addAttribute('Table', [
'name' => 'user',
'constraints' => [
Literal::new('UniqueConstraint', ['name' => 'ean', 'columns' => ['ean']]),
],
]);

$class->addConstant('FOO', 123)
->addComment('Commented')
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/ClassType.promotion.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $method->addPromotedParameter('c')
->addComment('promo')
->addAttribute('Example');

$method->addPromotedParameter('d', new Literal('new Draft(?)', [10]))
$method->addPromotedParameter('d', Literal::new('Draft', [10]))
->setType('Draft')
->setReadOnly();

Expand Down
6 changes: 6 additions & 0 deletions tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ Assert::same("[strlen('hello')]", $dumper->dump([new Literal('strlen(?)', ['hell
Assert::same("a\nb", $dumper->dump(new Literal("a\r\nb")));


// Literal::new
Assert::same('new stdClass()', $dumper->dump(Literal::new('stdClass')));
Assert::same('new stdClass(10, 20)', $dumper->dump(Literal::new('stdClass', [10, 20])));
Assert::same('new stdClass(10, c: 20)', $dumper->dump(Literal::new('stdClass', [10, 'c' => 20])));


// arrays
Assert::same('[]', $dumper->dump([]));
Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/expected/ClassType.attributes.expect
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
#[ExampleAttribute]
#[WithArgument(Foo::BAR)]
#[NamedArguments(foo: 'bar', bar: [1, 2, 3])]
#[Table(name: 'user', constraints: [new UniqueConstraint(name: 'ean', columns: ['ean'])])]
class Example
{
/** Commented */
Expand Down

0 comments on commit 72cf9e2

Please sign in to comment.