-
-
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 #10 from lcobucci/add-attributes
Add attributes to the unformatted response
- Loading branch information
Showing
10 changed files
with
231 additions
and
30 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
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
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,57 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\Formatter; | ||
use SplFileObject; | ||
use function array_keys; | ||
use function array_map; | ||
use function assert; | ||
use function is_string; | ||
use function str_replace; | ||
use function trim; | ||
|
||
final class NaiveTemplateEngine implements Formatter | ||
{ | ||
private const BASE_DIR = __DIR__ . '/templates/naive/'; | ||
private const EXTENSION = 'html'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content, array $attributes = []): string | ||
{ | ||
$template = $this->getTemplateContent($attributes); | ||
|
||
return $this->render($template, (array) $content); | ||
} | ||
|
||
/** | ||
* @param mixed[] $attributes | ||
*/ | ||
private function getTemplateContent(array $attributes): string | ||
{ | ||
$template = $attributes['template'] ?? ''; | ||
assert(is_string($template)); | ||
|
||
$file = new SplFileObject(self::BASE_DIR . $template . '.' . self::EXTENSION); | ||
|
||
return $file->fread($file->getSize()); | ||
} | ||
|
||
/** | ||
* @param mixed[] $data | ||
*/ | ||
private function render(string $template, array $data): string | ||
{ | ||
$variables = array_map( | ||
function (string $attribute): string { | ||
return '{' . $attribute . '}'; | ||
}, | ||
array_keys($data) | ||
); | ||
|
||
return trim(str_replace($variables, $data, $template)); | ||
} | ||
} |
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>{id}</dd> | ||
<dt>Name</dt> | ||
<dd>{name}</dd> | ||
</dl> | ||
</section> |
Oops, something went wrong.