Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Enums/ToonDelimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
class ToonDelimiter
{
public const COMMA = ',';

public const TAB = "\t";

public const PIPE = '|';

/**
Expand Down
22 changes: 10 additions & 12 deletions src/Toon.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Toon
public static function encode($value, $options = null): string
{
if ($options === null) {
$options = new EncodeOptions();
$options = new EncodeOptions;
}

return ToonSerializer::serialize($value, $options);
Expand All @@ -45,7 +45,7 @@ public static function encode($value, $options = null): string
public static function decode(string $toon, $options = null)
{
if ($options === null) {
$options = new DecodeOptions();
$options = new DecodeOptions;
}

return ToonDeserializer::deserialize($toon, $options);
Expand All @@ -54,8 +54,7 @@ public static function decode(string $toon, $options = null)
/**
* Encode to compact TOON format (minimal whitespace).
*
* @param mixed $value
* @return string
* @param mixed $value
*/
public static function compact($value): string
{
Expand All @@ -65,8 +64,7 @@ public static function compact($value): string
/**
* Encode to readable TOON format (more whitespace for readability).
*
* @param mixed $value
* @return string
* @param mixed $value
*/
public static function readable($value): string
{
Expand All @@ -76,8 +74,7 @@ public static function readable($value): string
/**
* Encode to tabular TOON format (optimized for uniform arrays).
*
* @param mixed $value
* @return string
* @param mixed $value
*/
public static function tabular($value): string
{
Expand All @@ -98,13 +95,14 @@ public static function estimateTokens(string $toon): int
/**
* Compare TOON vs JSON token usage.
*
* @param mixed $value
* @param EncodeOptions|null $options
* Uses compact format to showcase TOON's token savings benefits.
*
* @param mixed $value
* @return array<string, mixed> Array with keys: toon, json, toon_tokens, json_tokens, savings_percent
*/
public static function compare($value, $options = null): array
public static function compare($value): array
{
$toon = self::encode($value, $options);
$toon = self::compact($value);
$json = json_encode($value, JSON_THROW_ON_ERROR);

$toonTokens = self::estimateTokens($toon);
Expand Down
12 changes: 2 additions & 10 deletions src/ToonDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public function __construct(DecodeOptions $options)
}

/**
* @param string $toon
* @param DecodeOptions $options
* @return mixed
*/
public static function deserialize(string $toon, DecodeOptions $options)
Expand All @@ -46,7 +44,6 @@ public static function deserialize(string $toon, DecodeOptions $options)
}

/**
* @param string $toon
* @return mixed
*/
private function parse(string $toon)
Expand Down Expand Up @@ -146,7 +143,7 @@ private function parseObject()
break;
}

list($key, $valueStr) = explode(':', $trimmed, 2);
[$key, $valueStr] = explode(':', $trimmed, 2);
$key = trim($key);
$valueStr = trim($valueStr);

Expand All @@ -167,7 +164,6 @@ private function parseObject()
}

/**
* @param int $parentIndent
* @return mixed
*/
private function parseNestedValue(int $parentIndent)
Expand Down Expand Up @@ -222,8 +218,7 @@ private function parseArray(): array
}

/**
* @param string $values
* @param int|null $expectedCount
* @param int|null $expectedCount
* @return array<int, mixed>
*/
private function parseSimpleArray(string $values, $expectedCount = null): array
Expand Down Expand Up @@ -253,8 +248,6 @@ private function parseSimpleArray(string $values, $expectedCount = null): array
}

/**
* @param string $count
* @param string $keysStr
* @return array<int, array<string, mixed>|object>
*/
private function parseTabularArray(string $count, string $keysStr): array
Expand Down Expand Up @@ -323,7 +316,6 @@ private function parseTabularArray(string $count, string $keysStr): array
}

/**
* @param string $value
* @return mixed
*/
private function parsePrimitive(string $value)
Expand Down
14 changes: 7 additions & 7 deletions src/ToonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(EncodeOptions $options)
}

/**
* @param mixed $value
* @param mixed $value
*/
public static function serialize($value, EncodeOptions $options): string
{
Expand All @@ -32,7 +32,7 @@ public static function serialize($value, EncodeOptions $options): string
}

/**
* @param mixed $value
* @param mixed $value
*/
private function serializeValue($value, int $depth = 0): string
{
Expand Down Expand Up @@ -68,7 +68,7 @@ private function serializeValue($value, int $depth = 0): string
}

/**
* @param int|float $value
* @param int|float $value
*/
private function serializeNumber($value): string
{
Expand Down Expand Up @@ -182,7 +182,7 @@ private function canUseTabularFormat(array $value): bool
* @param array<array-key, mixed> $value
*/
/**
* @param array<array-key, mixed> $value
* @param array<array-key, mixed> $value
*/
private function serializeSimpleArray(array $value, int $depth): string
{
Expand Down Expand Up @@ -233,7 +233,7 @@ private function serializeSimpleArray(array $value, int $depth): string
* @param array<array-key, mixed> $value
*/
/**
* @param array<array-key, mixed> $value
* @param array<array-key, mixed> $value
*/
private function serializeTabularArray(array $value, int $depth): string
{
Expand Down Expand Up @@ -272,7 +272,7 @@ private function serializeTabularArray(array $value, int $depth): string
}

/**
* @param object|array<array-key, mixed> $value
* @param object|array<array-key, mixed> $value
*/
private function serializeObject($value, int $depth): string
{
Expand Down Expand Up @@ -314,7 +314,7 @@ private function serializeObject($value, int $depth): string
}

/**
* @param string|int $key
* @param string|int $key
*/
private function cleanObjectKey($key): string
{
Expand Down
21 changes: 8 additions & 13 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
/**
* Encode a value to TOON format.
*
* @param mixed $value
* @param EncodeOptions|null $options
* @return string
* @param mixed $value
* @param EncodeOptions|null $options
*/
function toon($value, $options = null): string
{
Expand All @@ -24,8 +23,7 @@ function toon($value, $options = null): string
/**
* Decode a TOON string to a PHP value.
*
* @param string $toon
* @param DecodeOptions|null $options
* @param DecodeOptions|null $options
* @return mixed
*/
function toon_decode(string $toon, $options = null)
Expand All @@ -38,8 +36,7 @@ function toon_decode(string $toon, $options = null)
/**
* Encode to compact TOON format.
*
* @param mixed $value
* @return string
* @param mixed $value
*/
function toon_compact($value): string
{
Expand All @@ -51,8 +48,7 @@ function toon_compact($value): string
/**
* Encode to readable TOON format.
*
* @param mixed $value
* @return string
* @param mixed $value
*/
function toon_readable($value): string
{
Expand All @@ -64,8 +60,7 @@ function toon_readable($value): string
/**
* Encode to tabular TOON format.
*
* @param mixed $value
* @return string
* @param mixed $value
*/
function toon_tabular($value): string
{
Expand All @@ -77,8 +72,8 @@ function toon_tabular($value): string
/**
* Compare TOON vs JSON token usage.
*
* @param mixed $value
* @param EncodeOptions|null $options
* @param mixed $value
* @param EncodeOptions|null $options
* @return array<string, mixed> Array with keys: toon, json, toon_tokens, json_tokens, savings_percent
*/
function toon_compare($value, $options = null): array
Expand Down