Skip to content

Commit

Permalink
Merge pull request #9 from fabianpnke/configuration
Browse files Browse the repository at this point in the history
Make the date format configurable
  • Loading branch information
dvdheiden authored Mar 17, 2024
2 parents 7110d9f + ce09c9a commit 10dda1c
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added

- Add support for Laravel 11.
- Add support for Laravel 11. Thanks to @julesjanssen.
- Make the Dateformat configurable. Thanks to @fabianpnke.

## [2.1.0] - 2023-05-30

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ $parser = new \Vdhicts\KeepAChangelog\Parser();
$changelog = $parser->parse($content);
```

### Using a different date format

The parser will parse the releases with the `Y-m-d` format by default.
If you would like to use another format, you can just pass the format to the `setDateFormat` function.

```php
$content = file_get_contents('CHANGELOG.md');

$parser = new \Vdhicts\KeepAChangelog\Parser();
$changelog = $parser
->setDateFormat('Y-m-d H:i:s')
->parse($content);
```

### Accessing the changelog

The parser will return a `Changelog` models, with contains a collection of `Release` models. The `Release` model
Expand Down
11 changes: 10 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class Parser
{
private string $dateFormat = 'Y-m-d';

private function getNodesText(Crawler $crawler): array
{
return $crawler->each(function ($node) {
Expand All @@ -30,7 +32,7 @@ private function parseRelease(Crawler $parsedRelease): Release

$version = str_replace(['[', ']'], '', $releaseData[0]);
$date = count($releaseData) >= 2
? Carbon::createFromFormat('Y-m-d', $releaseData[1])
? Carbon::createFromFormat($this->dateFormat, $releaseData[1])
: null;

return new Release($version, $date);
Expand All @@ -56,6 +58,13 @@ private function parseEntries(Crawler $lines)
});
}

public function setDateFormat(string $dateFormat): Parser
{
$this->dateFormat = $dateFormat;

return $this;
}

public function parse(string $content): Changelog
{
$crawler = new Crawler($this->getChangelogHtml($content));
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/ParserTestDateTimeFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Unit;

use PHPUnit\Framework\TestCase;
use Vdhicts\KeepAChangelog\Models\Changelog;
use Vdhicts\KeepAChangelog\Parser;

class ParserTestDateTimeFormatTest extends TestCase
{
private Changelog $changelog;

protected function setUp(): void
{
$changelogContent = file_get_contents(__DIR__.'/../resources/ExampleChangelogDateTimeFormat.md');

$parser = new Parser();
$parser->setDateFormat('Y-m-d H:i');
$this->changelog = $parser->parse($changelogContent);
}

public function testRelease()
{
$release = $this
->changelog
->getLatestRelease();

$this->assertSame('1.0.0', $release->getVersion());
$this->assertSame('2017-06-20 01:22', $release->getReleasedAt()->format('Y-m-d H:i'));
}
}
141 changes: 141 additions & 0 deletions tests/resources/ExampleChangelogDateTimeFormat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.0.0] - 2017-06-20 01:22
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.
- Links to latest released version in previous versions.
- "Why keep a changelog?" section.
- "Who needs a changelog?" section.
- "How do I make a changelog?" section.
- "Frequently Asked Questions" section.
- New "Guiding Principles" sub-section to "How do I make a changelog?".
- Simplified and Traditional Chinese translations from [@tianshuo](https://github.com/tianshuo).
- German translation from [@mpbzh](https://github.com/mpbzh) & [@Art4](https://github.com/Art4).
- Italian translation from [@azkidenz](https://github.com/azkidenz).
- Swedish translation from [@magol](https://github.com/magol).
- Turkish translation from [@karalamalar](https://github.com/karalamalar).
- French translation from [@zapashcanon](https://github.com/zapashcanon).
- Brazilian Portugese translation from [@Webysther](https://github.com/Webysther).
- Polish translation from [@amielucha](https://github.com/amielucha) & [@m-aciek](https://github.com/m-aciek).
- Russian translation from [@aishek](https://github.com/aishek).
- Czech translation from [@h4vry](https://github.com/h4vry).
- Slovak translation from [@jkostolansky](https://github.com/jkostolansky).
- Korean translation from [@pierceh89](https://github.com/pierceh89).
- Croatian translation from [@porx](https://github.com/porx).
- Persian translation from [@Hameds](https://github.com/Hameds).
- Ukrainian translation from [@osadchyi-s](https://github.com/osadchyi-s).

### Changed
- Start using "changelog" over "change log" since it's the common usage.
- Start versioning based on the current English version at 0.3.0 to help
translation authors keep things up-to-date.
- Rewrite "What makes unicorns cry?" section.
- Rewrite "Ignoring Deprecations" sub-section to clarify the ideal
scenario.
- Improve "Commit log diffs" sub-section to further argument against
them.
- Merge "Why can’t people just use a git log diff?" with "Commit log
diffs"
- Fix typos in Simplified Chinese and Traditional Chinese translations.
- Fix typos in Brazilian Portuguese translation.
- Fix typos in Turkish translation.
- Fix typos in Czech translation.
- Fix typos in Swedish translation.
- Improve phrasing in French translation.
- Fix phrasing and spelling in German translation.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03 01:00
### Added
- RU translation from [@aishek](https://github.com/aishek).
- pt-BR translation from [@tallesl](https://github.com/tallesl).
- es-ES translation from [@ZeliosAriex](https://github.com/ZeliosAriex).

## [0.2.0] - 2015-10-06 02:00
### Changed
- Remove exclusionary mentions of "open source" since this project can
benefit both "open" and "closed" source projects equally.

## [0.1.0] - 2015-10-06 03:00
### Added
- Answer "Should you ever rewrite a change log?".

### Changed
- Improve argument against commit logs.
- Start following [SemVer](https://semver.org) properly.

## [0.0.8] - 2015-02-17 04:00
### Changed
- Update year to match in every README example.
- Reluctantly stop making fun of Brits only, since most of the world
writes dates in a strange way.

### Fixed
- Fix typos in recent README changes.
- Update outdated unreleased diff link.

## [0.0.7] - 2015-02-16 05:00
### Added
- Link, and make it obvious that date format is ISO 8601.

### Changed
- Clarified the section on "Is there a standard change log format?".

### Fixed
- Fix Markdown links to tag comparison URL with footnote-style links.

## [0.0.6] - 2014-12-12 06:00
### Added
- README section on "yanked" releases.

## [0.0.5] - 2014-08-09 07:00
### Added
- Markdown links to version tags on release headings.
- Unreleased section to gather unreleased changes and encourage note
keeping prior to releases.

## [0.0.4] - 2014-08-09 08:00
### Added
- Better explanation of the difference between the file ("CHANGELOG")
and its function "the change log".

### Changed
- Refer to a "change log" instead of a "CHANGELOG" throughout the site
to differentiate between the file and the purpose of the file — the
logging of changes.

### Removed
- Remove empty sections from CHANGELOG, they occupy too much space and
create too much noise in the file. People will have to assume that the
missing sections were intentionally left out because they contained no
notable changes.

## [0.0.3] - 2014-08-09 09:00
### Added
- "Why should I care?" section mentioning The Changelog podcast.

## [0.0.2] - 2014-07-10 10:00
### Added
- Explanation of the recommended reverse chronological release ordering.

## [0.0.1] - 2014-05-31 11:00
### Added
- This CHANGELOG file to hopefully serve as an evolving example of a
standardized open source project CHANGELOG.
- CNAME file to enable GitHub Pages custom domain
- README now contains answers to common questions about CHANGELOGs
- Good examples and basic guidelines, including proper date formatting.
- Counter-examples: "What makes unicorns cry?"

[unreleased]: https://github.com/vdhicts/keepachangelog-parser/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/vdhicts/keepachangelog-parser/compare/v0.0.1...v1.0.0
[0.0.1]: https://github.com/vdhicts/keepachangelog-parser/releases/tag/v0.0.1

0 comments on commit 10dda1c

Please sign in to comment.