-
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.
- Loading branch information
Showing
3 changed files
with
629 additions
and
0 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,79 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\RestResource; | ||
|
||
use SimpleXMLElement; | ||
use Throwable; | ||
use function is_array; | ||
use function is_numeric; | ||
use function sprintf; | ||
use function str_replace; | ||
|
||
final class CondensedXmlFormatter implements ResourceFormatter | ||
{ | ||
use LinkRetrieval; | ||
|
||
/** @var string */ | ||
private $baseUri; | ||
/** @var Singularizer */ | ||
private $singularizer; | ||
|
||
public function __construct(string $baseUri, Singularizer $singularizer = null) | ||
{ | ||
$this->baseUri = $baseUri; | ||
$this->singularizer = $singularizer ?: BoogieSingularizer::default(); | ||
} | ||
|
||
public static function in( | ||
string $locale, | ||
string $baseUri | ||
): ResourceFormatter { | ||
return new self($baseUri, BoogieSingularizer::in($locale)); | ||
} | ||
|
||
public function from(RestResource $resource): string | ||
{ | ||
$xml = new SimpleXMLElement(sprintf( | ||
'<?xml version="1.0"?><%s />', | ||
$resource->name() | ||
)); | ||
try { | ||
$this->toSimpleXML( | ||
$resource->body() + $this->linksOf($resource, $this->baseUri), | ||
$xml | ||
); | ||
return (string) $xml->asXML(); | ||
} catch (Throwable $exception) { | ||
throw CannotFormatXml::because($resource, $exception); | ||
} | ||
} | ||
|
||
private function toSimpleXML( | ||
array $input, | ||
SimpleXMLElement $parent, | ||
bool $alreadySingularized = false | ||
): void { | ||
foreach ($input as $key => $value) { | ||
|
||
if (is_numeric($key)) { | ||
$name = $alreadySingularized ? | ||
'item' : $this->singularizer->convert($parent->getName()); | ||
$singularized = true; | ||
} else { | ||
$name = str_replace(['<', '>'], '', (string) $key); | ||
$singularized = false; | ||
} | ||
|
||
if (is_array($value)) { | ||
$node = $parent->addChild($name); | ||
$this->toSimpleXML($value, $node, $singularized); | ||
} elseif ($singularized) { | ||
$child = $parent->addChild($name); | ||
$child->addAttribute('value', (string) $value); | ||
} else { | ||
$parent->addAttribute($name, (string) $value); | ||
} | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.