Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

PHPLIB-1381 PHPLIB-1269 Add enum for Sort and TimeUnit #63

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion generator/config/accumulator/derivative.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ arguments:
-
name: unit
type:
- string
- timeUnit
optional: true
description: |
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".
Expand Down
2 changes: 1 addition & 1 deletion generator/config/accumulator/integral.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ arguments:
-
name: unit
type:
- resolvesToString
- timeUnit
optional: true
description: |
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".
Expand Down
2 changes: 1 addition & 1 deletion generator/config/expression/dateAdd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ arguments:
-
name: unit
type:
- resolvesToString
- timeUnit
description: |
The unit used to measure the amount of time added to the startDate.
-
Expand Down
2 changes: 1 addition & 1 deletion generator/config/expression/dateDiff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ arguments:
-
name: unit
type:
- resolvesToString
- timeUnit
description: |
The time measurement unit between the startDate and endDate
-
Expand Down
2 changes: 1 addition & 1 deletion generator/config/expression/dateSubtract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ arguments:
-
name: unit
type:
- resolvesToString
- timeUnit
description: |
The unit used to measure the amount of time added to the startDate.
-
Expand Down
2 changes: 1 addition & 1 deletion generator/config/expression/dateTrunc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ arguments:
-
name: unit
type:
- resolvesToString
- timeUnit
description: |
The unit of time, specified as an expression that must resolve to one of these strings: year, quarter, week, month, day, hour, minute, second.
Together, binSize and unit specify the time period used in the $dateTrunc calculation.
Expand Down
1 change: 1 addition & 0 deletions generator/config/expression/sortArray.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ arguments:
type:
- object # SortSpec
- int
- sortSpec
description: |
The document specifies a sort ordering.
tests:
Expand Down
8 changes: 8 additions & 0 deletions generator/config/expressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@
'returnType' => Type\SwitchBranchInterface::class,
'acceptedTypes' => [Type\SwitchBranchInterface::class, ...$bsonTypes['object']],
],
'timeUnit' => [
'returnType' => Type\TimeUnit::class,
'acceptedTypes' => [Type\TimeUnit::class, ResolvesToString::class, ...$bsonTypes['string']],
],
'sortSpec' => [
'returnType' => Type\Sort::class,
'acceptedTypes' => [Type\Sort::class],
],

// @todo add enum values
'Granularity' => [
Expand Down
2 changes: 2 additions & 0 deletions generator/config/query/text.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ tests:
$text:
$search: 'CAFÉ'
$diacriticSensitive: true
-
$project:
score:
$meta: 'textScore'
-
Expand Down
2 changes: 2 additions & 0 deletions generator/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
"expression",
"geometry",
"fieldPath",
"timeUnit",
"sortSpec",
"any",
"resolvesToNumber", "numberFieldPath", "number",
"resolvesToDouble", "doubleFieldPath", "double",
Expand Down
4 changes: 3 additions & 1 deletion generator/config/stage/sort.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ arguments:
-
name: sort
type:
- object # SortSpec
- expression
- sortSpec
variadic: object
tests:
-
name: 'Ascending Descending Sort'
Expand Down
10 changes: 9 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4">
<files psalm-version="5.21.1@8c473e2437be8b6a8fd8f630f0f11a16b114c494">
<file src="src/Builder/Encoder/AbstractExpressionEncoder.php">
<MixedAssignment>
<code>$val</code>
Expand Down Expand Up @@ -113,6 +113,14 @@
<code>stdClass</code>
</TooManyTemplateParams>
</file>
<file src="src/Builder/Stage/SortStage.php">
<PropertyTypeCoercion>
<code>$sort</code>
</PropertyTypeCoercion>
<TooManyTemplateParams>
<code>stdClass</code>
</TooManyTemplateParams>
</file>
<file src="src/Builder/Type/OutputWindow.php">
<DocblockTypeContradiction>
<code><![CDATA[! is_string($documents[1]) && ! is_int($documents[1])]]></code>
Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Accumulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\BSON\Serializable;
use MongoDB\Builder\Type\Optional;
use MongoDB\Builder\Type\OutputWindow;
use MongoDB\Builder\Type\TimeUnit;
use MongoDB\Builder\Type\WindowInterface;
use stdClass;

Expand All @@ -31,7 +32,7 @@ public static function outputWindow(
Document|Serializable|WindowInterface|stdClass|array $operator,
Optional|array $documents = Optional::Undefined,
Optional|array $range = Optional::Undefined,
Optional|string $unit = Optional::Undefined,
Optional|TimeUnit|string $unit = Optional::Undefined,
): OutputWindow {
return new OutputWindow($operator, $documents, $range, $unit);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Builder/Accumulator/DerivativeAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/Builder/Accumulator/FactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/Builder/Accumulator/IntegralAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/Builder/BuilderEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MongoDB\Builder;

use MongoDB\Builder\Encoder\CombinedFieldQueryEncoder;
use MongoDB\Builder\Encoder\DictionaryEncoder;
use MongoDB\Builder\Encoder\ExpressionEncoder;
use MongoDB\Builder\Encoder\FieldPathEncoder;
use MongoDB\Builder\Encoder\OperatorEncoder;
Expand All @@ -14,6 +15,7 @@
use MongoDB\Builder\Encoder\VariableEncoder;
use MongoDB\Builder\Expression\Variable;
use MongoDB\Builder\Type\CombinedFieldQuery;
use MongoDB\Builder\Type\DictionaryInterface;
use MongoDB\Builder\Type\ExpressionInterface;
use MongoDB\Builder\Type\FieldPathInterface;
use MongoDB\Builder\Type\OperatorInterface;
Expand All @@ -29,16 +31,17 @@
use function array_key_exists;
use function is_object;

/** @template-implements Encoder<stdClass|array|string, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
/** @template-implements Encoder<stdClass|array|string|int, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
class BuilderEncoder implements Encoder
{
/** @template-use EncodeIfSupported<stdClass|array|string, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
/** @template-use EncodeIfSupported<stdClass|array|string|int, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
use EncodeIfSupported;

/** @var array<class-string, class-string<ExpressionEncoder>> */
private array $defaultEncoders = [
Pipeline::class => PipelineEncoder::class,
Variable::class => VariableEncoder::class,
DictionaryInterface::class => DictionaryEncoder::class,
FieldPathInterface::class => FieldPathEncoder::class,
CombinedFieldQuery::class => CombinedFieldQueryEncoder::class,
QueryObject::class => QueryEncoder::class,
Expand All @@ -64,7 +67,7 @@ public function canEncode(mixed $value): bool
return (bool) $this->getEncoderFor($value)?->canEncode($value);
}

public function encode(mixed $value): stdClass|array|string
public function encode(mixed $value): stdClass|array|string|int
{
$encoder = $this->getEncoderFor($value);

Expand Down
2 changes: 1 addition & 1 deletion src/Builder/Encoder/AbstractExpressionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function is_array;

/**
* @template BSONType of stdClass|array|string
* @template BSONType of stdClass|array|string|int
* @template NativeType
* @template-implements ExpressionEncoder<BSONType, NativeType>
*/
Expand Down
31 changes: 31 additions & 0 deletions src/Builder/Encoder/DictionaryEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace MongoDB\Builder\Encoder;

use MongoDB\Builder\Type\DictionaryInterface;
use MongoDB\Codec\EncodeIfSupported;
use MongoDB\Exception\UnsupportedValueException;
use stdClass;

/** @template-extends AbstractExpressionEncoder<string|int|array|stdClass, DictionaryInterface> */
class DictionaryEncoder extends AbstractExpressionEncoder
{
/** @template-use EncodeIfSupported<string|int|array|stdClass, DictionaryInterface> */
use EncodeIfSupported;

public function canEncode(mixed $value): bool
{
return $value instanceof DictionaryInterface;
}

public function encode(mixed $value): string|int|array|stdClass
{
if (! $this->canEncode($value)) {
throw UnsupportedValueException::invalidEncodableValue($value);
}

return $value->getValue();
}
}
2 changes: 1 addition & 1 deletion src/Builder/Encoder/ExpressionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use stdClass;

/**
* @template BSONType of stdClass|array|string
* @template BSONType of stdClass|array|string|int
* @template NativeType
* @template-extends Encoder<BSONType, NativeType>
*/
Expand Down
9 changes: 5 additions & 4 deletions src/Builder/Expression/DateAddOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading