|
1 | | -# Data Coder |
| 1 | +## Data Coder |
2 | 2 |
|
3 | | -## A flexible data coder for encoding and decoding formats like JSON, YAML, and more. |
| 3 | +### A flexible data coder for encoding and decoding formats like JSON, YAML, and more. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +[](https://scrutinizer-ci.com/g/sunrise-php/coder/?branch=master) |
| 8 | +[](https://scrutinizer-ci.com/g/sunrise-php/coder/?branch=master) |
| 9 | +[](https://packagist.org/packages/sunrise/coder) |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +composer require sunrise/coder |
| 15 | +``` |
| 16 | + |
| 17 | +## How to use |
| 18 | + |
| 19 | +### Quick start |
| 20 | + |
| 21 | +```php |
| 22 | +use Sunrise\Coder\CodecManager; |
| 23 | +use Sunrise\Coder\Codec\JsonCodec; |
| 24 | +use Sunrise\Coder\Dictionary\MediaType; |
| 25 | + |
| 26 | +$codecManager = new CodecManager(codecs: [ |
| 27 | + new JsonCodec(), |
| 28 | +]); |
| 29 | + |
| 30 | +// Encoding result: {"foo": "bar"} |
| 31 | +$codecManager->encode(MediaType::JSON, ['foo' => 'bar']); |
| 32 | + |
| 33 | +// Decoding result: ['foo' => 'bar'] |
| 34 | +$codecManager->decode(MediaType::JSON, '{"foo": "bar"}'); |
| 35 | +``` |
| 36 | + |
| 37 | +### PHP-DI definitions |
| 38 | + |
| 39 | +```php |
| 40 | +use DI\ContainerBuilder; |
| 41 | +use Sunrise\Coder\CodecManagerInterface; |
| 42 | + |
| 43 | +$containerBuilder = new ContainerBuilder(); |
| 44 | +$containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/coder/resources/definitions/coder.php'); |
| 45 | +$containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/coder/resources/definitions/codecs/json_codec.php'); |
| 46 | + |
| 47 | +$container = $containerBuilder->build(); |
| 48 | + |
| 49 | +// See above for usage examples. |
| 50 | +$codecManager = $container->get(CodecManagerInterface::class); |
| 51 | +``` |
| 52 | + |
| 53 | +## Tests |
| 54 | + |
| 55 | +```bash |
| 56 | +composer test |
| 57 | +``` |
0 commit comments