1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Aggregation\Stage; |
6
|
|
|
|
7
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
8
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Expr; |
9
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Stage; |
10
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
11
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
12
|
|
|
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister; |
13
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
14
|
|
|
use function array_map; |
15
|
|
|
use function is_array; |
16
|
|
|
use function is_string; |
17
|
|
|
use function substr; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract class with common functionality for $bucket and $bucketAuto stages |
21
|
|
|
* |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractBucket extends Stage |
25
|
|
|
{ |
26
|
|
|
/** @var DocumentManager */ |
27
|
|
|
private $dm; |
28
|
|
|
|
29
|
|
|
/** @var ClassMetadata */ |
30
|
|
|
private $class; |
31
|
|
|
|
32
|
|
|
/** @var Bucket\AbstractOutput|null */ |
33
|
|
|
protected $output; |
34
|
|
|
|
35
|
|
|
/** @var Expr|array */ |
36
|
|
|
protected $groupBy; |
37
|
|
|
|
38
|
8 |
|
public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class) |
39
|
|
|
{ |
40
|
8 |
|
$this->dm = $documentManager; |
41
|
8 |
|
$this->class = $class; |
42
|
|
|
|
43
|
8 |
|
parent::__construct($builder); |
44
|
8 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An expression to group documents by. To specify a field path, prefix the |
48
|
|
|
* field name with a dollar sign $ and enclose it in quotes. |
49
|
|
|
* |
50
|
|
|
* @param array|Expr $expression |
51
|
|
|
*/ |
52
|
8 |
|
public function groupBy($expression) : self |
53
|
|
|
{ |
54
|
8 |
|
$this->groupBy = $expression; |
55
|
|
|
|
56
|
8 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
8 |
|
public function getExpression() : array |
63
|
|
|
{ |
64
|
|
|
$stage = [ |
65
|
8 |
|
$this->getStageName() => [ |
66
|
8 |
|
'groupBy' => $this->convertExpression($this->groupBy), |
67
|
8 |
|
] + $this->getExtraPipelineFields(), |
68
|
|
|
]; |
69
|
|
|
|
70
|
8 |
|
if ($this->output !== null) { |
71
|
6 |
|
$stage[$this->getStageName()]['output'] = $this->output->getExpression(); |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
return $stage; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
abstract protected function getExtraPipelineFields() : array; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the stage name with the dollar prefix |
81
|
|
|
*/ |
82
|
|
|
abstract protected function getStageName() : string; |
83
|
|
|
|
84
|
8 |
View Code Duplication |
private function convertExpression($expression) |
|
|
|
|
85
|
|
|
{ |
86
|
8 |
|
if (is_array($expression)) { |
87
|
|
|
return array_map([$this, 'convertExpression'], $expression); |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
if (is_string($expression) && substr($expression, 0, 1) === '$') { |
91
|
8 |
|
return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return Type::convertPHPToDatabaseValue(Expr::convertExpression($expression)); |
95
|
|
|
} |
96
|
|
|
|
97
|
8 |
|
private function getDocumentPersister() : DocumentPersister |
98
|
|
|
{ |
99
|
8 |
|
return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.