-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1098 from spiral/hotfix/stempler-dircetive-zeros
Fixed bug in directive parser
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Directive; | ||
|
||
use Spiral\Tests\Stempler\fixtures\ImageDirective; | ||
use Spiral\Tests\Stempler\Directive\BaseTestCase; | ||
|
||
final class DirectiveTest extends BaseTestCase | ||
{ | ||
protected const DIRECTIVES = [ | ||
ImageDirective::class, | ||
]; | ||
|
||
public function testStringWithZeroChars(): void | ||
{ | ||
$doc = $this->parse('@image("blog", "test.png", "150|250", "webp")'); | ||
|
||
$this->assertSame( | ||
'<img title="blog" src="test.png" size="150|250" type="webp">', | ||
$this->compile($doc), | ||
); | ||
} | ||
|
||
public function testStringWithSingleQuotes(): void | ||
{ | ||
$doc = $this->parse("@image('blog', 'test.png', '150|250', 'webp')"); | ||
|
||
$this->assertSame( | ||
"<img title='blog' src='test.png' size='150|250' type='webp'>", | ||
$this->compile($doc), | ||
); | ||
} | ||
|
||
public function testVariableInjection(): void | ||
{ | ||
$doc = $this->parse('@image("blog", $src, "150|250", "webp")'); | ||
|
||
$this->assertSame( | ||
'<img title="blog" src="<?php echo $src; ?>" size="150|250" type="webp">', | ||
$this->compile($doc), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Stempler\fixtures; | ||
|
||
use Spiral\Stempler\Directive\AbstractDirective; | ||
use Spiral\Stempler\Node\Dynamic\Directive; | ||
|
||
final class ImageDirective extends AbstractDirective | ||
{ | ||
public function renderImage(Directive $directive): string | ||
{ | ||
return \sprintf( | ||
'<img title=%s src=%s size=%s type=%s>', | ||
$directive->values[0], | ||
\str_starts_with($directive->values[1], '$') ? \sprintf('"<?php echo %s; ?>"', $directive->values[1]) : $directive->values[1], | ||
$directive->values[2], | ||
$directive->values[3], | ||
); | ||
} | ||
} |