Skip to content

Commit

Permalink
Merge pull request #400 from hydephp/398-remove-legacy-metadata-model
Browse files Browse the repository at this point in the history
Fix #398: Remove the deprecated Metadata model
  • Loading branch information
caendesilva authored May 18, 2022
2 parents d49bb6d + 9aee23c commit ad7ad3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 75 deletions.
42 changes: 14 additions & 28 deletions src/Concerns/GeneratesPageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Models\Metadata;
use Hyde\Framework\Services\AuthorService;
use JetBrains\PhpStorm\ArrayShape;

/**
* Generates metadata for page models that have front matter.
Expand All @@ -16,37 +15,26 @@
*/
trait GeneratesPageMetadata
{
public ?Metadata $metadata = null;
public array $metadata = [];
public array $properties = [];

public function constructMetadata(): void
{
$this->metadata = new Metadata();

$this->parseFrontMatterMetadata();

if ($this instanceof MarkdownPost || $this instanceof \Tests\TestCase) {
$this->makeOpenGraphPropertiesForArticle();
}
}

#[ArrayShape(['name' => "\content"])]
public function getMetadata(): array
{
if (! isset($this->metadata)) {
return [];
}

return $this->metadata->metadata;
return $this->metadata;
}

#[ArrayShape(['property' => 'content'])]
public function getMetaProperties(): array
{
if (! isset($this->metadata)) {
return [];
}

return $this->metadata->properties;
return $this->properties;
}

/**
Expand All @@ -56,15 +44,15 @@ public function getMetaProperties(): array
protected function parseFrontMatterMetadata(): void
{
if (isset($this->matter['description'])) {
$this->metadata->add('description', $this->matter['description']);
$this->metadata['description'] = $this->matter['description'];
}

if (isset($this->matter['author'])) {
$this->metadata->add('author', AuthorService::getAuthorName($this->matter['author']));
$this->metadata['author'] = AuthorService::getAuthorName($this->matter['author']);
}

if (isset($this->matter['category'])) {
$this->metadata->add('keywords', $this->matter['category']);
$this->metadata['keywords'] = $this->matter['category'];
}
}

Expand All @@ -73,30 +61,28 @@ protected function parseFrontMatterMetadata(): void
*/
protected function makeOpenGraphPropertiesForArticle(): void
{
$this->metadata->addProperty('og:type', 'article');

$this->properties['og:type'] = 'article';
if (Hyde::uriPath()) {
$this->metadata->addProperty('og:url', Hyde::uriPath(Hyde::pageLink('posts/'.$this->slug.'.html')));
$this->properties['og:url'] = Hyde::uriPath(Hyde::pageLink('posts/'.$this->slug.'.html'));
}

if (isset($this->matter['title'])) {
$this->metadata->addProperty('og:title', $this->matter['title']);
$this->properties['og:title'] = $this->matter['title'];
}

if (isset($this->matter['date'])) {
$date = date('c', strtotime($this->matter['date']));
$this->metadata->addProperty('og:article:published_time', $date);
$this->properties['og:article:published_time'] = date('c', strtotime($this->matter['date']));
}

if (isset($this->matter['image'])) {
if (is_string($this->matter['image'])) {
$this->metadata->addProperty('og:image', $this->matter['image']);
$this->properties['og:image'] = $this->matter['image'];
} else {
if (isset($this->matter['image']['path'])) {
$this->metadata->addProperty('og:image', $this->matter['image']['path']);
$this->properties['og:image'] = $this->matter['image']['path'];
}
if (isset($this->matter['image']['uri'])) {
$this->metadata->addProperty('og:image', $this->matter['image']['uri']);
$this->properties['og:image'] = $this->matter['image']['uri'];
}
}
}
Expand Down
40 changes: 0 additions & 40 deletions src/Models/Metadata.php

This file was deleted.

7 changes: 0 additions & 7 deletions tests/Feature/Concerns/GeneratesPageMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tests\Feature\Concerns;

use Hyde\Framework\Concerns\GeneratesPageMetadata;
use Hyde\Framework\Models\Metadata;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;

Expand All @@ -27,12 +26,6 @@ protected function tearDown(): void
parent::tearDown();
}

public function test_metadata_object_can_be_constructed()
{
$this->constructMetadata();
$this->assertInstanceOf(Metadata::class, $this->metadata);
}

public function test_get_metadata_returns_empty_array_when_uninitialized()
{
$this->matter = ['description' => 'foo'];
Expand Down

0 comments on commit ad7ad3c

Please sign in to comment.