Page MenuHomeCode

StringView.php
No OneTemporary

StringView.php

<?php
declare(strict_types=1);
namespace Zed\Engines\API\Format;
use Keruald\OmniTools\Collections\HashMap;
use Keruald\OmniTools\Collections\Vector;
use LogicException;
class StringView {
public static function toString (mixed $data) : string {
switch (ValueRepresentation::from($data)) {
case ValueRepresentation::List:
$response = Vector::from($data)
->map(fn($item) => self::toString($item))
->implode(", ");
return "[$response]";
case ValueRepresentation::Object:
$response = HashMap::from($data)
->mapToVector(
fn($key, $value) => "$key: " . self::toString($value)
)
->implode(", ");
return '{' . $response . '}';
case ValueRepresentation::Scalar:
return self::getScalarElement($data);
}
throw new LogicException("Unreachable code");
}
public static function getScalarElement (mixed $data) : string {
if (is_bool($data)) {
return $data ? 'true' : 'false';
}
return (string)$data;
}
}

File Metadata

Mime Type
text/x-php
Expires
Fri, Jul 11, 10:37 (3 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21313
Default Alt Text
StringView.php (1 KB)

Event Timeline