Skip to content

Commit 8a3d6cb

Browse files
committed
Literal: added formatWith() for custom dumper
1 parent 541170f commit 8a3d6cb

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ private function dumpObject(&$var, array $parents, int $level): string
181181

182182
private function dumpLiteral(Literal $var, int $level): string
183183
{
184-
$s = Nette\Utils\Strings::indent(trim((string) $var), $level, $this->indentation);
184+
$s = $var->formatWith($this);
185+
$s = Nette\Utils\Strings::indent(trim($s), $level, $this->indentation);
185186
return ltrim($s, $this->indentation);
186187
}
187188

src/PhpGenerator/Literal.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,28 @@ class Literal
1818
/** @var string */
1919
private $value;
2020

21+
/** @var ?array */
22+
private $args;
23+
2124

2225
public function __construct(string $value, array $args = null)
2326
{
24-
$this->value = $args === null
25-
? $value
26-
: (new Dumper)->format($value, ...$args);
27+
$this->value = $value;
28+
$this->args = $args;
2729
}
2830

2931

3032
public function __toString(): string
3133
{
32-
return $this->value;
34+
return $this->formatWith(new Dumper);
35+
}
36+
37+
38+
/** @internal */
39+
public function formatWith(Dumper $dumper): string
40+
{
41+
return $this->args === null
42+
? $this->value
43+
: $dumper->format($this->value, ...$this->args);
3344
}
3445
}

0 commit comments

Comments
 (0)