Skip to content

Commit

Permalink
Doc and CBORDecoder class
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Nov 23, 2021
1 parent 71880a0 commit 512ac65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ If you want to load a CBOR encoded data, you just have to use de decoder availab
```php
<?php

use CBOR\Decoder;
use CBOR\StringStream;
use SpomkyLabs\CborBundle\CBORDecoder;

// CBOR object (in hex for the example)
$data = hex2bin('fb3fd5555555555555');

// String Stream
$stream = new StringStream($data);

// Load the data
$object = $container->get(Decoder::class)->decode($stream); // Return a CBOR\OtherObject\DoublePrecisionFloatObject class with normalized value ~0.3333 (=1/3)
$object = $container->get(CBORDecoder::class)->decode($data); // Return a CBOR\OtherObject\DoublePrecisionFloatObject class with normalized value ~0.3333 (=1/3)
```

## Custom Tags / Other Objects
Expand All @@ -58,6 +54,10 @@ I bring solutions to your problems and answer your questions.

If you really love that project and the work I have done or if you want I prioritize your issues, then you can help me out for a couple of :beers: or more!

[Become a sponsor](https://github.com/sponsors/Spomky)

Or

[![Become a Patreon](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/FlorentMorselli)

# Contributing
Expand Down
25 changes: 25 additions & 0 deletions src/CBORDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\CborBundle;

use CBOR\CBORObject;
use CBOR\Decoder;
use CBOR\StringStream;

class CBORDecoder
{
public function __construct(
private Decoder $decoder
)
{
}

public function decode(string $data): CBORObject
{
$stream = new StringStream($data);

return $this->decoder->decode($stream);
}
}
6 changes: 3 additions & 3 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use CBOR\Tag\TimestampTag;
use CBOR\Tag\UnsignedBigIntegerTag;
use CBOR\Tag\UriTag;
use SpomkyLabs\CborBundle\CBORDecoder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $container): void {
Expand All @@ -39,9 +40,8 @@
->autowire()
;

$container->set(Decoder::class)
->public()
;
$container->set(Decoder::class)->public();
$container->set(CBORDecoder::class)->public();

$container->set(OtherObjectManager::class)
->call('add', [BreakObject::class])
Expand Down
9 changes: 4 additions & 5 deletions tests/Functional/DecodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use CBOR\Decoder;
use CBOR\Normalizable;
use CBOR\StringStream;
use SpomkyLabs\CborBundle\CBORDecoder;
use function is_string;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand Down Expand Up @@ -37,15 +37,14 @@ public function theDecoderCanDecodeInputs(string $data, string $expectedNormaliz
static::bootKernel();
$container = static::$kernel->getContainer();

/** @var Decoder $decoder */
$decoder = $container->get(Decoder::class);
/** @var CBORDecoder $decoder */
$decoder = $container->get(CBORDecoder::class);
$binary = hex2bin($data);
if (! is_string($binary)) {
throw new RuntimeException('Invalid test case');
}
$stream = new StringStream($binary);

$result = $decoder->decode($stream);
$result = $decoder->decode($binary);
if ($result instanceof Normalizable) {
static::assertSame($expectedNormalizedValue, $result->normalize());
}
Expand Down

0 comments on commit 512ac65

Please sign in to comment.