Skip to content

Commit

Permalink
Fix SlugWithDateParser & SlugWithOrderParser
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed May 31, 2018
1 parent a2b9fa1 commit be7866c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/PathParsers/SlugParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class SlugParser implements PathParser
{
public function parse(string $path): array
{
return ['slug' => pathinfo($path, PATHINFO_FILENAME)];
return ['slug' => explode('.', $path)[0]];
}
}
8 changes: 6 additions & 2 deletions src/PathParsers/SlugWithDateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ class SlugWithDateParser implements PathParser
{
public function parse(string $path): array
{
[$date, $slug] = explode('.', $path);
$parts = explode('/', $path);

$filename = array_pop($parts);

[$date, $slug] = explode('.', $filename);

return [
'date' => Carbon::parse($date),
'slug' => $slug ?? '',
'slug' => implode('/', array_merge($parts, [$slug])),
];
}
}
10 changes: 7 additions & 3 deletions src/PathParsers/SlugWithOrderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ class SlugWithOrderParser implements PathParser
{
public function parse(string $path): array
{
[$order, $slug] = explode('.', $path);
$parts = explode('/', $path);

$filename = array_pop($parts);

[$order, $slug] = explode('.', $filename);

return [
'order' => (int) $order,
'slug' => $slug ?? '',
'order' => $order,
'slug' => implode('/', array_merge($parts, [$slug])),
];
}
}
8 changes: 8 additions & 0 deletions tests/PathParsers/SlugParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public function it_extracts_a_slug_attribute_from_a_path()

$this->assertEquals(['slug' => 'hello-world'], $slugParser->parse('hello-world.md'));
}

/** @test */
public function it_extracts_a_slug_attribute_from_a_nested_path()
{
$slugParser = new SlugParser();

$this->assertEquals(['slug' => 'pages/hello-world'], $slugParser->parse('pages/hello-world.md'));
}
}
13 changes: 13 additions & 0 deletions tests/PathParsers/SlugWithDateParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ public function it_extracts_a_date_and_slug_attribute_from_a_path()

$this->assertEquals($expected, $slugWithDateParser->parse('1992-02-01.hello-world.md'));
}

/** @test */
public function it_extracts_a_date_and_slug_attribute_from_a_nested_path()
{
$slugWithDateParser = new SlugWithDateParser();

$expected = [
'date' => Carbon::parse('1992-02-01'),
'slug' => 'pages/hello-world',
];

$this->assertEquals($expected, $slugWithDateParser->parse('pages/1992-02-01.hello-world.md'));
}
}
13 changes: 13 additions & 0 deletions tests/PathParsers/SlugWithOrderParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ public function it_extracts_an_order_and_slug_attribute_from_a_path()

$this->assertEquals($expected, $slugWithOrderParser->parse('1.hello-world.md'));
}

/** @test */
public function it_extracts_an_order_and_slug_attribute_from_a_nested_path()
{
$slugWithOrderParser = new SlugWithOrderParser();

$expected = [
'order' => 1,
'slug' => 'pages/hello-world',
];

$this->assertEquals($expected, $slugWithOrderParser->parse('pages/1.hello-world.md'));
}
}

0 comments on commit be7866c

Please sign in to comment.