From 858cf6dfd5dfb5919746b000874cc847fb9fefe6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 8 Aug 2022 16:01:28 +0200 Subject: [PATCH] Fix #370, add toString magic method to Image model --- packages/framework/src/Models/Image.php | 8 +++++++- .../tests/Feature/ImageModelTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Models/Image.php b/packages/framework/src/Models/Image.php index 8286749585c..20033f28029 100644 --- a/packages/framework/src/Models/Image.php +++ b/packages/framework/src/Models/Image.php @@ -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)). @@ -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 { diff --git a/packages/framework/tests/Feature/ImageModelTest.php b/packages/framework/tests/Feature/ImageModelTest.php index 885ccd6401f..9c3dc962c95 100644 --- a/packages/framework/tests/Feature/ImageModelTest.php +++ b/packages/framework/tests/Feature/ImageModelTest.php @@ -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', + ]))); + } }