1 | <?php |
||
40 | class LimitSubqueryWalker extends TreeWalkerAdapter |
||
41 | { |
||
42 | /** |
||
43 | * ID type hint. |
||
44 | */ |
||
45 | const IDENTIFIER_TYPE = 'doctrine_paginator.id.type'; |
||
46 | |||
47 | /** |
||
48 | * Counter for generating unique order column aliases. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | private $_aliasCounter = 0; |
||
53 | |||
54 | /** |
||
55 | * Walks down a SelectStatement AST node, modifying it to retrieve DISTINCT ids |
||
56 | * of the root Entity. |
||
57 | * |
||
58 | * @param SelectStatement $AST |
||
59 | * |
||
60 | * @return void |
||
61 | * |
||
62 | * @throws \RuntimeException |
||
63 | */ |
||
64 | 28 | public function walkSelectStatement(SelectStatement $AST) |
|
65 | { |
||
66 | 28 | $queryComponents = $this->_getQueryComponents(); |
|
67 | // Get the root entity and alias from the AST fromClause |
||
68 | 28 | $from = $AST->fromClause->identificationVariableDeclarations; |
|
69 | 28 | $fromRoot = reset($from); |
|
70 | 28 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
|
71 | 28 | $rootClass = $queryComponents[$rootAlias]['metadata']; |
|
72 | 28 | $selectExpressions = []; |
|
73 | |||
74 | 28 | $this->validate($AST); |
|
75 | |||
76 | 26 | foreach ($queryComponents as $dqlAlias => $qComp) { |
|
77 | // Preserve mixed data in query for ordering. |
||
78 | 26 | if (isset($qComp['resultVariable'])) { |
|
79 | $selectExpressions[] = new SelectExpression($qComp['resultVariable'], $dqlAlias); |
||
80 | 26 | continue; |
|
81 | } |
||
82 | } |
||
83 | |||
84 | 26 | $identifier = $rootClass->getSingleIdentifierFieldName(); |
|
85 | |||
86 | 26 | if (isset($rootClass->associationMappings[$identifier])) { |
|
87 | 1 | throw new \RuntimeException("Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator."); |
|
88 | } |
||
89 | |||
90 | 25 | $this->_getQuery()->setHint( |
|
91 | 25 | self::IDENTIFIER_TYPE, |
|
92 | 25 | Type::getType($rootClass->fieldMappings[$identifier]['type']) |
|
93 | ); |
||
94 | |||
95 | 25 | $pathExpression = new PathExpression( |
|
96 | 25 | PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, |
|
97 | $rootAlias, |
||
98 | 25 | $identifier |
|
99 | ); |
||
100 | |||
101 | 25 | $pathExpression->type = PathExpression::TYPE_STATE_FIELD; |
|
102 | |||
103 | 25 | array_unshift($selectExpressions, new SelectExpression($pathExpression, '_dctrn_id')); |
|
104 | |||
105 | 25 | $AST->selectClause->selectExpressions = $selectExpressions; |
|
106 | |||
107 | 25 | if (isset($AST->orderByClause)) { |
|
108 | 20 | foreach ($AST->orderByClause->orderByItems as $item) { |
|
109 | 20 | if ( ! $item->expression instanceof PathExpression) { |
|
110 | continue; |
||
111 | } |
||
112 | |||
113 | 20 | $AST->selectClause->selectExpressions[] = new SelectExpression( |
|
114 | 20 | $this->createSelectExpressionItem($item->expression), |
|
115 | 20 | '_dctrn_ord' . $this->_aliasCounter++ |
|
116 | ); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | 25 | $AST->selectClause->isDistinct = true; |
|
121 | 25 | } |
|
122 | |||
123 | /** |
||
124 | * Validate the AST to ensure that this walker is able to properly manipulate it. |
||
125 | * |
||
126 | * @param SelectStatement $AST |
||
127 | */ |
||
128 | 28 | private function validate(SelectStatement $AST) |
|
157 | |||
158 | /** |
||
159 | * Retrieve either an IdentityFunction (IDENTITY(u.assoc)) or a state field (u.name). |
||
160 | * |
||
161 | * @param \Doctrine\ORM\Query\AST\PathExpression $pathExpression |
||
162 | * |
||
163 | * @return \Doctrine\ORM\Query\AST\Functions\IdentityFunction |
||
164 | */ |
||
165 | 20 | private function createSelectExpressionItem(PathExpression $pathExpression) |
|
177 | } |
||
178 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.