Skip to content

Commit 6348a1b

Browse files
committed
Allow null value in aggregation result
1 parent 390f0d3 commit 6348a1b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Fixed
1010

1111
- Bad copy/paste in the [README](README.md) file
12+
- Allow `null` value in Aggregate result
1213

1314
## [1.1.0]
1415

src/Aggregate/Result.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@
2626

2727
class Result
2828
{
29-
/** @var array<string,float|int|string> */
29+
/** @var array<string,null|float|int|string> */
3030
private $fields = [];
3131

3232
/**
3333
* Result constructor.
3434
*
35-
* @param array<string,float|int|string> $fields
35+
* @param array<string,null|float|int|string> $fields
3636
*
3737
* @throws Throwable
3838
*/
3939
public function __construct(array $fields)
4040
{
41-
DataHelper::assertArrayOf($fields, 'scalar');
41+
DataHelper::assertArrayOf($fields, '?scalar');
4242
$this->fields = $fields;
4343
}
4444

4545
/**
46-
* @return array<string,float|int|string>
46+
* @return array<string,null|float|int|string>
4747
*/
4848
public function getFields(): array
4949
{

src/Helper/DataHelper.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use function sscanf;
3838
use function strpos;
3939
use function strtolower;
40+
use function substr;
4041
use Throwable;
4142
use UnexpectedValueException;
4243

@@ -77,12 +78,20 @@ public static function assert(bool $condition, $exception = null, int $deep = 1)
7778
* @param null|string|Throwable $exception
7879
*
7980
* @throws Throwable
80-
* @suppress PhanUndeclaredClass
8181
*/
8282
public static function assertArrayOf(array $array, string $type, $exception = null): void
8383
{
84+
$allowNull = false;
85+
if (0 === strpos($type, '?')) {
86+
$allowNull = true;
87+
$type = substr($type, 1);
88+
assert(is_string($type));
89+
}
8490
$type = self::getPhpType($type);
8591
foreach ($array as $item) {
92+
if (true === $allowNull && null === $item) {
93+
continue;
94+
}
8695
if (in_array($type, ['string', 'boolean', 'integer', 'double'], true)) {
8796
self::assert(gettype($item) === $type, $exception, 2);
8897
} elseif ('scalar' === $type) {

0 commit comments

Comments
 (0)