|
22 | 22 | namespace MacFJA\RediSearch\Aggregate; |
23 | 23 |
|
24 | 24 | use function count; |
| 25 | +use function is_bool; |
25 | 26 | use function is_string; |
26 | 27 | use MacFJA\RediSearch\Helper\DataHelper; |
27 | 28 | use MacFJA\RediSearch\PartialQuery; |
28 | 29 | use Throwable; |
29 | 30 |
|
| 31 | +/** |
| 32 | + * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
| 33 | + */ |
30 | 34 | class Reducer implements PartialQuery |
31 | 35 | { |
32 | 36 | /** @var string */ |
@@ -64,4 +68,73 @@ public function getQueryParts(): array |
64 | 68 |
|
65 | 69 | return $query; |
66 | 70 | } |
| 71 | + |
| 72 | + public static function count(?string $name = null): self |
| 73 | + { |
| 74 | + return new self('COUNT', [], $name); |
| 75 | + } |
| 76 | + |
| 77 | + public static function countDistinct(string $property, ?string $name = null): self |
| 78 | + { |
| 79 | + return new self('COUNT_DISTINCT', ['@'.$property], $name); |
| 80 | + } |
| 81 | + |
| 82 | + public static function countDistinctish(string $property, ?string $name = null): self |
| 83 | + { |
| 84 | + return new self('COUNT_DISTINCTISH', ['@'.$property], $name); |
| 85 | + } |
| 86 | + |
| 87 | + public static function sum(string $property, ?string $name = null): self |
| 88 | + { |
| 89 | + return new self('SUM', ['@'.$property], $name); |
| 90 | + } |
| 91 | + |
| 92 | + public static function minimum(string $property, ?string $name = null): self |
| 93 | + { |
| 94 | + return new self('MIN', ['@'.$property], $name); |
| 95 | + } |
| 96 | + |
| 97 | + public static function maximum(string $property, ?string $name = null): self |
| 98 | + { |
| 99 | + return new self('MAX', ['@'.$property], $name); |
| 100 | + } |
| 101 | + |
| 102 | + public static function average(string $property, ?string $name = null): self |
| 103 | + { |
| 104 | + return new self('AVG', ['@'.$property], $name); |
| 105 | + } |
| 106 | + |
| 107 | + public static function standardDeviation(string $property, ?string $name = null): self |
| 108 | + { |
| 109 | + return new self('STDDEV', ['@'.$property], $name); |
| 110 | + } |
| 111 | + |
| 112 | + public static function quantile(string $property, float $quantile, ?string $name = null): self |
| 113 | + { |
| 114 | + return new self('QUANTILE', ['@'.$property, $quantile], $name); |
| 115 | + } |
| 116 | + |
| 117 | + public static function median(string $property, ?string $name = null): self |
| 118 | + { |
| 119 | + return new self('QUANTILE', ['@'.$property, 0.5], $name); |
| 120 | + } |
| 121 | + |
| 122 | + public static function toList(string $property, ?string $name = null): self |
| 123 | + { |
| 124 | + return new self('TOLIST', ['@'.$property], $name); |
| 125 | + } |
| 126 | + |
| 127 | + public static function firstValue(string $property, ?string $sortByProperty = null, ?bool $ascending = null, ?string $name = null): self |
| 128 | + { |
| 129 | + $arguments = ['@'.$property]; |
| 130 | + if (is_string($sortByProperty)) { |
| 131 | + $arguments[] = 'BY'; |
| 132 | + $arguments[] = '@'.$sortByProperty; |
| 133 | + if (is_bool($ascending)) { |
| 134 | + $arguments[] = true === $ascending ? 'ASC' : 'DESC'; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return new self('FIRST_VALUE', $arguments, $name); |
| 139 | + } |
67 | 140 | } |
0 commit comments