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: Implement NoFinalNewLineNormalizer #3

Merged
merged 1 commit into from
Jan 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This package comes with the following normalizers:

* [`Localheinz\Json\Normalizer\FinalNewLineNormalizer`](#finalnewlinenormalizer)
* [`Localheinz\Json\Normalizer\IndentNormalizer`](#indentnormalizer)
* [`Localheinz\Json\Normalizer\NoFinalNewLineNormalizer`](#nofinalnewlinenormalizer)

:bulb: All of these normalizers implement the `Localheinz\Json\Normalizer\NormalizerInterface`.

Expand Down Expand Up @@ -70,6 +71,29 @@ $normalized = $normalizer->normalize($json);

The normalized version will now be indented with 2 spaces.

### `NoFinalNewLineNormalizer`

If you want to ensure that a JSON file does not have a final new line, you can use the `FinalNewLineNormalizer`.

```php
use Localheinz\Json\Normalizer;

$json = <<<'JSON'
{
"name": "Andreas Möller",
"url": "https://localheinz.com"
}


JSON;

$normalizer = new Normalizer\NoFinalNewLineNormalizer();

$normalized = $normalizer->normalize($json);
```

The normalized version will now not have a final new line or any whitespace at the end.

## Contributing

Please have a look at [`CONTRIBUTING.md`](.github/CONTRIBUTING.md).
Expand Down
29 changes: 29 additions & 0 deletions src/NoFinalNewLineNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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;

final class NoFinalNewLineNormalizer implements NormalizerInterface
{
public function normalize(string $json): string
{
if (null === \json_decode($json) && JSON_ERROR_NONE !== \json_last_error()) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not valid JSON.',
$json
));
}

return \rtrim($json);
}
}
82 changes: 82 additions & 0 deletions test/Unit/NoFinalNewLineNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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\Test\Unit;

use Localheinz\Json\Normalizer\NoFinalNewLineNormalizer;
use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

final class NoFinalNewLineNormalizerTest extends Framework\TestCase
{
use Helper;

public function testImplementsNormalizerInterface()
{
$this->assertClassImplementsInterface(NormalizerInterface::class, NoFinalNewLineNormalizer::class);
}

public function testNormalizeRejectsInvalidJson()
{
$json = $this->faker()->realText();

$normalizer = new NoFinalNewLineNormalizer();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
'"%s" is not valid JSON.',
$json
));

$normalizer->normalize($json);
}

/**
* @dataProvider providerWhitespace
*
* @param string $whitespace
*/
public function testNormalizeRemovesAllWhitespaceFromEndOfJson(string $whitespace)
{
$json = <<<'JSON'
{
"name": "Andreas Möller",
"url": "https://localheinz.com"
}
JSON;
$json .= $whitespace;

$normalized = \rtrim($json);

$normalizer = new NoFinalNewLineNormalizer();

$this->assertSame($normalized, $normalizer->normalize($json));
}

public function providerWhitespace()
{
$values = [
'',
' ',
"\t",
PHP_EOL,
];

foreach ($values as $value) {
yield [
$value,
];
}
}
}