generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ergebnis/feature/decoder
Enhancement: Implement Decoder
- Loading branch information
Showing
7 changed files
with
246 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-schema-validator | ||
*/ | ||
|
||
namespace Ergebnis\Json\SchemaValidator; | ||
|
||
final class Decoder | ||
{ | ||
/** | ||
* @throws Exception\InvalidJson | ||
* | ||
* @return null|array|bool|float|int|string | ||
*/ | ||
public function decodeAssociative(string $value) | ||
{ | ||
$decoded = \json_decode( | ||
$value, | ||
true | ||
); | ||
|
||
if (null === $decoded && \JSON_ERROR_NONE !== \json_last_error()) { | ||
throw Exception\InvalidJson::string(); | ||
} | ||
|
||
return $decoded; | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidJson | ||
* | ||
* @return null|array|bool|float|int|\stdClass|string | ||
*/ | ||
public function decodeNonAssociative(string $value) | ||
{ | ||
$decoded = \json_decode( | ||
$value, | ||
false | ||
); | ||
|
||
if (null === $decoded && \JSON_ERROR_NONE !== \json_last_error()) { | ||
throw Exception\InvalidJson::string(); | ||
} | ||
|
||
return $decoded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-schema-validator | ||
*/ | ||
|
||
namespace Ergebnis\Json\SchemaValidator\Test\DataProvider; | ||
|
||
use Ergebnis\Test\Util; | ||
|
||
final class JsonProvider | ||
{ | ||
use Util\Helper; | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function validString(): \Generator | ||
{ | ||
yield from self::provideDataForValues(\array_map(static function (string $file): string { | ||
$contents | ||
= \file_get_contents($file); | ||
|
||
if (!\is_string($contents)) { | ||
throw new \RuntimeException(\sprintf( | ||
'File "%s" does not exist or can not be read.', | ||
$file | ||
)); | ||
} | ||
|
||
return $contents; | ||
}, self::filesContainingValidJson())); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public function validFile(): \Generator | ||
{ | ||
yield from self::provideDataForValues(self::filesContainingValidJson()); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private static function filesContainingValidJson(): array | ||
{ | ||
return [ | ||
'array' => __DIR__ . '/../Fixture/Json/valid/array.json', | ||
'bool-false' => __DIR__ . '/../Fixture/Json/valid/bool-false.json', | ||
'bool-true' => __DIR__ . '/../Fixture/Json/valid/bool-true.json', | ||
'float' => __DIR__ . '/../Fixture/Json/valid/float.json', | ||
'int' => __DIR__ . '/../Fixture/Json/valid/int.json', | ||
'object' => __DIR__ . '/../Fixture/Json/valid/object.json', | ||
'string-arbitrary' => __DIR__ . '/../Fixture/Json/valid/string-arbitrary.json', | ||
'string-empty' => __DIR__ . '/../Fixture/Json/valid/string-empty.json', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-schema-validator | ||
*/ | ||
|
||
namespace Ergebnis\Json\SchemaValidator\Test\Unit; | ||
|
||
use Ergebnis\Json\SchemaValidator\Decoder; | ||
use Ergebnis\Json\SchemaValidator\Exception; | ||
use Ergebnis\Test\Util; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\Json\SchemaValidator\Decoder | ||
* | ||
* @uses \Ergebnis\Json\SchemaValidator\Exception\InvalidJson | ||
*/ | ||
final class DecoderTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
public function testDecodeAssociativeThrowsExceptionWhenValueCanNotBeDecoded(): void | ||
{ | ||
$value = <<<'TXT' | ||
{ | ||
"foo: | ||
TXT; | ||
|
||
$decoder = new Decoder(); | ||
|
||
$this->expectException(Exception\InvalidJson::class); | ||
|
||
$decoder->decodeAssociative($value); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Json\SchemaValidator\Test\DataProvider\JsonProvider::validString() | ||
*/ | ||
public function testDecodeAssociativeReturnsJsonDecodedValue(string $encoded): void | ||
{ | ||
$decoder = new Decoder(); | ||
|
||
$decoded = $decoder->decodeAssociative($encoded); | ||
|
||
$expected = \json_decode( | ||
$encoded, | ||
true | ||
); | ||
|
||
self::assertSame($expected, $decoded); | ||
} | ||
|
||
public function testDecodeNonAssociativeThrowsExceptionWhenValueCanNotBeDecoded(): void | ||
{ | ||
$value = <<<'TXT' | ||
{ | ||
"foo: | ||
TXT; | ||
|
||
$decoder = new Decoder(); | ||
|
||
$this->expectException(Exception\InvalidJson::class); | ||
|
||
$decoder->decodeAssociative($value); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Json\SchemaValidator\Test\DataProvider\JsonProvider::validString() | ||
*/ | ||
public function testDecodeNonAssociativeReturnsJsonDecodedValue(string $encoded): void | ||
{ | ||
$decoder = new Decoder(); | ||
|
||
$decoded = $decoder->decodeNonAssociative($encoded); | ||
|
||
$expected = \json_decode( | ||
$encoded, | ||
false | ||
); | ||
|
||
self::assertEquals($expected, $decoded); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters