|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Aggregation\Stage; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
|
23
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Stage; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Fluent interface for adding a $unwind stage to an aggregation pipeline. |
|
27
|
|
|
* |
|
28
|
|
|
* @author alcaeus <[email protected]> |
|
29
|
|
|
* @since 1.2 |
|
30
|
|
|
*/ |
|
31
|
|
|
class Unwind extends Stage |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $fieldName; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $includeArrayIndex; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private $preserveNullAndEmptyArrays = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Builder $builder |
|
50
|
|
|
* @param string $fieldName |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function __construct(Builder $builder, $fieldName) |
|
53
|
|
|
{ |
|
54
|
9 |
|
parent::__construct($builder); |
|
55
|
|
|
|
|
56
|
9 |
|
$this->fieldName = (string) $fieldName; |
|
57
|
9 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
9 |
|
public function getExpression() |
|
63
|
|
|
{ |
|
64
|
|
|
// Fallback behavior for MongoDB < 3.2 |
|
65
|
9 |
|
if ($this->includeArrayIndex === null && ! $this->preserveNullAndEmptyArrays) { |
|
66
|
|
|
return [ |
|
67
|
8 |
|
'$unwind' => $this->fieldName |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$unwind = ['path' => $this->fieldName]; |
|
72
|
|
|
|
|
73
|
1 |
|
foreach (['includeArrayIndex', 'preserveNullAndEmptyArrays'] as $option) { |
|
74
|
1 |
|
if ( ! $this->$option) { |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$unwind[$option] = $this->$option; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return [ |
|
82
|
1 |
|
'$unwind' => $unwind |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* The name of a new field to hold the array index of the element. The name |
|
88
|
|
|
* cannot start with a dollar sign $. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $includeArrayIndex |
|
91
|
|
|
* @return $this |
|
92
|
|
|
* |
|
93
|
|
|
* @since 1.3 |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function includeArrayIndex($includeArrayIndex) |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->includeArrayIndex = $includeArrayIndex; |
|
98
|
|
|
|
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* If true, if the path is null, missing, or an empty array, $unwind outputs |
|
104
|
|
|
* the document. |
|
105
|
|
|
* |
|
106
|
|
|
* @param bool $preserveNullAndEmptyArrays |
|
107
|
|
|
* @return $this |
|
108
|
|
|
* |
|
109
|
|
|
* @since 1.3 |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function preserveNullAndEmptyArrays($preserveNullAndEmptyArrays = true) |
|
112
|
|
|
{ |
|
113
|
1 |
|
$this->preserveNullAndEmptyArrays = $preserveNullAndEmptyArrays; |
|
114
|
|
|
|
|
115
|
1 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|