Skip to content

Commit

Permalink
Enhancement: Add Template
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 9, 2020
1 parent 5a3e779 commit 1d4ed69
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 6 deletions.
48 changes: 43 additions & 5 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,52 @@ $range = License\Range::since(
);
$url = License\Url::fromString('https://github.com/ergebnis/license');

$header = <<<EOF
Copyright (c) {$range->toString()} {$holder->toString()}
$headerTemplate = License\Template::fromString(
<<<'EOF'
Copyright (c) <range> <holder>
For the full copyright and license information, please view
the LICENSE file that was distributed with this source code.
the <file> file that was distributed with this source code.
@see {$url->toString()}
EOF;
@see <url>
EOF
);

$fileTemplate = License\Template::fromString(
<<<'EOF'
The MIT License (MIT)
Copyright (c) <range> <holder>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

EOF
);

$file = __DIR__ . '/LICENSE';

\file_put_contents($file, $fileTemplate->toString([
'<holder>' => $holder->toString(),
'<range>' => $range->toString(),
]));

$header = $headerTemplate->toString([
'<file>' => \basename($file),
'<holder>' => $holder->toString(),
'<range>' => $range->toString(),
'<url>' => $url->toString(),
]);

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($header));

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ For a full diff see [`675601b...master`][675601b...master].
* Added `Year` ([#10]), by [@localheinz]
* Added `Range` ([#12]), by [@localheinz]
* Added `Url` ([#19]), by [@localheinz]
* Added `Template` ([#20]), by [@localheinz]

[675601b...master]: https://github.com/ergebnis/license/compare/675601b...master

[#7]: https://github.com/ergebnis/license/pull/7
[#10]: https://github.com/ergebnis/license/pull/10
[#12]: https://github.com/ergebnis/license/pull/12
[#19]: https://github.com/ergebnis/license/pull/18
[#20]: https://github.com/ergebnis/license/pull/18

[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Andreas Möller
Copyright (c) 2020 Andreas Möller

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
Expand Down
27 changes: 27 additions & 0 deletions src/Exception/InvalidReplacements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 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/ergebnis/license
*/

namespace Ergebnis\License\Exception;

final class InvalidReplacements extends \InvalidArgumentException implements Exception
{
public static function keys(): self
{
return new self('Keys need to be strings.');
}

public static function values(): self
{
return new self('Values need to be strings.');
}
}
61 changes: 61 additions & 0 deletions src/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 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/ergebnis/license
*/

namespace Ergebnis\License;

final class Template
{
private $value;

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

public static function fromString(string $value): self
{
return new self($value);
}

/**
* @param array $replacements
*
* @throws Exception\InvalidReplacements
*
* @return string
*/
public function toString(array $replacements): string
{
$invalidKeys = \array_filter(\array_keys($replacements), static function ($key): bool {
return !\is_string($key);
});

if ([] !== $invalidKeys) {
throw Exception\InvalidReplacements::keys();
}

$invalidValues = \array_filter($replacements, static function ($value): bool {
return !\is_string($value);
});

if ([] !== $invalidValues) {
throw Exception\InvalidReplacements::values();
}

return \str_replace(
\array_keys($replacements),
$replacements,
$this->value
);
}
}
41 changes: 41 additions & 0 deletions test/Unit/Exception/InvalidReplacementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 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/ergebnis/license
*/

namespace Ergebnis\License\Test\Unit\Exception;

use Ergebnis\License\Exception\InvalidReplacements;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\License\Exception\InvalidReplacements
*
* @uses \Ergebnis\License\Year
*/
final class InvalidReplacementsTest extends Framework\TestCase
{
public function testKeysReturnsInvalidReplacements(): void
{
$exception = InvalidReplacements::keys();

self::assertSame('Keys need to be strings.', $exception->getMessage());
}

public function testValuesReturnsInvalidReplacements(): void
{
$exception = InvalidReplacements::values();

self::assertSame('Values need to be strings.', $exception->getMessage());
}
}
134 changes: 134 additions & 0 deletions test/Unit/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 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/ergebnis/license
*/

namespace Ergebnis\License\Test\Unit;

use Ergebnis\License\Exception;
use Ergebnis\License\Template;
use Ergebnis\Test\Util\Helper;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\License\Template
*
* @uses \Ergebnis\License\Exception\InvalidReplacements
*/
final class TemplateTest extends Framework\TestCase
{
use Helper;

public function testCreateReturnsTemplate(): void
{
$faker = self::faker();

$when = $faker->dateTime->format('Y');
$who = $faker->name;

$template = Template::fromString(
<<<'EOF'
Ah!
This was done in <when> by <who>.
Who would have thought?

EOF
);

$expected = <<<EOF
Ah!
This was done in {$when} by {$who}.
Who would have thought?
EOF;

self::assertSame($expected, $template->toString([
'<when>' => $when,
'<who>' => $who,
]));
}

/**
* @dataProvider provideReplacementsWithInvalidKeys
*
* @param array $replacements
*/
public function testToStringRejectsReplacementsWithInvalidKeys(array $replacements): void
{
$template = Template::fromString('');

$this->expectException(Exception\InvalidReplacements::class);

$template->toString($replacements);
}

public function provideReplacementsWithInvalidKeys(): \Generator
{
$faker = self::faker();

$values = [
'float' => $faker->randomFloat(2, 1),
'int' => $faker->numberBetween(1),
];

foreach ($values as $key => $value) {
yield $key => [
[
$value => 'foo',
],
];
}
}

/**
* @dataProvider provideReplacementsWithInvalidValues
*
* @param array $replacements
*/
public function testToStringRejectsReplacementsWithInvalidValues(array $replacements): void
{
$template = Template::fromString('');

$this->expectException(Exception\InvalidReplacements::class);

$template->toString($replacements);
}

public function provideReplacementsWithInvalidValues(): \Generator
{
$faker = self::faker();

$values = [
'array' => $faker->words,
'boolean-false' => false,
'boolean-true' => true,
'float' => $faker->randomFloat(2, 1),
'int' => $faker->numberBetween(1),
'null' => null,
'object' => new \stdClass(),
'resource' => \fopen(__FILE__, 'rb'),
];

foreach ($values as $key => $value) {
yield $key => [
[
'foo' => $value,
],
];
}
}
}

0 comments on commit 1d4ed69

Please sign in to comment.