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: Add Header #34

Merged
merged 1 commit into from
Jan 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ declare(strict_types=1);
use Ergebnis\License;
use Ergebnis\PhpCsFixer\Config;

$holder = License\Holder::fromString('Andreas Möller');
$range = License\Range::since(
License\Year::fromString('2020'),
new \DateTimeZone('UTC')
);
$url = License\Url::fromString('https://github.com/ergebnis/license');

$headerTemplate = License\Template::fromFile(__DIR__ . '/.license/header-template.txt');
$holder = License\Holder::fromString('Andreas Möller');

$file = License\File::create(
__DIR__ . '/LICENSE',
Expand All @@ -32,14 +30,15 @@ $file = License\File::create(

$file->save();

$header = $headerTemplate->toString([
'<file>' => \basename($file->name()),
'<holder>' => $holder->toString(),
'<range>' => $range->toString(),
'<url>' => $url->toString(),
]);
$header = License\Header::create(
License\Template::fromFile(__DIR__ . '/resource/header.txt'),
$range,
$holder,
$file,
License\Url::fromString('https://github.com/ergebnis/license')
);

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

$config->getFinder()
->ignoreDotFiles(false)
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For a full diff see [`675601b...master`][675601b...master].
* Added `Url` ([#19]), by [@localheinz]
* Added `Template` ([#20]), by [@localheinz]
* Added `File` ([#33]), by [@localheinz]
* Added `Header` ([#34]), by [@localheinz]

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

Expand All @@ -25,5 +26,6 @@ For a full diff see [`675601b...master`][675601b...master].
[#19]: https://github.com/ergebnis/license/pull/19
[#20]: https://github.com/ergebnis/license/pull/20
[#33]: https://github.com/ergebnis/license/pull/33
[#34]: https://github.com/ergebnis/license/pull/34

[@localheinz]: https://github.com/localheinz
File renamed without changes.
57 changes: 57 additions & 0 deletions src/Header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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 Header
{
private $template;

private $period;

private $holder;

private $file;

private $url;

private function __construct(Template $template, Period $period, Holder $holder, File $file, Url $url)
{
$this->template = $template;
$this->period = $period;
$this->holder = $holder;
$this->file = $file;
$this->url = $url;
}

public static function create(Template $template, Period $period, Holder $holder, File $file, Url $url): self
{
return new self(
$template,
$period,
$holder,
$file,
$url
);
}

public function toString(): string
{
return $this->template->toString([
'<file>' => \basename($this->file->name()),
'<holder>' => $this->holder->toString(),
'<range>' => $this->period->toString(),
'<url>' => $this->url->toString(),
]);
}
}
81 changes: 81 additions & 0 deletions test/Unit/HeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\File;
use Ergebnis\License\Header;
use Ergebnis\License\Holder;
use Ergebnis\License\Range;
use Ergebnis\License\Template;
use Ergebnis\License\Url;
use Ergebnis\License\Year;
use Ergebnis\Test\Util\Helper;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\License\Header
*
* @uses \Ergebnis\License\File
* @uses \Ergebnis\License\Holder
* @uses \Ergebnis\License\Range
* @uses \Ergebnis\License\Template
* @uses \Ergebnis\License\Url
* @uses \Ergebnis\License\Year
*/
final class HeaderTest extends Framework\TestCase
{
use Helper;

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

$template = Template::fromFile(__DIR__ . '/../../resource/header.txt');
$range = Range::since(
Year::fromString($faker->year),
new \DateTimeZone($faker->timezone)
);
$holder = Holder::fromString($faker->name);
$file = File::create(
\sprintf(
'%s/%s.txt',
__DIR__,
$faker->slug
),
Template::fromFile(__DIR__ . '/../../resource/license/MIT.txt'),
$range,
$holder
);
$url = Url::fromString($faker->url);

$header = Header::create(
$template,
$range,
$holder,
$file,
$url
);

$expected = $template->toString([
'<file>' => \basename($file->name()),
'<holder>' => $holder->toString(),
'<range>' => $range->toString(),
'<url>' => $url->toString(),
]);

self::assertSame($expected, $header->toString());
}
}