1+ <?php
2+
3+ namespace Zend \EntityMapper \Db \Select \Parsing ;
4+
5+ use Zend \Db \Sql \Select ;
6+ use Zend \EntityMapper \Config \Container \Container ;
7+ use Zend \EntityMapper \Config \Entity ;
8+ use Zend \EntityMapper \Db \Select \Reflection \OperatorReflector ;
9+ use Zend \EntityMapper \Db \Select \Reflection \SelectReflector ;
10+
11+ /**
12+ * SelectParser
13+ *
14+ * @package Zend\EntityMapper\Db\Select\Parsing
15+ */
16+ class SelectParser
17+ {
18+ /**
19+ * @var SelectReflector
20+ */
21+ private $ reflector ;
22+
23+ /**
24+ * @var Container
25+ */
26+ private $ container ;
27+
28+ /**
29+ * @var string
30+ */
31+ private $ entity ;
32+
33+ /**
34+ * @var string
35+ */
36+ private $ tableAlias ;
37+
38+ /**
39+ * @var Entity
40+ */
41+ private $ map ;
42+
43+ /**
44+ * SelectParser constructor.
45+ *
46+ * @param Select $select
47+ * @throws \Zend\Cache\Exception\ExceptionInterface
48+ */
49+ public function __construct (Select $ select )
50+ {
51+ $ this ->reflector = new SelectReflector ($ select );
52+ $ this ->container = new Container ();
53+
54+ $ entity = $ this ->reflector ->getFrom ();
55+
56+ if (is_array ($ entity )) {
57+ foreach ($ entity as $ alias => $ namespace ) {
58+ $ this ->entity = $ namespace ;
59+ $ this ->tableAlias = $ alias ;
60+ }
61+ }
62+
63+
64+ if (is_string ($ entity ) && class_exists ($ entity )) {
65+ $ this ->entity = $ entity ;
66+ }
67+
68+ }
69+
70+ /**
71+ * @return mixed|Entity
72+ * @throws \Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException
73+ */
74+ public function getMap ()
75+ {
76+ if (!$ this ->map instanceof Entity) {
77+ $ this ->map = $ this ->container ->get ($ this ->entity );
78+ }
79+
80+ return $ this ->map ;
81+ }
82+
83+ /**
84+ * @return Select
85+ * @throws \Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException
86+ */
87+ public function parseFrom (): Select
88+ {
89+ if (!empty ($ this ->tableAlias )) {
90+ $ this ->reflector ->setFrom ([
91+ $ this ->tableAlias => $ this ->getMap ()->getTable ()
92+ ]);
93+ }
94+ else {
95+ $ this ->reflector ->setFrom ($ this ->getMap ()->getTable ());
96+ }
97+
98+ return $ this ->reflector ->getSelect ();
99+ }
100+
101+ /**
102+ * @return Select
103+ * @throws \Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException
104+ */
105+ public function parseColumns (): Select
106+ {
107+ $ map = $ this ->getMap ();
108+
109+ $ columns = [];
110+
111+ foreach ($ this ->reflector ->getColumns () as $ columnAlias => $ column ) {
112+ $ propertyAlias = $ map ->getField ($ column )->getAlias ();
113+ $ columns [$ columnAlias ] = $ propertyAlias ;
114+ }
115+
116+ $ this ->reflector ->setColumns ($ columns );
117+ return $ this ->reflector ->getSelect ();
118+ }
119+
120+ /**
121+ * @return Select
122+ * @throws \Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException
123+ */
124+ public function parseWhere (): Select
125+ {
126+ $ predicates = $ this ->reflector ->getWherePredicates ();
127+ $ map = $ this ->getMap ();
128+
129+ foreach ($ predicates as $ predicate ) {
130+ $ reflection = new OperatorReflector ($ predicate [1 ]);
131+ $ identifiers = $ reflection ->getIdentifiers ();
132+ $ fields = $ map ->getFields ();
133+
134+ foreach ($ fields as $ field ) {
135+ if (in_array ($ field ->getProperty (), $ identifiers )) {
136+ $ predicate [1 ] = $ reflection ->replaceIdentifier ($ field ->getProperty (), $ field ->getAlias ());
137+ }
138+ }
139+
140+ }
141+
142+ return $ this ->reflector ->getSelect ();
143+ }
144+
145+ /**
146+ * @return Select
147+ * @throws \Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException
148+ */
149+ public function parseOrder (): Select
150+ {
151+ $ orders = $ this ->reflector ->getOrder ();
152+ $ parsedOrders = [];
153+
154+ foreach ($ orders as $ order ) {
155+ $ fields = $ this ->getMap ()->getFields ();
156+
157+ foreach ($ fields as $ field ) {
158+ $ order = str_replace ($ field ->getProperty (), $ field ->getAlias (), $ order );
159+ }
160+
161+ $ parsedOrders [] = $ order ;
162+ }
163+
164+ $ select = $ this ->reflector ->getSelect ();
165+
166+ $ reflection = new \ReflectionObject ($ select );
167+ $ order = $ reflection ->getProperty ('order ' );
168+ $ order ->setAccessible (true );
169+ $ order ->setValue ($ select , $ parsedOrders );
170+
171+ return $ select ;
172+
173+ }
174+ }
0 commit comments