Skip to content

Commit

Permalink
Update CBOR.php
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed Dec 15, 2024
1 parent 3ff45ae commit 73a4001
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/Support/CBOR.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
use CBOR\OtherObject\NullObject;
use CBOR\OtherObject\TrueObject;
use CBOR\StringStream;
use CBOR\Tag\GenericTag;
use CBOR\Tag\TagManager;
use CBOR\TextStringObject;
use CBOR\UnsignedIntegerObject;
use Illuminate\Support\Str;
use InvalidArgumentException;
use YOCLIB\Multiformats\Multibase\Multibase;
use Revolution\Bluesky\Support\CBOR\CidTag;

final class CBOR
{
Expand All @@ -44,7 +43,10 @@ public static function fromArray(array $data): string

public static function decode(string $data): CBORObject
{
$decoder = Decoder::create();
$tagManager = TagManager::create()
->add(CidTag::class);

$decoder = Decoder::create($tagManager);

return $decoder->decode(StringStream::create($data));
}
Expand All @@ -55,23 +57,20 @@ public static function decode(string $data): CBORObject
public static function normalize(mixed $data): mixed
{
if (is_array($data)) {
return collect($data)->map(function ($item) {
return collect($data)->map(function ($item, $key) {
if (is_numeric($item)) {
return (int) $item;
}
if ($key === 'ref' && $item instanceof CidTag) {
return $item->link();
}
if (in_array($key, ['v', 't', 'l', 'data'], true) && $item instanceof CidTag) {
return $item->mst();
}
return self::normalize($item);
})->toArray();
}

if ($data instanceof GenericTag) {
$cid = $data->getValue();
if ($cid instanceof ByteStringObject) {
$cid = $cid->normalize();
$cid = Str::ltrim($cid, "\x00");

return Multibase::encode(Multibase::BASE32, $cid);
}

dump($data);
}

return $data;
}

Expand All @@ -92,11 +91,11 @@ private function processData(mixed $data, int $option): CBORObject
$data instanceof CBORObject => $data,
is_string($data) => preg_match('//u', $data) === 1 ? $this->processTextString(
$data,
$option
$option,
) : $this->processByteString($data, $option),
is_array($data) => array_is_list($data) ? $this->processList($data, $option) : $this->processMap(
$data,
$option
$option,
),
is_int($data) => $data < 0 ? NegativeIntegerObject::create($data) : UnsignedIntegerObject::create($data),
//is_float($data) => $this->processFloat($data, $option),
Expand Down

0 comments on commit 73a4001

Please sign in to comment.