Skip to content

Commit

Permalink
Merge pull request #608 from ergebnis/feature/named
Browse files Browse the repository at this point in the history
Enhancement: Extract named constructor
  • Loading branch information
localheinz committed Dec 29, 2021
2 parents b4be9cf + a7d2dee commit 76e9c34
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ For a full diff see [`1.0.3...main`][1.0.3...main].
- Renamed `Format::__toString()`, `Indent::__toString()`, and `Json::__toString()` to `Format::toString()`, `Indent::toString()`, and `Json::toString()`, requiring consumers to explicitly invoke methods instead of allowing to cast to `string` ([#589]), by [@localheinz]
- Started using the `SchemaValidator` provided by `ergebnis/json-schema-validator` ([#595]), by [@localheinz]
- Renamed `Format\JsonEncodeOptions::value()` to `Format\JsonEncodeOptions::toInt()` ([#603]), by [@localheinz]
- Extracted `Format\Format::create()` as named constructor and reduced visibility of `__construct` to `private` ([#608]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -436,6 +437,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#595]: https://github.com/ergebnis/json-normalizer/pull/595
[#597]: https://github.com/ergebnis/json-normalizer/pull/597
[#603]: https://github.com/ergebnis/json-normalizer/pull/603
[#608]: https://github.com/ergebnis/json-normalizer/pull/608

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
16 changes: 15 additions & 1 deletion src/Format/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Format
private NewLine $newLine;
private bool $hasFinalNewLine;

public function __construct(
private function __construct(
JsonEncodeOptions $jsonEncodeOptions,
Indent $indent,
NewLine $newLine,
Expand All @@ -34,6 +34,20 @@ public function __construct(
$this->hasFinalNewLine = $hasFinalNewLine;
}

public static function create(
JsonEncodeOptions $jsonEncodeOptions,
Indent $indent,
NewLine $newLine,
bool $hasFinalNewLine
): self {
return new self(
$jsonEncodeOptions,
$indent,
$newLine,
$hasFinalNewLine,
);
}

public static function fromJson(Json $json): self
{
$encoded = $json->encoded();
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/FixedFormatNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testNormalizeNormalizesAndFormatsUsingFormat(): void
{
$faker = self::faker();

$format = new Format\Format(
$format = Format\Format::create(
Format\JsonEncodeOptions::fromInt($faker->numberBetween(1)),
Format\Indent::fromString("\t"),
Format\NewLine::fromString("\r\n"),
Expand Down
12 changes: 6 additions & 6 deletions test/Unit/Format/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ final class FormatTest extends Framework\TestCase
/**
* @dataProvider \Ergebnis\DataProvider\BoolProvider::arbitrary()
*/
public function testConstructorSetsValues(bool $hasFinalNewLine): void
public function testCreateReturnsFormat(bool $hasFinalNewLine): void
{
$jsonEncodeOptions = Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES);
$indent = Format\Indent::fromString(' ');
$newLine = Format\NewLine::fromString("\r\n");

$format = new Format\Format(
$format = Format\Format::create(
$jsonEncodeOptions,
$indent,
$newLine,
Expand All @@ -53,7 +53,7 @@ public function testConstructorSetsValues(bool $hasFinalNewLine): void

public function testWithJsonEncodeOptionsClonesFormatAndSetsJsonEncodeOptions(): void
{
$format = new Format\Format(
$format = Format\Format::create(
Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES),
Format\Indent::fromString(' '),
Format\NewLine::fromString("\r\n"),
Expand All @@ -72,7 +72,7 @@ public function testWithIndentClonesFormatAndSetsIndent(): void
{
$indent = Format\Indent::fromString("\t");

$format = new Format\Format(
$format = Format\Format::create(
Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES),
Format\Indent::fromString(' '),
Format\NewLine::fromString("\r\n"),
Expand All @@ -89,7 +89,7 @@ public function testWithNewLineClonesFormatAndSetsNewLine(): void
{
$newLine = Format\NewLine::fromString("\r\n");

$format = new Format\Format(
$format = Format\Format::create(
Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES),
Format\Indent::fromString(' '),
Format\NewLine::fromString("\r"),
Expand All @@ -107,7 +107,7 @@ public function testWithNewLineClonesFormatAndSetsNewLine(): void
*/
public function testWithHasFinalNewLineClonesFormatAndSetsFinalNewLine(bool $hasFinalNewLine): void
{
$format = new Format\Format(
$format = Format\Format::create(
Format\JsonEncodeOptions::fromInt(\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES),
Format\Indent::fromString(' '),
Format\NewLine::fromString("\r\n"),
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/Format/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testFormatEncodesWithJsonEncodeOptionsIndentsAndPossiblySuffixes
}
JSON;

$format = new Format\Format(
$format = Format\Format::create(
Format\JsonEncodeOptions::fromInt($jsonEncodeOptions),
Format\Indent::fromString($indentString),
Format\NewLine::fromString($newLineString),
Expand Down

0 comments on commit 76e9c34

Please sign in to comment.