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: Extract NewLine value object #76

Merged
merged 1 commit into from
Aug 18, 2018
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
41 changes: 11 additions & 30 deletions src/Format/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@

final class Format implements FormatInterface
{
/**
* Constant for a regular expression matching valid new-line character sequence.
*/
private const PATTERN_NEW_LINE = '/^(?>\r\n|\n|\r)$/';

/**
* @var int
*/
Expand All @@ -31,24 +26,24 @@ final class Format implements FormatInterface
private $indent;

/**
* @var bool
* @var NewLineInterface
*/
private $hasFinalNewLine;
private $newLine;

/**
* @var string
* @var bool
*/
private $newLine;
private $hasFinalNewLine;

/**
* @param int $jsonEncodeOptions
* @param IndentInterface $indent
* @param string $newLine
* @param bool $hasFinalNewLine
* @param int $jsonEncodeOptions
* @param IndentInterface $indent
* @param NewLineInterface $newLine
* @param bool $hasFinalNewLine
*
* @throws \InvalidArgumentException
*/
public function __construct(int $jsonEncodeOptions, IndentInterface $indent, string $newLine, bool $hasFinalNewLine)
public function __construct(int $jsonEncodeOptions, IndentInterface $indent, NewLineInterface $newLine, bool $hasFinalNewLine)
{
if (0 > $jsonEncodeOptions) {
throw new \InvalidArgumentException(\sprintf(
Expand All @@ -57,13 +52,6 @@ public function __construct(int $jsonEncodeOptions, IndentInterface $indent, str
));
}

if (1 !== \preg_match(self::PATTERN_NEW_LINE, $newLine)) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not a valid new-line character sequence.',
$newLine
));
}

$this->jsonEncodeOptions = $jsonEncodeOptions;
$this->indent = $indent;
$this->newLine = $newLine;
Expand All @@ -80,7 +68,7 @@ public function indent(): IndentInterface
return $this->indent;
}

public function newLine(): string
public function newLine(): NewLineInterface
{
return $this->newLine;
}
Expand Down Expand Up @@ -115,15 +103,8 @@ public function withIndent(IndentInterface $indent): FormatInterface
return $mutated;
}

public function withNewLine(string $newLine): FormatInterface
public function withNewLine(NewLineInterface $newLine): FormatInterface
{
if (1 !== \preg_match(self::PATTERN_NEW_LINE, $newLine)) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not a valid new-line character sequence.',
$newLine
));
}

$mutated = clone $this;

$mutated->newLine = $newLine;
Expand Down
11 changes: 2 additions & 9 deletions src/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function jsonEncodeOptions(): int;

public function indent(): IndentInterface;

public function newLine(): string;
public function newLine(): NewLineInterface;

public function hasFinalNewLine(): bool;

Expand All @@ -34,14 +34,7 @@ public function withJsonEncodeOptions(int $jsonEncodeOptions): self;

public function withIndent(IndentInterface $indent): self;

/**
* @param string $newLine
*
* @throws \InvalidArgumentException
*
* @return FormatInterface
*/
public function withNewLine(string $newLine): self;
public function withNewLine(NewLineInterface $newLine): self;

public function withHasFinalNewLine(bool $hasFinalNewLine): self;
}
2 changes: 1 addition & 1 deletion src/Format/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function format(string $json, FormatInterface $format): string
$printed = $this->printer->print(
$encoded,
(string) $format->indent(),
$format->newLine()
(string) $format->newLine()
);

if (!$format->hasFinalNewLine()) {
Expand Down
51 changes: 51 additions & 0 deletions src/Format/NewLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/json-normalizer
*/

namespace Localheinz\Json\Normalizer\Format;

final class NewLine implements NewLineInterface
{
/**
* @var string
*/
private $string;

private function __construct(string $string)
{
$this->string = $string;
}

public function __toString(): string
{
return $this->string;
}

/**
* @param string $string
*
* @throws \InvalidArgumentException
*
* @return NewLineInterface
*/
public static function fromString(string $string): NewLineInterface
{
if (1 !== \preg_match('/^(?>\r\n|\n|\r)$/', $string)) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not a valid new-line character sequence.',
$string
));
}

return new self($string);
}
}
19 changes: 19 additions & 0 deletions src/Format/NewLineInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/json-normalizer
*/

namespace Localheinz\Json\Normalizer\Format;

interface NewLineInterface
{
public function __toString(): string;
}
6 changes: 3 additions & 3 deletions src/Format/Sniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ private function indent(string $json): IndentInterface
);
}

private function newLine(string $json): string
private function newLine(string $json): NewLineInterface
{
if (1 === \preg_match('/(?P<newLine>\r\n|\n|\r)/', $json, $match)) {
return $match['newLine'];
return NewLine::fromString($match['newLine']);
}

return \PHP_EOL;
return NewLine::fromString(\PHP_EOL);
}

private function hasFinalNewLine(string $json): bool
Expand Down
Loading