Skip to content

Commit

Permalink
Merge pull request #1 from qlimix/v2
Browse files Browse the repository at this point in the history
v2
  • Loading branch information
frank-q committed Apr 12, 2020
2 parents 93cb2db + 9631ec7 commit dde231c
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 62 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: php

php:
- 7.2
- 7.3
- 7.4
- nightly

matrix:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 2.0.0 - 12-03-2020

New:
- Added JsonEncoding

Changed:
- updated encoding dependency to version ^2.0

Backward compatibility:
- required PHP >= 7.4
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Encoding-json

[![Travis CI](https://api.travis-ci.org/qlimix/encoding-json.svg?branch=master)](https://travis-ci.org/qlimix/encoding-json)
[![Coveralls](https://img.shields.io/coveralls/github/qlimix/encoding-json.svg)](https://coveralls.io/qlimix/encoding-json)
[![Coveralls](https://img.shields.io/coveralls/github/qlimix/encoding-json.svg)](https://coveralls.io/github/qlimix/encoding-json)
[![Packagist](https://img.shields.io/packagist/v/qlimix/encoding-json.svg)](https://packagist.org/packages/qlimix/encoding-json)
[![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/qlimix/encoding-json/blob/master/LICENSE)

Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "Encoding & decoding array's to string and vice versa",
"license": "MIT",
"keywords": [
"qlimix",
"encoding",
"decoding",
"decode",
Expand All @@ -19,8 +18,8 @@
],
"require": {
"ext-json": "*",
"php": ">=7.2",
"qlimix/encoding": "^1.0"
"php": ">=7.4",
"qlimix/encoding": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -36,6 +35,6 @@
"sort-packages": true
},
"require-dev": {
"qlimix/code-standard": "^1.0"
"qlimix/code-standard": "^2.0"
}
}
27 changes: 9 additions & 18 deletions src/Decode/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@
namespace Qlimix\Encoding\Decode;

use Qlimix\Encoding\Decode\Exception\DecodeException;
use Throwable;
use function json_decode;
use function json_last_error;
use const JSON_ERROR_NONE;
use const JSON_THROW_ON_ERROR;

final class JsonDecode implements DecodeInterface
{
/** @var bool */
private $assoc;
private int $depth;

/** @var int */
private $depth;
private int $options;

/** @var int */
private $options;

public function __construct(bool $assoc = true, int $depth = 512, int $options = 0)
public function __construct(int $depth = 512, int $options = 0)
{
$this->assoc = $assoc;
$this->depth = $depth;
$this->options = $options;
}
Expand All @@ -30,13 +24,10 @@ public function __construct(bool $assoc = true, int $depth = 512, int $options =
*/
public function decode(string $message): array
{
$json = json_decode($message, $this->assoc, $this->depth, $this->options);

$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new DecodeException('Failed to decode json (error number '.$error.')');
try {
return json_decode($message, true, $this->depth, JSON_THROW_ON_ERROR | $this->options);
} catch (Throwable $exception) {
throw new DecodeException('Failed to decode json', 0, $exception);
}

return $json;
}
}
25 changes: 8 additions & 17 deletions src/Encode/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace Qlimix\Encoding\Encode;

use Qlimix\Encoding\Encode\Exception\EncodeException;
use Throwable;
use function json_encode;
use function json_last_error;
use const JSON_ERROR_NONE;
use const JSON_THROW_ON_ERROR;

final class JsonEncode implements EncodeInterface
{
/** @var int */
private $options;
private int $options;

/** @var int */
private $depth;
private int $depth;

public function __construct(int $options = 0, int $depth = 512)
{
Expand All @@ -26,17 +24,10 @@ public function __construct(int $options = 0, int $depth = 512)
*/
public function encode(array $message): string
{
$json = json_encode($message, $this->options, $this->depth);

if ($json === false) {
throw new EncodeException('Failed to decode json');
}

$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new EncodeException('Failed to decode json (error number '.$error.')');
try {
return json_encode($message, JSON_THROW_ON_ERROR | $this->options, $this->depth);
} catch (Throwable $exception) {
throw new EncodeException('Failed to decode json', 0, $exception);
}

return $json;
}
}
34 changes: 34 additions & 0 deletions src/JsonEncoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Qlimix\Encoding;

use Qlimix\Encoding\Decode\JsonDecode;
use Qlimix\Encoding\Encode\JsonEncode;

final class JsonEncoding implements EncodingInterface
{
private JsonDecode $jsonDecode;
private JsonEncode $jsonEncode;

public function __construct(JsonDecode $jsonDecode, JsonEncode $jsonEncode)
{
$this->jsonDecode = $jsonDecode;
$this->jsonEncode = $jsonEncode;
}

/**
* @inheritDoc
*/
public function decode(string $value): array
{
return $this->jsonDecode->decode($value);
}

/**
* @inheritDoc
*/
public function encode(array $value): string
{
return $this->jsonEncode->encode($value);
}
}
13 changes: 3 additions & 10 deletions tests/Decode/JsonDecodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,20 @@

final class JsonDecodeTest extends TestCase
{
/** @var JsonDecode */
private $jsonDecode;
private JsonDecode $jsonDecode;

public function setUp(): void
{
$this->jsonDecode = new JsonDecode();
}

/**
* @test
*/
public function shouldDecode(): void
public function testShouldDecode(): void
{
$this->jsonDecode->decode('{"foo":"bar"}');
$this->addToAssertionCount(1);
}

/**
* @test
*/
public function shouldThrowOnJsonDecodeError(): void
public function testShouldThrowOnJsonDecodeError(): void
{
$this->expectException(DecodeException::class);

Expand Down
13 changes: 3 additions & 10 deletions tests/Encode/JsonEncodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,20 @@

final class JsonEncodeTest extends TestCase
{
/** @var JsonEncode */
private $jsonEncode;
private JsonEncode $jsonEncode;

public function setUp(): void
{
$this->jsonEncode = new JsonEncode();
}

/**
* @test
*/
public function shouldEncode(): void
public function testShouldEncode(): void
{
$this->jsonEncode->encode(['foo'=>'bar']);
$this->addToAssertionCount(1);
}

/**
* @test
*/
public function shouldThrowOnJsonEncodeError(): void
public function testShouldThrowOnJsonEncodeError(): void
{
$this->expectException(EncodeException::class);

Expand Down
38 changes: 38 additions & 0 deletions tests/JsonEncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Qlimix\Tests\Encoding;

use PHPUnit\Framework\TestCase;
use Qlimix\Encoding\Decode\Exception\DecodeException;
use Qlimix\Encoding\Decode\JsonDecode;
use Qlimix\Encoding\Encode\JsonEncode;
use Qlimix\Encoding\JsonEncoding;

final class JsonEncodingTest extends TestCase
{
private JsonEncoding $jsonEncoding;

public function setUp(): void
{
$this->jsonEncoding = new JsonEncoding(new JsonDecode(), new JsonEncode());
}

public function testShouldDecode(): void
{
$this->jsonEncoding->decode('{"foo":"bar"}');
$this->addToAssertionCount(1);
}

public function testShouldEncode(): void
{
$this->jsonEncoding->encode(['foo'=>'bar']);
$this->addToAssertionCount(1);
}

public function testShouldThrowOnJsonDecodeError(): void
{
$this->expectException(DecodeException::class);

$this->jsonEncoding->decode('{foo:"\xB1\x31"}');
}
}

0 comments on commit dde231c

Please sign in to comment.