Skip to content

Commit

Permalink
Introduce MongoDB\object() function
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 5, 2023
1 parent 9a36d17 commit c0de34d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Psr\Log\LoggerInterface;
use ReflectionClass;
use ReflectionException;
use stdClass;

use function array_is_list;
use function array_key_first;
Expand All @@ -58,6 +59,17 @@ function add_logger(LoggerInterface $logger): void
PsrLogAdapter::addLogger($logger);
}

/**
* Create a new stdClass instance with the provided properties.
*
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*/
function object(mixed ...$values): stdClass
{
return (object) $values;
}

/**
* Unregisters a PSR-3 logger.
*
Expand Down
16 changes: 8 additions & 8 deletions tests/Builder/BuilderEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function array_merge;
use function array_walk;
use function is_array;
use function MongoDB\object;
use function var_export;

/**
Expand All @@ -27,8 +28,7 @@ class BuilderEncoderTest extends TestCase
public function testPipeline(): void
{
$pipeline = new Pipeline(
// @todo array is accepted by the stage class, but we expect an object. The driver accepts both.
Stage::match((object) ['author' => 'dave']),
Stage::match(object(author: 'dave')),
Stage::limit(1),
);

Expand All @@ -44,7 +44,7 @@ public function testPipeline(): void
public function testSort(): void
{
$pipeline = new Pipeline(
Stage::sort((object) ['age' => -1, 'posts' => 1]),
Stage::sort(object(age: -1, posts: 1)),
);

$expected = [
Expand All @@ -60,8 +60,8 @@ public function testPerformCount(): void
$pipeline = new Pipeline(
Stage::match(
Query::or(
(object) ['score' => [Query::gt(70), Query::lt(90)]],
(object) ['views' => Query::gte(1000)],
object(score: [Query::gt(70), Query::lt(90)]),
object(views: Query::gte(1000)),
),
),
Stage::group(
Expand Down Expand Up @@ -99,14 +99,14 @@ public function testPerformCount(): void
public function testAggregationFilter(array $limit, array $expectedLimit): void
{
$pipeline = new Pipeline(
Stage::project(...[
'items' => Aggregation::filter(
Stage::project(
items: Aggregation::filter(
Expression::arrayFieldPath('items'),
Aggregation::gte(Expression::variable('item.price'), 100),
'item',
...$limit,
),
]),
),
);

$expected = [
Expand Down

0 comments on commit c0de34d

Please sign in to comment.