Skip to content

Commit

Permalink
Enhancement: Implement NoFinalNewLineNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 12, 2018
1 parent 0d0c5ff commit 0adde4d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
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,
];
}
}
}

0 comments on commit 0adde4d

Please sign in to comment.