Skip to content

Commit

Permalink
Fix #370, add toString magic method to Image model
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Aug 8, 2022
1 parent 1d60e0a commit 858cf6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/framework/src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* 'credit' => '?string'
* ];
*/
class Image
class Image implements \Stringable
{
/**
* The image's path (if it is stored locally (in the _media directory)).
Expand Down Expand Up @@ -108,6 +108,12 @@ public function __construct(array $data = [])
}
}

/** @inheritDoc */
public function __toString()
{
return $this->getLink();
}

/** Dynamically create an image based on string or front matter array */
public static function make(string|array $data): static
{
Expand Down
19 changes: 19 additions & 0 deletions packages/framework/tests/Feature/ImageModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,23 @@ public function test_local_path_is_normalized_to_the_media_directory()
'path' => 'media/image.jpg',
]))->path);
}

public function test_to_string_returns_the_image_source()
{
$this->assertEquals('https://example.com/image.jpg', (string) (new Image([
'uri' => 'https://example.com/image.jpg',
])));

$this->assertEquals('media/image.jpg', (string) (new Image([
'path' => 'image.jpg',
])));
}

public function test_to_string_returns_the_image_source_for_nested_pages()
{
$this->mockCurrentPage('foo/bar');
$this->assertEquals('../media/image.jpg', (string) (new Image([
'path' => 'image.jpg',
])));
}
}

0 comments on commit 858cf6d

Please sign in to comment.