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: Sniff new-line character sequence #55

Merged
merged 2 commits into from
Apr 7, 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "^7.1",
"justinrainbow/json-schema": "^4.0.0 || ^5.0.0",
"localheinz/json-printer": "^1.0.0"
"localheinz/json-printer": "^2.0.0"
},
"require-dev": {
"infection/infection": "~0.8.1",
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion src/Format/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ final class Format implements FormatInterface
*/
private const PATTERN_INDENT = '/^[ \t]+$/';

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

/**
* @var int
*/
Expand All @@ -35,14 +40,20 @@ final class Format implements FormatInterface
*/
private $hasFinalNewLine;

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

/**
* @param int $jsonEncodeOptions
* @param string $indent
* @param string $newLine
* @param bool $hasFinalNewLine
*
* @throws \InvalidArgumentException
*/
public function __construct(int $jsonEncodeOptions, string $indent, bool $hasFinalNewLine)
public function __construct(int $jsonEncodeOptions, string $indent, string $newLine, bool $hasFinalNewLine)
{
if (0 > $jsonEncodeOptions) {
throw new \InvalidArgumentException(\sprintf(
Expand All @@ -58,8 +69,16 @@ public function __construct(int $jsonEncodeOptions, string $indent, bool $hasFin
));
}

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;
$this->hasFinalNewLine = $hasFinalNewLine;
}

Expand All @@ -73,6 +92,11 @@ public function indent(): string
return $this->indent;
}

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

public function hasFinalNewLine(): bool
{
return $this->hasFinalNewLine;
Expand Down Expand Up @@ -110,6 +134,22 @@ public function withIndent(string $indent): FormatInterface
return $mutated;
}

public function withNewLine(string $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;

return $mutated;
}

public function withHasFinalNewLine(bool $hasFinalNewLine): FormatInterface
{
$mutated = clone $this;
Expand Down
15 changes: 13 additions & 2 deletions src/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ public function jsonEncodeOptions(): int;

public function indent(): string;

public function newLine(): string;

public function hasFinalNewLine(): bool;

/**
* @param int $jsonEncodeOptions
*
* @throws \InvalidArgumentException
*
* @return FormatInterface
*/
public function withJsonEncodeOptions(int $jsonEncodeOptions): self;

/**
* @param string $indent
*
Expand All @@ -31,13 +42,13 @@ public function hasFinalNewLine(): bool;
public function withIndent(string $indent): self;

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

public function withHasFinalNewLine(bool $hasFinalNewLine): self;
}
5 changes: 3 additions & 2 deletions src/Format/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public function format(string $json, FormatInterface $format): string

$printed = $this->printer->print(
$encoded,
$format->indent()
$format->indent(),
$format->newLine()
);

if (!$format->hasFinalNewLine()) {
return $printed;
}

return $printed . PHP_EOL;
return $printed . $format->newLine();
}
}
10 changes: 10 additions & 0 deletions src/Format/Sniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function sniff(string $json): FormatInterface
return new Format(
$this->jsonEncodeOptions($json),
$this->indent($json),
$this->newLine($json),
$this->hasFinalNewLine($json)
);
}
Expand Down Expand Up @@ -55,6 +56,15 @@ private function indent(string $json): string
return ' ';
}

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

return PHP_EOL;
}

private function hasFinalNewLine(string $json): bool
{
if (\rtrim($json, " \t") === \rtrim($json)) {
Expand Down
Loading