Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Promise/ResolvedResourceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Markup\Contentful\Promise;

use function GuzzleHttp\Promise\is_fulfilled;
use GuzzleHttp\Promise\PromiseInterface;
use Markup\Contentful\ResourceArray;
use Markup\Contentful\ResourceInterface;

Expand All @@ -21,7 +22,17 @@ private function ensureResolved()
if (is_fulfilled($promise) && null !== $this->resolvedResource) {
return;
}
$this->resolvedResource = $promise->wait();
$this->doResolve($promise);
}

protected function doResolve(PromiseInterface $promise)
{
$this->setResolvedResource($promise->wait());
}

protected function setResolvedResource($resource)
{
$this->resolvedResource = $resource;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Promise/ResourceArrayPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,19 @@ protected function addRejectionHandlerToPromise(PromiseInterface $promise)
return new ResourceArray([], 0, 0, 0);
});
}

protected function doResolve(PromiseInterface $promise)
{
$resolved = $promise->wait();
//temporarily set resolved resource with array that may contain nulls
$this->setResolvedResource($resolved);
//now set it again but with access to skip/limit parameters etc - using a resource array will auto-filter nulls
$this->setResolvedResource(new ResourceArray(
$resolved,
$this->getTotal(),
$this->getSkip(),
$this->getLimit(),
$this->getEnvelope()
));
}
}
17 changes: 15 additions & 2 deletions src/ResourceArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function __construct($items, $total, $skip, $limit, ResourceEnvelope $env
return $resource instanceof ResourceInterface;
};
if ($items instanceof \Traversable) {
$this->items = array_filter(array_values(iterator_to_array($items)), $filterNonResource);
$this->items = array_values(array_filter(array_values(iterator_to_array($items)), $filterNonResource));
} elseif (is_array($items)) {
$this->items = array_filter(array_values($items), $filterNonResource);
$this->items = array_values(array_filter(array_values($items), $filterNonResource));
} else {
throw new \InvalidArgumentException('Items parameter should be an array or a traversable object.');
}
Expand Down Expand Up @@ -157,4 +157,17 @@ public function offsetGet($offset)

return $this->items[$offset];
}

/**
* Ensures that this resource array strips out missing resources if they don't exist, so that counts and members
* always reflect resources only.
*
* This will resolve a resource array if it is a promise/ future.
*
* @return void
*/
public function ensureNoMissing()
{
// constructor already ensures no missing, so no action necessary here
}
}
17 changes: 16 additions & 1 deletion tests/Promise/ResourceArrayPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public function testIsPromise()
public function testGetEnvelope()
{
$envelope = m::mock(ResourceEnvelope::class);
$inner = m::mock(ResourceArrayInterface::class)
$inner = m::spy(ResourceArrayInterface::class)
->shouldReceive('getEnvelope')
->andReturn($envelope)
->shouldReceive('getIterator')
->andReturn(new \ArrayIterator())
->getMock();
$resourceArray = new ResourceArrayPromise(promise_for($inner));
$this->assertSame($envelope, $resourceArray->getEnvelope());
Expand All @@ -56,4 +58,17 @@ public function testGettersForArray()
$this->assertInstanceOf(EntryInterface::class, $resourceArray->first());
$this->assertInstanceOf(AssetInterface::class, $resourceArray->last());
}

public function testFiltersEmpty()
{
$resourceArray = new ResourceArrayPromise(promise_for([
m::mock(EntryInterface::class),
null,
m::mock(AssetInterface::class),
]));
$this->assertCount(2, $resourceArray);
$this->assertInstanceOf(EntryInterface::class, $resourceArray->first());
$this->assertInstanceOf(AssetInterface::class, $resourceArray->last());
$this->assertInstanceOf(AssetInterface::class, $resourceArray[1]);
}
}
3 changes: 2 additions & 1 deletion tests/ResourceArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Markup\Contentful\Tests;

use Markup\Contentful\EntryInterface;
use Markup\Contentful\ResourceArray;
use Markup\Contentful\ResourceArrayInterface;
use Mockery as m;
Expand Down Expand Up @@ -85,6 +86,6 @@ public function testLastWhenArrayEmptyReturnsNull()

private function getMockEntry()
{
return m::mock('Markup\Contentful\EntryInterface');
return m::mock(EntryInterface::class);
}
}