diff --git a/.php_cs b/.php_cs index 252e8c9d..de1d2b90 100644 --- a/.php_cs +++ b/.php_cs @@ -21,14 +21,52 @@ $range = License\Range::since( ); $url = License\Url::fromString('https://github.com/ergebnis/license'); -$header = <<toString()} {$holder->toString()} +$headerTemplate = License\Template::fromString( + <<<'EOF' +Copyright (c) For the full copyright and license information, please view -the LICENSE file that was distributed with this source code. +the file that was distributed with this source code. -@see {$url->toString()} -EOF; +@see +EOF +); + +$fileTemplate = License\Template::fromString( + <<<'EOF' +The MIT License (MIT) + +Copyright (c) + +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->toString(), + '' => $range->toString(), +])); + +$header = $headerTemplate->toString([ + '' => \basename($file), + '' => $holder->toString(), + '' => $range->toString(), + '' => $url->toString(), +]); $config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($header)); diff --git a/CHANGELOG.md b/CHANGELOG.md index dfbbfdb4..038877db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ 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 @@ -21,5 +22,6 @@ For a full diff see [`675601b...master`][675601b...master]. [#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 diff --git a/LICENSE b/LICENSE index 6bc0bf4a..28f8792f 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/src/Exception/InvalidReplacements.php b/src/Exception/InvalidReplacements.php new file mode 100644 index 00000000..0fa4b2e2 --- /dev/null +++ b/src/Exception/InvalidReplacements.php @@ -0,0 +1,27 @@ +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 + ); + } +} diff --git a/test/Unit/Exception/InvalidReplacementsTest.php b/test/Unit/Exception/InvalidReplacementsTest.php new file mode 100644 index 00000000..14e3bb9f --- /dev/null +++ b/test/Unit/Exception/InvalidReplacementsTest.php @@ -0,0 +1,41 @@ +getMessage()); + } + + public function testValuesReturnsInvalidReplacements(): void + { + $exception = InvalidReplacements::values(); + + self::assertSame('Values need to be strings.', $exception->getMessage()); + } +} diff --git a/test/Unit/TemplateTest.php b/test/Unit/TemplateTest.php new file mode 100644 index 00000000..13ab7a68 --- /dev/null +++ b/test/Unit/TemplateTest.php @@ -0,0 +1,134 @@ +dateTime->format('Y'); + $who = $faker->name; + + $template = Template::fromString( + <<<'EOF' +Ah! + +This was done in by . + +Who would have thought? + +EOF + ); + + $expected = <<toString([ + '' => $when, + '' => $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, + ], + ]; + } + } +}