Skip to content

Commit

Permalink
Add Expression interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 26, 2023
1 parent 6d49c56 commit 883a4a4
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 10 deletions.
1 change: 1 addition & 0 deletions generator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"require": {
"php": ">=8.2",
"ext-mongodb": "*",
"mongodb/mongodb": "@dev",
"nette/php-generator": "^4",
"symfony/console": "^6.3",
"symfony/yaml": "^6.3"
Expand Down
6 changes: 6 additions & 0 deletions generator/config/pipeline-operators.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
- name: and
type: resolvesToBoolExpression
args:
- name: expressions
type: resolvesToExpression
isVariadic: true
- name: eq
type: resolvesToBoolExpression
args:
- name: expression1
type: resolvesToExpression
- name: expression2
type: resolvesToExpression
- name: gt
type: resolvesToBoolExpression
args:
- name: expression1
type: resolvesToExpression
- name: expression2
type: resolvesToExpression
- name: lt
type: resolvesToBoolExpression
args:
- name: expression1
type: resolvesToExpression
- name: expression2
type: resolvesToExpression
- name: ne
type: resolvesToBoolExpression
args:
- name: expression1
type: resolvesToExpression
- name: expression2
type: resolvesToExpression
- name: filter
type: resolvesToArrayExpression
usesNamedArgs: true
args:
- name: input
Expand Down
2 changes: 2 additions & 0 deletions generator/config/query-operators.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
- name: and
type: resolvesToBoolExpression
args:
- name: query
type: resolvesToQueryOperator
isVariadic: true
- name: expr
type: resolvesToExpression
args:
- name: expression
type: resolvesToExpression
3 changes: 0 additions & 3 deletions generator/config/stages.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
---
- name: match
args:
- name: matchExpr
type: resolvesToMatchExpression
isVariadic: true

- name: sort
args:
- name: sortSpecification
type: resolvesToSortSpecification

- name: limit
args:
- name: limit
Expand Down
9 changes: 4 additions & 5 deletions generator/src/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
use function file_put_contents;
use function implode;
use function in_array;
use function interface_exists;
use function is_dir;
use function mkdir;
use function sort;
use function str_starts_with;
use function ucfirst;

/** @internal */
Expand All @@ -32,7 +32,7 @@ abstract class AbstractGenerator
'resolvesToBoolExpression' => [Expression\ResolvesToBoolExpression::class, 'array', 'object', 'string', 'bool'],
'resolvesToMatchExpression' => ['array', 'object', Expression\ResolvesToMatchExpression::class],
'resolvesToNumberExpression' => [Expression\ResolvesToBoolExpression::class, 'array', 'object', 'string', 'int', 'float'],
'resolvesToQueryOperator' => ['array', 'object', Expression\ResolvesToQuery::class],
'resolvesToQueryOperator' => ['array', 'object', Expression\ResolvesToQueryOperator::class],
'resolvesToSortSpecification' => ['array', 'object', Expression\ResolvesToSortSpecification::class],
];

Expand Down Expand Up @@ -72,11 +72,10 @@ final protected function generateTypeString(ArgumentDefinition $arg): array
$docTypes = $nativeTypes;

foreach ($nativeTypes as $key => $typeName) {
// @todo replace with class_exists
if (str_starts_with($typeName, 'MongoDB\\')) {
if (interface_exists($typeName)) {
$nativeTypes[$key] = $docTypes[$key] = '\\' . $typeName;

// A union cannot contain both object and a class type, which is redundant and causes a PHP error
// @todo replace "object" with "stdClass" and force any class object to implement the proper interface
if (in_array('object', $nativeTypes, true)) {
unset($nativeTypes[$key]);
}
Expand Down
1 change: 1 addition & 0 deletions generator/src/Definition/OperatorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public function __construct(
public string $name,
public ?string $type = null,
public bool $usesNamedArgs = false,
array $args = [],
) {
Expand Down
20 changes: 18 additions & 2 deletions generator/src/ValueClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

use MongoDB\CodeGenerator\Definition\OperatorDefinition;
use Nette\PhpGenerator\ClassType;
use RuntimeException;

use function assert;

use const PHP_EOL;
use function interface_exists;
use function ucfirst;

/**
* Generates a value object class for stages and operators.
Expand All @@ -20,6 +21,7 @@ public function createClassForObject(object $object): ClassType
assert($object instanceof OperatorDefinition);

$class = new ClassType($this->getClassName($object));
$class->setImplements($this->getInterfaces($object));

$constuctor = $class->addMethod('__construct');

Expand Down Expand Up @@ -51,4 +53,18 @@ public function createClassForObject(object $object): ClassType

return $class;
}

private function getInterfaces(OperatorDefinition $definition): array
{
if ($definition->type === null) {
return [];
}

$interface = 'MongoDB\\Builder\\Expression\\' . ucfirst($definition->type);
if (! interface_exists($interface)) {
throw new RuntimeException('Interface ' . $interface . ' does not exist');
}

return [$interface];
}
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToArrayExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToArrayExpression
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object', 'string'];
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToBoolExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToBoolExpression
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object', 'string', 'bool'];
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToExpression
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object', 'string', 'int', 'float', 'bool', 'null'];
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToMatchExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToMatchExpression
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object'];
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToNumberExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToNumberExpression
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object', 'string', 'int', 'float'];
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToQueryOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToQueryOperator
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object'];
}
8 changes: 8 additions & 0 deletions src/Builder/Expression/ResolvesToSortSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MongoDB\Builder\Expression;

interface ResolvesToSortSpecification
{
public const ACCEPTED_TYPES = [self::class, 'array', 'object'];
}

0 comments on commit 883a4a4

Please sign in to comment.