Skip to content

Commit 63dd036

Browse files
authored
PHPLIB-1381 PHPLIB-1269 Add enum for Sort and TimeUnit (#63)
1 parent ceac70d commit 63dd036

File tree

84 files changed

+400
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+400
-238
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/expression/sortArray.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ arguments:
2020
type:
2121
- object # SortSpec
2222
- int
23+
- sortSpec
2324
description: |
2425
The document specifies a sort ordering.
2526
tests:

generator/config/expressions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@
117117
'returnType' => Type\SwitchBranchInterface::class,
118118
'acceptedTypes' => [Type\SwitchBranchInterface::class, ...$bsonTypes['object']],
119119
],
120+
'timeUnit' => [
121+
'returnType' => Type\TimeUnit::class,
122+
'acceptedTypes' => [Type\TimeUnit::class, ResolvesToString::class, ...$bsonTypes['string']],
123+
],
124+
'sortSpec' => [
125+
'returnType' => Type\Sort::class,
126+
'acceptedTypes' => [Type\Sort::class],
127+
],
120128

121129
// @todo add enum values
122130
'Granularity' => [

generator/config/query/text.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ tests:
102102
$text:
103103
$search: 'CAFÉ'
104104
$diacriticSensitive: true
105+
-
106+
$project:
105107
score:
106108
$meta: 'textScore'
107109
-

generator/config/schema.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
"expression",
116116
"geometry",
117117
"fieldPath",
118+
"timeUnit",
119+
"sortSpec",
118120
"any",
119121
"resolvesToNumber", "numberFieldPath", "number",
120122
"resolvesToDouble", "doubleFieldPath", "double",

generator/config/stage/sort.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ arguments:
1010
-
1111
name: sort
1212
type:
13-
- object # SortSpec
13+
- expression
14+
- sortSpec
15+
variadic: object
1416
tests:
1517
-
1618
name: 'Ascending Descending Sort'

psalm-baseline.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4">
2+
<files psalm-version="5.21.1@8c473e2437be8b6a8fd8f630f0f11a16b114c494">
33
<file src="src/Builder/Encoder/AbstractExpressionEncoder.php">
44
<MixedAssignment>
55
<code>$val</code>
@@ -113,6 +113,14 @@
113113
<code>stdClass</code>
114114
</TooManyTemplateParams>
115115
</file>
116+
<file src="src/Builder/Stage/SortStage.php">
117+
<PropertyTypeCoercion>
118+
<code>$sort</code>
119+
</PropertyTypeCoercion>
120+
<TooManyTemplateParams>
121+
<code>stdClass</code>
122+
</TooManyTemplateParams>
123+
</file>
116124
<file src="src/Builder/Type/OutputWindow.php">
117125
<DocblockTypeContradiction>
118126
<code><![CDATA[! is_string($documents[1]) && ! is_int($documents[1])]]></code>

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: 6 additions & 3 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\DictionaryInterface;
1719
use MongoDB\Builder\Type\ExpressionInterface;
1820
use MongoDB\Builder\Type\FieldPathInterface;
1921
use MongoDB\Builder\Type\OperatorInterface;
@@ -29,16 +31,17 @@
2931
use function array_key_exists;
3032
use function is_object;
3133

32-
/** @template-implements Encoder<stdClass|array|string, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
34+
/** @template-implements Encoder<stdClass|array|string|int, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
3335
class BuilderEncoder implements Encoder
3436
{
35-
/** @template-use EncodeIfSupported<stdClass|array|string, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
37+
/** @template-use EncodeIfSupported<stdClass|array|string|int, Pipeline|StageInterface|ExpressionInterface|QueryInterface> */
3638
use EncodeIfSupported;
3739

3840
/** @var array<class-string, class-string<ExpressionEncoder>> */
3941
private array $defaultEncoders = [
4042
Pipeline::class => PipelineEncoder::class,
4143
Variable::class => VariableEncoder::class,
44+
DictionaryInterface::class => DictionaryEncoder::class,
4245
FieldPathInterface::class => FieldPathEncoder::class,
4346
CombinedFieldQuery::class => CombinedFieldQueryEncoder::class,
4447
QueryObject::class => QueryEncoder::class,
@@ -64,7 +67,7 @@ public function canEncode(mixed $value): bool
6467
return (bool) $this->getEncoderFor($value)?->canEncode($value);
6568
}
6669

67-
public function encode(mixed $value): stdClass|array|string
70+
public function encode(mixed $value): stdClass|array|string|int
6871
{
6972
$encoder = $this->getEncoderFor($value);
7073

src/Builder/Encoder/AbstractExpressionEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use function is_array;
1212

1313
/**
14-
* @template BSONType of stdClass|array|string
14+
* @template BSONType of stdClass|array|string|int
1515
* @template NativeType
1616
* @template-implements ExpressionEncoder<BSONType, NativeType>
1717
*/
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\DictionaryInterface;
8+
use MongoDB\Codec\EncodeIfSupported;
9+
use MongoDB\Exception\UnsupportedValueException;
10+
use stdClass;
11+
12+
/** @template-extends AbstractExpressionEncoder<string|int|array|stdClass, DictionaryInterface> */
13+
class DictionaryEncoder extends AbstractExpressionEncoder
14+
{
15+
/** @template-use EncodeIfSupported<string|int|array|stdClass, DictionaryInterface> */
16+
use EncodeIfSupported;
17+
18+
public function canEncode(mixed $value): bool
19+
{
20+
return $value instanceof DictionaryInterface;
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/Encoder/ExpressionEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use stdClass;
1010

1111
/**
12-
* @template BSONType of stdClass|array|string
12+
* @template BSONType of stdClass|array|string|int
1313
* @template NativeType
1414
* @template-extends Encoder<BSONType, NativeType>
1515
*/

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.

0 commit comments

Comments
 (0)