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

Commit 2e725e1

Browse files
committed
PHPLIB-1381 Add dictionnary for time units
1 parent 592b0a0 commit 2e725e1

31 files changed

+167
-74
lines changed

generator/config/accumulator/derivative.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ arguments:
1616
-
1717
name: unit
1818
type:
19-
- string
19+
- timeUnit
2020
optional: true
2121
description: |
2222
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".

generator/config/accumulator/integral.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ arguments:
1616
-
1717
name: unit
1818
type:
19-
- resolvesToString
19+
- timeUnit
2020
optional: true
2121
description: |
2222
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".

generator/config/expression/dateAdd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ arguments:
1818
-
1919
name: unit
2020
type:
21-
- resolvesToString
21+
- timeUnit
2222
description: |
2323
The unit used to measure the amount of time added to the startDate.
2424
-

generator/config/expression/dateDiff.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ arguments:
2626
-
2727
name: unit
2828
type:
29-
- resolvesToString
29+
- timeUnit
3030
description: |
3131
The time measurement unit between the startDate and endDate
3232
-

generator/config/expression/dateSubtract.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ arguments:
1818
-
1919
name: unit
2020
type:
21-
- resolvesToString
21+
- timeUnit
2222
description: |
2323
The unit used to measure the amount of time added to the startDate.
2424
-

generator/config/expression/dateTrunc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ arguments:
1818
-
1919
name: unit
2020
type:
21-
- resolvesToString
21+
- timeUnit
2222
description: |
2323
The unit of time, specified as an expression that must resolve to one of these strings: year, quarter, week, month, day, hour, minute, second.
2424
Together, binSize and unit specify the time period used in the $dateTrunc calculation.

generator/config/expressions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113
'returnType' => Type\GeometryInterface::class,
114114
'acceptedTypes' => [Type\GeometryInterface::class, ...$bsonTypes['object']],
115115
],
116+
'timeUnit' => [
117+
'returnType' => Type\TimeUnit::class,
118+
'acceptedTypes' => [Type\TimeUnit::class, ResolvesToString::class, ...$bsonTypes['string']],
119+
],
116120

117121
// @todo add enum values
118122
'Granularity' => [

generator/config/schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"expression",
114114
"geometry",
115115
"fieldPath",
116+
"timeUnit",
116117
"any",
117118
"resolvesToNumber", "numberFieldPath", "number",
118119
"resolvesToDouble", "doubleFieldPath", "double",

src/Builder/Accumulator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\BSON\Serializable;
99
use MongoDB\Builder\Type\Optional;
1010
use MongoDB\Builder\Type\OutputWindow;
11+
use MongoDB\Builder\Type\TimeUnit;
1112
use MongoDB\Builder\Type\WindowInterface;
1213
use stdClass;
1314

@@ -31,7 +32,7 @@ public static function outputWindow(
3132
Document|Serializable|WindowInterface|stdClass|array $operator,
3233
Optional|array $documents = Optional::Undefined,
3334
Optional|array $range = Optional::Undefined,
34-
Optional|string $unit = Optional::Undefined,
35+
Optional|TimeUnit|string $unit = Optional::Undefined,
3536
): OutputWindow {
3637
return new OutputWindow($operator, $documents, $range, $unit);
3738
}

src/Builder/Accumulator/DerivativeAccumulator.php

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/FactoryTrait.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/IntegralAccumulator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/BuilderEncoder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MongoDB\Builder;
66

77
use MongoDB\Builder\Encoder\CombinedFieldQueryEncoder;
8+
use MongoDB\Builder\Encoder\DictionaryEncoder;
89
use MongoDB\Builder\Encoder\ExpressionEncoder;
910
use MongoDB\Builder\Encoder\FieldPathEncoder;
1011
use MongoDB\Builder\Encoder\OperatorEncoder;
@@ -14,6 +15,7 @@
1415
use MongoDB\Builder\Encoder\VariableEncoder;
1516
use MongoDB\Builder\Expression\Variable;
1617
use MongoDB\Builder\Type\CombinedFieldQuery;
18+
use MongoDB\Builder\Type\Dictionnary;
1719
use MongoDB\Builder\Type\ExpressionInterface;
1820
use MongoDB\Builder\Type\FieldPathInterface;
1921
use MongoDB\Builder\Type\OperatorInterface;
@@ -39,6 +41,7 @@ class BuilderEncoder implements Encoder
3941
private array $defaultEncoders = [
4042
Pipeline::class => PipelineEncoder::class,
4143
Variable::class => VariableEncoder::class,
44+
Dictionnary::class => DictionaryEncoder::class,
4245
FieldPathInterface::class => FieldPathEncoder::class,
4346
CombinedFieldQuery::class => CombinedFieldQueryEncoder::class,
4447
QueryObject::class => QueryEncoder::class,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Builder\Encoder;
6+
7+
use MongoDB\Builder\Type\Dictionnary;
8+
use MongoDB\Codec\EncodeIfSupported;
9+
use MongoDB\Exception\UnsupportedValueException;
10+
use stdClass;
11+
12+
/** @template-extends AbstractExpressionEncoder<string, Dictionnary> */
13+
class DictionaryEncoder extends AbstractExpressionEncoder
14+
{
15+
/** @template-use EncodeIfSupported<string, Dictionnary> */
16+
use EncodeIfSupported;
17+
18+
public function canEncode(mixed $value): bool
19+
{
20+
return $value instanceof Dictionnary;
21+
}
22+
23+
public function encode(mixed $value): string|int|array|stdClass
24+
{
25+
if (! $this->canEncode($value)) {
26+
throw UnsupportedValueException::invalidEncodableValue($value);
27+
}
28+
29+
return $value->getValue();
30+
}
31+
}

src/Builder/Expression/DateAddOperator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/DateDiffOperator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/DateSubtractOperator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)