Skip to content

Commit

Permalink
Enhancement: Allow creation of Header without File
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Oct 1, 2022
1 parent 4c72d0b commit 0152f09
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`1.2.0...main`][1.2.0...main].

## Added

- Added `Header::createWithoutReferenceToLicenseFile()` ([#534]), by [@localheinz]

## Changed

- Renamed `Header::create()` to `Header::createWithReferenceToLicenseFile()` ([#533]), by [@localheinz]
Expand Down Expand Up @@ -82,5 +86,6 @@ For a full diff see [`675601b...0.1.0`][675601b...0.1.0].
[#416]: https://github.com/ergebnis/license/pull/416
[#422]: https://github.com/ergebnis/license/pull/422
[#533]: https://github.com/ergebnis/license/pull/533
[#534]: https://github.com/ergebnis/license/pull/534

[@localheinz]: https://github.com/localheinz
3 changes: 3 additions & 0 deletions resource/header/without-reference-to-license-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Copyright (c) <range> <holder>

@see <url>
27 changes: 25 additions & 2 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ final class Header
private Template $template;
private Period $period;
private Holder $holder;
private File $file;
private ?File $file;
private Url $url;

private function __construct(
Template $template,
Period $period,
Holder $holder,
File $file,
?File $file,
Url $url
) {
$this->template = $template;
Expand All @@ -51,8 +51,31 @@ public static function createWithReferenceToLicenseFile(
);
}

public static function createWithoutReferenceToLicenseFile(
Template $template,
Period $range,
Holder $holder,
Url $url
): self {
return new self(
$template,
$range,
$holder,
null,
$url,
);
}

public function toString(): string
{
if (!$this->file instanceof File) {
return $this->template->toString([
'<holder>' => $this->holder->toString(),
'<range>' => $this->period->toString(),
'<url>' => $this->url->toString(),
]);
}

return $this->template->toString([
'<file>' => \basename($this->file->name()),
'<holder>' => $this->holder->toString(),
Expand Down
28 changes: 28 additions & 0 deletions test/Unit/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,32 @@ public function testCreateWithReferenceToLicenseFileReturnsHeader(): void

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

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

$template = Template::fromFile(__DIR__ . '/../../resource/header/without-reference-to-license-file.txt');
$range = Range::since(
Year::fromString($faker->year()),
new \DateTimeZone($faker->timezone()),
);
$holder = Holder::fromString($faker->name());
$url = Url::fromString($faker->url);

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

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

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

0 comments on commit 0152f09

Please sign in to comment.