Skip to content

Commit

Permalink
Adding tags to pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Nov 24, 2017
1 parent 4b23ab7 commit 063b5a5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/Loaders/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ class Page
* @var array
*/
private $context;
/**
* @var string[]
*/
private $tags;

/**
* @param string[]|null $menu
* @param mixed[] $context
* @param string[] $tags
*/
public function __construct(?string $id, string $title, string $content, string $url, string $lang, ?string $website, ?array $menu, ?int $menuOrder, ?string $menuCssClass, ?string $metaTitle, ?string $metaDescription, ?string $theme, ?string $template, array $context = [])
public function __construct(?string $id, string $title, string $content, string $url, string $lang, ?string $website, ?array $menu, ?int $menuOrder, ?string $menuCssClass, ?string $metaTitle, ?string $metaDescription, ?string $theme, ?string $template, array $context = [], array $tags = [])
{
$this->id = $id;
$this->title = $title;
Expand All @@ -83,6 +88,7 @@ public function __construct(?string $id, string $title, string $content, string
$this->theme = $theme;
$this->template = $template;
$this->context = $context;
$this->tags = $tags;
}

public static function fromFile(string $file): self
Expand Down Expand Up @@ -137,7 +143,8 @@ public static function fromFile(string $file): self
$yaml['meta_description'] ?? null,
$yaml['theme'] ?? null,
$yaml['template'] ?? null,
$yaml['context'] ?? []
$yaml['context'] ?? [],
$yaml['tags'] ?? []
);
}

Expand Down Expand Up @@ -178,6 +185,7 @@ private static function mergeYaml(array $baseYaml, array $yaml, string $file): a
'theme' => YamlUtils::OVERRIDE,
'template' => YamlUtils::OVERRIDE,
'context' => YamlUtils::MERGE_ARRAY,
'tags' => YamlUtils::MERGE_ARRAY,
]);
}

Expand Down Expand Up @@ -292,4 +300,12 @@ public function getContext(): array
{
return $this->context;
}

/**
* @return string[]
*/
public function getTags(): array
{
return $this->tags;
}
}
6 changes: 5 additions & 1 deletion src/Loaders/YamlUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public function mergeArrays(array $baseYaml, array $yaml, array $instructions):
break;
case self::MERGE_ARRAY:
if (isset($baseYaml[$key])) {
$yaml[$key] = $this->recursiveMerge($baseYaml[$key], $yaml[$key]);
if (isset($yaml[$key])) {
$yaml[$key] = $this->recursiveMerge($baseYaml[$key], $yaml[$key]);
} else {
$yaml[$key] = $baseYaml[$key];
}
}
break;
default:
Expand Down
1 change: 1 addition & 0 deletions tests/Loaders/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testInherits()
$this->assertSame('fr', $page->getLang());
$this->assertSame('foo_theme', $page->getTheme());
$this->assertSame('/foo/bar/baz', $page->getUrl());
$this->assertSame(['blog'], $page->getTags());
}

public function testInvalidInheritance()
Expand Down
5 changes: 4 additions & 1 deletion tests/Loaders/YamlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function testMergeArrays()
'foobar' => [
'one' => 'two'
]
]
],
'tags' => ['blog']
], [
'title' => 'bar',
'lang' => 'fr',
Expand All @@ -39,6 +40,7 @@ public function testMergeArrays()
'theme' => YamlUtils::OVERRIDE,
'template' => YamlUtils::OVERRIDE,
'context' => YamlUtils::MERGE_ARRAY,
'tags' => YamlUtils::MERGE_ARRAY,
]);

$this->assertSame([
Expand All @@ -53,6 +55,7 @@ public function testMergeArrays()
]
],
'website' => 'example.com',
'tags' => ['blog']
], $result);
}
}
2 changes: 2 additions & 0 deletions tests/fixtures/Loaders/inherited_pages/parent.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
theme : foo_theme
tags:
- blog

0 comments on commit 063b5a5

Please sign in to comment.