diff --git a/.php_cs b/.php_cs index 32678dcf..7dbb5294 100644 --- a/.php_cs +++ b/.php_cs @@ -13,9 +13,40 @@ declare(strict_types=1); use Ergebnis\PhpCsFixer\Config; +$years = Config\License\Copyright\Years::fromYear(Config\License\Copyright\Year::fromString('2019')); +$holder = Config\License\Copyright\Holder::fromString('Andreas Möller'); + +$file = Config\License\File::create( + $years, + $holder, + Config\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->saveAs(__DIR__ . '/LICENSE'); + $header = Config\License\Header::create( - Config\License\Copyright\Years::fromYear(Config\License\Copyright\Year::fromString('2019')), - Config\License\Copyright\Holder::fromString('Andreas Möller'), + $years, + $holder, Config\License\Notice::fromString( <<<'EOF' For the full copyright and license information, please view diff --git a/composer.json b/composer.json index 581625de..3be2e2ef 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "phpstan/phpstan-strict-rules": "~0.12.0", "phpunit/phpunit": "^7.5.18", "psalm/plugin-phpunit": "~0.8.0", + "symfony/filesystem": "^4.4.0", "vimeo/psalm": "^3.8.1" }, "config": { diff --git a/composer.lock b/composer.lock index ab4b93e7..9e16b469 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c89b5bca275f3a3fab9cbd570e300a16", + "content-hash": "92e32fcd84e52211d528621fa5c14272", "packages": [ { "name": "composer/semver", @@ -727,7 +727,7 @@ }, { "name": "symfony/filesystem", - "version": "v4.4.1", + "version": "v4.4.2", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", diff --git a/src/License/File.php b/src/License/File.php new file mode 100644 index 00000000..5b847216 --- /dev/null +++ b/src/License/File.php @@ -0,0 +1,58 @@ +years = $years; + $this->holder = $holder; + $this->template = $template; + } + + public static function create(Copyright\Years $years, Copyright\Holder $holder, Template $template): self + { + return new self( + $years, + $holder, + $template + ); + } + + public function toString(): string + { + return $this->template->toString([ + '' => $this->years->toString(), + '' => $this->holder->toString(), + ]); + } + + public function saveAs(string $filename): void + { + \file_put_contents( + $filename, + $this->toString() + ); + } +} diff --git a/src/License/Template.php b/src/License/Template.php new file mode 100644 index 00000000..df7166a2 --- /dev/null +++ b/src/License/Template.php @@ -0,0 +1,41 @@ +value = $value; + } + + public static function fromString(string $value): self + { + return new self($value); + } + + public function toString(array $replacements): string + { + return \str_replace( + \array_keys($replacements), + $replacements, + $this->value + ); + } +} diff --git a/test/Unit/License/FileTest.php b/test/Unit/License/FileTest.php new file mode 100644 index 00000000..f5d3b0b0 --- /dev/null +++ b/test/Unit/License/FileTest.php @@ -0,0 +1,104 @@ +mkdir(self::directory()); + } + + protected function tearDown(): void + { + $filesystem = new Filesystem\Filesystem(); + + $filesystem->remove(self::directory()); + } + + public function testCreateReturnsFile(): void + { + $faker = self::faker(); + + $years = Years::fromYear(Year::fromString($faker->dateTime->format('Y'))); + $holder = Holder::fromString($faker->name); + + $template = Template::fromString( + <<<'EOF' +Ah! + +This was done in by . + +Who would have thought? + +EOF + ); + + $file = File::create( + $years, + $holder, + $template + ); + + $expected = <<toString()} by {$holder->toString()}. + +Who would have thought? + +EOF; + + self::assertSame($expected, $file->toString()); + + $filename = \sprintf( + '%s/%s', + self::directory(), + 'example.txt' + ); + + $file->saveAs($filename); + + self::assertFileExists($filename); + self::assertSame($expected, \file_get_contents($filename)); + } + + private static function directory(): string + { + return __DIR__ . '/../../../.build/test'; + } +} diff --git a/test/Unit/License/TemplateTest.php b/test/Unit/License/TemplateTest.php new file mode 100644 index 00000000..fd6d2efa --- /dev/null +++ b/test/Unit/License/TemplateTest.php @@ -0,0 +1,61 @@ +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, + ])); + } +}