Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper to ease creating an empty result #497

Merged
merged 3 commits into from
Feb 28, 2025
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
9 changes: 8 additions & 1 deletion packages/seal/src/Search/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @extends \IteratorIterator<int, array<string, mixed>, \Generator>
*/
class Result extends \IteratorIterator
final class Result extends \IteratorIterator
{
/**
* @param \Generator<int, array<string, mixed>> $documents
Expand All @@ -32,4 +32,11 @@ public function total(): int
{
return $this->total;
}

public static function createEmpty(): static
{
return new self((static function (): \Generator {
yield from [];
})(), 0);
}
}
37 changes: 37 additions & 0 deletions packages/seal/tests/Search/ResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CMS-IG SEAL project.
*
* (c) Alexander Schranz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CmsIg\Seal\Tests\Search;

use CmsIg\Seal\Search\Result;
use PHPUnit\Framework\TestCase;

class ResultTest extends TestCase
{
public function testIteratingResult(): void
{
$result = new Result((static function (): \Generator {
yield ['id' => 42];
})(), 1);

$this->assertSame(1, $result->total());
$this->assertSame([['id' => 42]], \iterator_to_array($result));
}

public function testCreateEmptyResult(): void
{
$result = Result::createEmpty();
$this->assertSame(0, $result->total());
$this->assertSame([], \iterator_to_array($result));
}
}
Loading