|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pinq\Queries\Builders; |
|
4
|
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
|
6
|
|
|
use Pinq\IQueryable; |
|
7
|
|
|
use Pinq\Queries\Builders\Interpretations\ISourceInterpretation; |
|
8
|
|
|
use Pinq\Queries; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Implementation of the source expression interpreter. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Elliot Levin <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class SourceInterpreter extends ExpressionInterpreter implements ISourceInterpreter |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $segmentId; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ISourceInterpretation |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $interpretation; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var IScopeInterpreter |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $scopeInterpreter; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
$segmentId, |
|
34
|
|
|
ISourceInterpretation $interpretation, |
|
35
|
|
|
IScopeInterpreter $scopeInterpreter, |
|
36
|
|
|
O\IEvaluationContext $evaluationContext = null |
|
37
|
|
|
) { |
|
38
|
|
|
parent::__construct($segmentId, $evaluationContext); |
|
39
|
|
|
$this->interpretation = $interpretation; |
|
40
|
|
|
$this->scopeInterpreter = $scopeInterpreter; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getInterpretation() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->interpretation; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function interpretSource(O\Expression $expression) |
|
49
|
|
|
{ |
|
50
|
|
|
$isQueryScope = false; |
|
51
|
|
|
$queryableQueryResolver = new O\DynamicExpressionWalker([ |
|
52
|
|
|
O\TraversalExpression::getType() => |
|
53
|
|
|
function (O\TraversalExpression $expression, O\ExpressionWalker $self) use (&$isQueryScope) { |
|
54
|
|
|
$expression = $expression->updateValue($self->walk($expression->getValue())); |
|
55
|
|
|
|
|
56
|
|
|
if ($isQueryScope) { |
|
57
|
|
|
return $expression; |
|
58
|
|
|
} else { |
|
59
|
|
|
return $self->walk(O\Expression::value($expression->evaluate($this->evaluationContext))); |
|
60
|
|
|
} |
|
61
|
|
|
}, |
|
62
|
|
|
O\ValueExpression::getType() => |
|
63
|
|
|
function (O\ValueExpression $expression) use (&$isQueryScope) { |
|
64
|
|
|
if ($expression->getValue() instanceof IQueryable) { |
|
65
|
|
|
$isQueryScope = true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $expression; |
|
69
|
|
|
} |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
$expression = $queryableQueryResolver->walk($expression); |
|
73
|
|
|
|
|
74
|
|
|
if ($isQueryScope) { |
|
75
|
|
|
$this->scopeInterpreter->interpretScope($expression); |
|
|
|
|
|
|
76
|
|
|
$this->interpretation->interpretQueryScope($this->getId('source-scope'), $this->scopeInterpreter->getInterpretation()); |
|
77
|
|
|
} else { |
|
78
|
|
|
$this->interpretation->interpretArrayOrIterator($this->getId('source-iterator'), $expression->evaluate($this->evaluationContext)); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: