-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #12 from lcobucci/add-template-engines
Add template engines
- Loading branch information
Showing
11 changed files
with
312 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/templates export-ignore | ||
/tests export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
|
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
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,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use League\Plates\Engine; | ||
use Throwable; | ||
|
||
final class Plates implements Formatter | ||
{ | ||
private const DEFAULT_ATTRIBUTE = 'template'; | ||
|
||
/** | ||
* @var Engine | ||
*/ | ||
private $engine; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $attributeName; | ||
|
||
public function __construct(Engine $engine, string $attributeName = self::DEFAULT_ATTRIBUTE) | ||
{ | ||
$this->engine = $engine; | ||
$this->attributeName = $attributeName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content, array $attributes = []): string | ||
{ | ||
try { | ||
return $this->render($content, $attributes); | ||
} catch (Throwable $exception) { | ||
throw new ContentCouldNotBeFormatted( | ||
'An error occurred while formatting using plates', | ||
$exception->getCode(), | ||
$exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $content | ||
* @param mixed[] $attributes | ||
* | ||
* @throws Throwable | ||
*/ | ||
private function render($content, array $attributes = []): string | ||
{ | ||
$template = $attributes[$this->attributeName] ?? ''; | ||
|
||
return $this->engine->render($template, ['content' => $content]); | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use Throwable; | ||
use Twig_Environment; | ||
|
||
final class Twig implements Formatter | ||
{ | ||
private const DEFAULT_ATTRIBUTE = 'template'; | ||
|
||
/** | ||
* @var Twig_Environment | ||
*/ | ||
private $environment; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $attributeName; | ||
|
||
public function __construct( | ||
Twig_Environment $environment, | ||
string $attributeName = self::DEFAULT_ATTRIBUTE | ||
) { | ||
$this->environment = $environment; | ||
$this->attributeName = $attributeName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content, array $attributes = []): string | ||
{ | ||
try { | ||
return $this->render($content, $attributes); | ||
} catch (Throwable $exception) { | ||
throw new ContentCouldNotBeFormatted( | ||
'An error occurred while formatting using twig', | ||
$exception->getCode(), | ||
$exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $content | ||
* @param mixed[] $attributes | ||
* | ||
* @throws Throwable | ||
*/ | ||
private function render($content, array $attributes = []): string | ||
{ | ||
$template = $attributes[$this->attributeName] ?? ''; | ||
|
||
return $this->environment->render($template, ['content' => $content]); | ||
} | ||
} |
File renamed without changes.
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,9 @@ | ||
<?php assert($content instanceof \Lcobucci\ContentNegotiation\Tests\PersonDto) ?> | ||
<section> | ||
<dl> | ||
<dt>Identifier</dt> | ||
<dd><?=$this->e($content->id)?></dd> | ||
<dt>Name</dt> | ||
<dd><?=$this->e($content->name)?></dd> | ||
</dl> | ||
</section> |
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,8 @@ | ||
<section> | ||
<dl> | ||
<dt>Identifier</dt> | ||
<dd>{{ content.id }}</dd> | ||
<dt>Name</dt> | ||
<dd>{{ content.name }}</dd> | ||
</dl> | ||
</section> |
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,79 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter\Plates; | ||
use Lcobucci\ContentNegotiation\Tests\PersonDto; | ||
use League\Plates\Engine; | ||
use PHPUnit\Framework\TestCase; | ||
use function dirname; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\Plates | ||
*/ | ||
final class PlatesTest extends TestCase | ||
{ | ||
/** | ||
* @var Engine | ||
*/ | ||
private $engine; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function configureEngine(): void | ||
{ | ||
$this->engine = new Engine(dirname(__DIR__, 2) . '/templates/plates'); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldReturnContentFormattedByPlates(): void | ||
{ | ||
$formatter = new Plates($this->engine); | ||
$content = $formatter->format(new PersonDto(1, 'Testing'), ['template' => 'person']); | ||
|
||
self::assertContains('<dd>1</dd>', $content); | ||
self::assertContains('<dd>Testing</dd>', $content); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldReadTemplateNameFromCustomAttribute(): void | ||
{ | ||
$formatter = new Plates($this->engine, 'fancy!'); | ||
$content = $formatter->format(new PersonDto(1, 'Testing'), ['fancy!' => 'person']); | ||
|
||
self::assertContains('<dd>1</dd>', $content); | ||
self::assertContains('<dd>Testing</dd>', $content); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldConvertAnyPlatesException(): void | ||
{ | ||
$formatter = new Plates($this->engine); | ||
|
||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
$this->expectExceptionMessage('An error occurred while formatting using plates'); | ||
|
||
$formatter->format(new PersonDto(1, 'Testing'), ['template' => 'no-template-at-all']); | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter\Twig; | ||
use Lcobucci\ContentNegotiation\Tests\PersonDto; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig_Environment; | ||
use Twig_Loader_Filesystem; | ||
use function dirname; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\Twig | ||
*/ | ||
final class TwigTest extends TestCase | ||
{ | ||
/** | ||
* @var Twig_Environment | ||
*/ | ||
private $environment; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function configureEngine(): void | ||
{ | ||
$this->environment = new Twig_Environment( | ||
new Twig_Loader_Filesystem('templates/twig', dirname(__DIR__, 2) . '/') | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldReturnContentFormattedByPlates(): void | ||
{ | ||
$formatter = new Twig($this->environment); | ||
$content = $formatter->format(new PersonDto(1, 'Testing'), ['template' => 'person.twig']); | ||
|
||
self::assertContains('<dd>1</dd>', $content); | ||
self::assertContains('<dd>Testing</dd>', $content); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldReadTemplateNameFromCustomAttribute(): void | ||
{ | ||
$formatter = new Twig($this->environment, 'fancy!'); | ||
$content = $formatter->format(new PersonDto(1, 'Testing'), ['fancy!' => 'person.twig']); | ||
|
||
self::assertContains('<dd>1</dd>', $content); | ||
self::assertContains('<dd>Testing</dd>', $content); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
* @covers ::render() | ||
*/ | ||
public function formatShouldConvertAnyTwigException(): void | ||
{ | ||
$formatter = new Twig($this->environment); | ||
|
||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
$this->expectExceptionMessage('An error occurred while formatting using twig'); | ||
|
||
$formatter->format(new PersonDto(1, 'Testing'), ['template' => 'no-template-at-all']); | ||
} | ||
} |