-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Klein Florian <[email protected]>
- Loading branch information
1 parent
dd4b436
commit 19f46de
Showing
11 changed files
with
161 additions
and
118 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
// php -S 0:8080 -t example | ||
// xdg-open http://0:8080/whatever.php | ||
|
||
use Hippiemedia\Infra\Negotiate\WilldurandNegotiator; | ||
use Hippiemedia\Format; | ||
use Hippiemedia\Resource; | ||
|
||
require(__DIR__.'/../vendor/autoload.php'); | ||
|
||
$contentType = array_change_key_case(getallheaders())['content-type'] ?? 'text/html'; | ||
|
||
$negotiate = new WilldurandNegotiator(new \Negotiation\Negotiator, 'text/html', new Format\Html, new Format\Siren, new Format\Hal); | ||
$format = $negotiate($contentType); | ||
|
||
header('Content-Type: '.$format->accepts()); | ||
|
||
echo implode("\n", iterator_to_array($format(Resource::whatever()), false)); |
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,20 @@ | ||
<?php | ||
|
||
namespace spec\Hippiemedia\Format; | ||
|
||
use Hippiemedia\Format\Siren; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Hippiemedia\Resource; | ||
|
||
class HtmlSpec extends ObjectBehavior | ||
{ | ||
function it_formats_html() | ||
{ | ||
$iterable = $this(Resource::whatever()); | ||
|
||
$html = implode("\n", iterator_to_array($iterable->getWrappedObject(), false)); | ||
$dom = new \DOMDocument('1.0', 'UTF-8'); | ||
$dom->loadHTML($html); | ||
} | ||
} |
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,65 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hippiemedia\Format; | ||
|
||
use Functional as f; | ||
use Hippiemedia\Format; | ||
use Hippiemedia\Resource; | ||
use Hippiemedia\Operation; | ||
|
||
final class Html implements Format | ||
{ | ||
public function accepts(): string | ||
{ | ||
return 'text/html'; | ||
} | ||
|
||
public function __invoke(Resource $resource): iterable | ||
{ | ||
$state = print_r($resource->state, true); | ||
yield '<div>'; | ||
yield "<pre>{$state}</pre>"; | ||
yield from $this->links($resource); | ||
yield from $this->operations($resource); | ||
yield '</div>'; | ||
} | ||
|
||
private function links(Resource $resource): iterable | ||
{ | ||
yield '<ul>'; | ||
foreach ($resource->links as $link) { | ||
yield <<<HTML | ||
<li><a href="{$link->href}">{$link->title}</a></li> | ||
HTML; | ||
} | ||
yield '</ul>'; | ||
} | ||
|
||
private function operations(Resource $resource): iterable | ||
{ | ||
foreach ($resource->operations as $operation) { | ||
yield <<<HTML | ||
<form action="{$operation->url}" method="{$operation->method}"> | ||
<fieldset> | ||
<legend>{$operation->title}</legend> | ||
HTML | ||
; | ||
yield from $this->fields($operation); | ||
|
||
yield <<<HTML | ||
</fieldset> | ||
<input type="submit" /> | ||
</form> | ||
HTML; | ||
} | ||
} | ||
|
||
private function fields(Operation $operation): iterable | ||
{ | ||
foreach ($operation->fields as $field) { | ||
yield <<<HTML | ||
<input name="{$field->name}" value="{$field->value}" /> | ||
HTML; | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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