1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Selector\Composite; |
14
|
|
|
use Cubiche\Core\Selector\Field; |
15
|
|
|
use Cubiche\Core\Selector\Property; |
16
|
|
|
use Cubiche\Core\Selector\SelectorInterface; |
17
|
|
|
use Cubiche\Core\Selector\This; |
18
|
|
|
use Cubiche\Core\Selector\Value; |
19
|
|
|
use Cubiche\Core\Specification\Selector; |
20
|
|
|
use Cubiche\Domain\Model\EntityInterface; |
21
|
|
|
use Cubiche\Domain\Model\NativeValueObjectInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Visitor Class Trait. |
25
|
|
|
* |
26
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait VisitorTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var QueryBuilder |
32
|
|
|
*/ |
33
|
|
|
protected $queryBuilder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Composite $specification |
37
|
|
|
* |
38
|
|
|
* @return \Cubiche\Core\Selector\Field |
39
|
|
|
*/ |
40
|
|
|
protected function createFieldFromComposite(Composite $specification) |
41
|
|
|
{ |
42
|
|
|
$valueField = $this->createField($specification->valueSelector()); |
43
|
|
|
$applyField = $this->createField($specification->applySelector()); |
44
|
|
|
|
45
|
|
|
return new Property(\sprintf('%s.%s', $valueField->name(), $applyField->name())); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param SelectorInterface $selector |
50
|
|
|
* |
51
|
|
|
* @throws \LogicException |
52
|
|
|
* |
53
|
|
|
* @return \Cubiche\Core\Selector\Field |
54
|
|
|
*/ |
55
|
|
|
protected function createField(SelectorInterface $selector, $isEntityValue = false) |
56
|
|
|
{ |
57
|
|
|
if ($selector instanceof Selector) { |
58
|
|
|
return $this->createField($selector->selector(), $isEntityValue); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($selector instanceof Field) { |
62
|
|
|
return $selector; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($selector instanceof This) { |
66
|
|
|
return $isEntityValue ? new Property('id') : null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($selector instanceof Composite) { |
70
|
|
|
return $this->createFieldFromComposite($selector); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new \LogicException(\sprintf( |
74
|
|
|
'The %s specification cannot be used in the field name', |
75
|
|
|
\get_class($selector) |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param SelectorInterface $value |
81
|
|
|
* |
82
|
|
|
* @throws \LogicException |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
protected function createValue(SelectorInterface $value) |
87
|
|
|
{ |
88
|
|
|
if ($value instanceof Value) { |
89
|
|
|
$actualValue = $value->value(); |
90
|
|
|
if ($actualValue instanceof EntityInterface) { |
91
|
|
|
return $actualValue->id(); |
92
|
|
|
} |
93
|
|
|
if ($actualValue instanceof NativeValueObjectInterface) { |
94
|
|
|
return $actualValue->toNative(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $actualValue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
throw new \LogicException(\sprintf( |
101
|
|
|
'The %s selector cannot be used as a value selector', |
102
|
|
|
\get_class($value) |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $operation |
108
|
|
|
* |
109
|
|
|
* @return \LogicException |
110
|
|
|
*/ |
111
|
|
|
protected function notSupportedException($operation) |
112
|
|
|
{ |
113
|
|
|
return new \LogicException( |
114
|
|
|
\sprintf('The %s operation is not supported by Doctrine\ODM\MongoDB', \get_class($operation)) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|