Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Introduce named constructor for default json_encode() options #799

Merged
merged 1 commit into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Format/JsonEncodeOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private function __construct(private int $value)
{
}

public static function default(): self
{
return new self(0);
}

/**
* @throws Exception\InvalidJsonEncodeOptions
*/
Expand Down
13 changes: 8 additions & 5 deletions src/SchemaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ public function normalize(Json $json): Json
);
}

$normalized = Json::fromString(\json_encode($this->normalizeData(
$json->decoded(),
$schema,
Pointer\JsonPointer::document(),
)));
$normalized = Json::fromString(\json_encode(
$this->normalizeData(
$json->decoded(),
$schema,
Pointer\JsonPointer::document(),
),
Format\JsonEncodeOptions::default()->toInt(),
));

$resultAfterNormalization = $this->schemaValidator->validate(
$normalized,
Expand Down
6 changes: 5 additions & 1 deletion src/Vendor/Composer/BinNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\Json\Normalizer\Vendor\Composer;

use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Format;
use Ergebnis\Json\Normalizer\Normalizer;

final class BinNormalizer implements Normalizer
Expand All @@ -37,7 +38,10 @@ public function normalize(Json $json): Json
$decoded->bin = $bin;

/** @var string $encoded */
$encoded = \json_encode($decoded);
$encoded = \json_encode(
$decoded,
Format\JsonEncodeOptions::default()->toInt(),
);

return Json::fromString($encoded);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Vendor/Composer/PackageHashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\Json\Normalizer\Vendor\Composer;

use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Format;
use Ergebnis\Json\Normalizer\Normalizer;

final class PackageHashNormalizer implements Normalizer
Expand Down Expand Up @@ -60,7 +61,10 @@ public function normalize(Json $json): Json
}

/** @var string $encoded */
$encoded = \json_encode($decoded);
$encoded = \json_encode(
$decoded,
Format\JsonEncodeOptions::default()->toInt(),
);

return Json::fromString($encoded);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Vendor/Composer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\Json\Normalizer\Vendor\Composer;

use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Format;
use Ergebnis\Json\Normalizer\Normalizer;

final class VersionConstraintNormalizer implements Normalizer
Expand Down Expand Up @@ -70,7 +71,10 @@ public function normalize(Json $json): Json
}

/** @var string $encoded */
$encoded = \json_encode($decoded);
$encoded = \json_encode(
$decoded,
Format\JsonEncodeOptions::default()->toInt(),
);

return Json::fromString($encoded);
}
Expand Down
9 changes: 9 additions & 0 deletions test/Unit/Format/JsonEncodeOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ final class JsonEncodeOptionsTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testDefaultReturnsJsonEncodeOptions(): void
{
$jsonEncodeOptions = Format\JsonEncodeOptions::default();

$expected = 0;

self::assertSame($expected, $jsonEncodeOptions->toInt());
}

/**
* @dataProvider provideInvalidValue
*/
Expand Down
1 change: 1 addition & 0 deletions test/Unit/SchemaNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @uses \Ergebnis\Json\Normalizer\Exception\SchemaUriCouldNotBeResolved
* @uses \Ergebnis\Json\Normalizer\Exception\SchemaUriReferencesDocumentWithInvalidMediaType
* @uses \Ergebnis\Json\Normalizer\Exception\SchemaUriReferencesInvalidJsonDocument
* @uses \Ergebnis\Json\Normalizer\Format\JsonEncodeOptions
*/
final class SchemaNormalizerTest extends AbstractNormalizerTestCase
{
Expand Down
11 changes: 8 additions & 3 deletions test/Unit/Vendor/Composer/BinNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @internal
*
* @covers \Ergebnis\Json\Normalizer\Vendor\Composer\BinNormalizer
*
* @uses \Ergebnis\Json\Normalizer\Format\JsonEncodeOptions
*/
final class BinNormalizerTest extends AbstractComposerTestCase
{
Expand Down Expand Up @@ -81,8 +83,9 @@ public function testNormalizeSortsBinIfPropertyExistsAsArray(): void
JSON
);

$expected = \json_encode(\json_decode(
<<<'JSON'
$expected = \json_encode(
\json_decode(
<<<'JSON'
{
"bin": [
"another-script.php",
Expand All @@ -94,7 +97,9 @@ public function testNormalizeSortsBinIfPropertyExistsAsArray(): void
}
}
JSON
));
),
0,
);

$normalizer = new Vendor\Composer\BinNormalizer();

Expand Down
1 change: 1 addition & 0 deletions test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @covers \Ergebnis\Json\Normalizer\Vendor\Composer\ComposerJsonNormalizer
*
* @uses \Ergebnis\Json\Normalizer\ChainNormalizer
* @uses \Ergebnis\Json\Normalizer\Format\JsonEncodeOptions
* @uses \Ergebnis\Json\Normalizer\SchemaNormalizer
* @uses \Ergebnis\Json\Normalizer\Vendor\Composer\BinNormalizer
* @uses \Ergebnis\Json\Normalizer\Vendor\Composer\PackageHashNormalizer
Expand Down
17 changes: 13 additions & 4 deletions test/Unit/Vendor/Composer/PackageHashNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
namespace Ergebnis\Json\Normalizer\Test\Unit\Vendor\Composer;

use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Format;
use Ergebnis\Json\Normalizer\Vendor;

/**
* @internal
*
* @covers \Ergebnis\Json\Normalizer\Vendor\Composer\PackageHashNormalizer
*
* @uses \Ergebnis\Json\Normalizer\Format\JsonEncodeOptions
*/
final class PackageHashNormalizerTest extends AbstractComposerTestCase
{
Expand Down Expand Up @@ -56,7 +59,10 @@ public function testNormalizeIgnoresEmptyPackageHash(string $property): void
JSON
);

$expected = \json_encode(\json_decode($json->encoded()));
$expected = \json_encode(
\json_decode($json->encoded()),
0,
);

$normalizer = new Vendor\Composer\PackageHashNormalizer();

Expand Down Expand Up @@ -91,8 +97,9 @@ public function testNormalizeSortsPackageHashIfPropertyExists(string $property):
JSON
);

$expected = \json_encode(\json_decode(
<<<JSON
$expected = \json_encode(
\json_decode(
<<<JSON
{
"{$property}": {
"php": "Because why not, it's great.",
Expand All @@ -110,7 +117,9 @@ public function testNormalizeSortsPackageHashIfPropertyExists(string $property):
}
}
JSON
));
),
Format\JsonEncodeOptions::default()->toInt(),
);

$normalizer = new Vendor\Composer\PackageHashNormalizer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @internal
*
* @covers \Ergebnis\Json\Normalizer\Vendor\Composer\VersionConstraintNormalizer
*
* @uses \Ergebnis\Json\Normalizer\Format\JsonEncodeOptions
*/
final class VersionConstraintNormalizerTest extends AbstractComposerTestCase
{
Expand Down Expand Up @@ -70,7 +72,10 @@ public function testNormalizeIgnoresEmptyPackageHash(string $property): void
JSON
);

$expected = \json_encode(\json_decode($json->encoded()));
$expected = \json_encode(
\json_decode($json->encoded()),
0,
);

$normalizer = new Vendor\Composer\VersionConstraintNormalizer();

Expand Down