Skip to content

Commit 58bb16b

Browse files
authored
make unresolved content types in entries resolve (#35)
1 parent cbce2b8 commit 58bb16b

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Entry.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class Entry implements EntryInterface
99
{
1010
use DisallowArrayAccessMutationTrait;
1111
use EntryUnknownMethodTrait;
12-
use MetadataAccessTrait;
12+
use MetadataAccessTrait {
13+
getContentType as protected getMetadataContentType;
14+
}
1315

1416
/**
1517
* @var array
@@ -94,6 +96,19 @@ public function getField($key)
9496
return $this->fields[$key];
9597
}
9698

99+
public function getContentType()
100+
{
101+
$metadataContentType = $this->getMetadataContentType();
102+
if (!$metadataContentType instanceof Link) {
103+
return $metadataContentType;
104+
}
105+
if (null === $this->resolveLinkFunction) {
106+
return null;
107+
}
108+
109+
return call_user_func($this->resolveLinkFunction, $metadataContentType)->wait();
110+
}
111+
97112
/**
98113
* @param mixed $offset
99114
* @return bool true on success or false on failure

tests/EntryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GuzzleHttp\Promise\Promise;
66
use function GuzzleHttp\Promise\promise_for;
77
use Markup\Contentful\AssetInterface;
8+
use Markup\Contentful\ContentTypeInterface;
89
use Markup\Contentful\Entry;
910
use Markup\Contentful\EntryInterface;
1011
use Markup\Contentful\Exception\LinkUnresolvableException;
@@ -115,4 +116,29 @@ public function testUnresolvedSingleLinkEmitsNull()
115116
$entry->setResolveLinkFunction($callback);
116117
$this->assertNull($entry['asset']);
117118
}
119+
120+
public function testGetResolvedContentType()
121+
{
122+
$contentType = m::mock(ContentTypeInterface::class);
123+
$this->metadata
124+
->shouldReceive('getContentType')
125+
->andReturn($contentType);
126+
$this->assertSame($contentType, $this->entry->getContentType());
127+
}
128+
129+
public function testGetUnresolvedContentType()
130+
{
131+
$link = m::mock(Link::class);
132+
$this->metadata
133+
->shouldReceive('getContentType')
134+
->andReturn($link);
135+
$contentType = m::mock(ContentTypeInterface::class);
136+
$callback = function ($link) use ($contentType) {
137+
$this->assertInstanceOf(Link::class, $link);
138+
139+
return promise_for($contentType);
140+
};
141+
$this->entry->setResolveLinkFunction($callback);
142+
$this->assertSame($contentType, $this->entry->getContentType());
143+
}
118144
}

0 commit comments

Comments
 (0)