Skip to content

Commit fad9b92

Browse files
committed
Implementação inicial dos parsings. Ainda sem o parsing de nome de colunas e tabelas para o join.
1 parent 943fe16 commit fad9b92

File tree

11 files changed

+591
-17
lines changed

11 files changed

+591
-17
lines changed

src/Config/Container/Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Zend\Cache\Storage\StorageInterface;
66
use Zend\Cache\StorageFactory;
77
use Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException;
8+
use Zend\EntityMapper\Config\Entity;
89

910
/**
1011
* Container
@@ -58,7 +59,7 @@ public function has($id)
5859
* @return mixed
5960
* @throws ItemNotFoundException
6061
*/
61-
public function get($id)
62+
public function get($id): Entity
6263
{
6364
if (!$this->has($id)) {
6465
throw new ItemNotFoundException("$id not found.");

src/Config/Enums/EntityConfigs.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Zend\EntityMapper\Config\Enums;
4+
5+
interface EntityConfigs
6+
{
7+
const SCHEMA = 'schema';
8+
const TABLE = 'table';
9+
const FIELDS = 'fields';
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Zend\EntityMapper\Config\Enums;
4+
5+
/**
6+
* PropertyConfigs
7+
*
8+
* @package Zend\EntityMapper\Enums
9+
*/
10+
interface PropertyConfigs
11+
{
12+
const PROPERTY = 'property';
13+
const ALIAS = 'alias';
14+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Zend\EntityMapper\Db\Select\Reflection;
4+
5+
use Zend\Db\Sql\Predicate\Operator;
6+
use Zend\Db\Sql\Where;
7+
8+
/**
9+
* WhereReflector
10+
*
11+
* @package Zend\EntityMapper\Db\Select\Reflection
12+
*/
13+
class OperatorReflector
14+
{
15+
/**
16+
* @var Operator
17+
*/
18+
private $operator;
19+
20+
/**
21+
* @var \ReflectionObject
22+
*/
23+
private $reflection;
24+
25+
/**
26+
* OperatorReflector constructor.
27+
*
28+
* @param Operator $operator
29+
*/
30+
public function __construct(Operator $operator)
31+
{
32+
$this->operator = $operator;
33+
$this->reflection = new \ReflectionObject($operator);
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
public function getIdentifiers(): array
40+
{
41+
$identifiers = [];
42+
43+
$leftType = $this->reflection->getProperty('leftType');
44+
$leftType->setAccessible(true);
45+
$leftType = $leftType->getValue($this->operator);
46+
47+
$rightType = $this->reflection->getProperty('rightType');
48+
$rightType->setAccessible(true);
49+
$rightType = $rightType->getValue($this->operator);
50+
51+
if($rightType == 'identifier') {
52+
$right = $this->reflection->getProperty('right');
53+
$right->setAccessible(true);
54+
$right = $right->getValue($this->operator);
55+
56+
$identifiers[] = $right;
57+
}
58+
59+
if($leftType == 'identifier') {
60+
$left = $this->reflection->getProperty('left');
61+
$left->setAccessible(true);
62+
$left = $left->getValue($this->operator);
63+
64+
$identifiers[] = $left;
65+
}
66+
67+
return $identifiers;
68+
}
69+
70+
/**
71+
* @param string $from
72+
* @param string $to
73+
* @return Operator
74+
*/
75+
public function replaceIdentifier(string $from, string $to): Operator
76+
{
77+
$leftType = $this->reflection->getProperty('leftType');
78+
$leftType->setAccessible(true);
79+
$leftType = $leftType->getValue($this->operator);
80+
81+
$rightType = $this->reflection->getProperty('rightType');
82+
$rightType->setAccessible(true);
83+
$rightType = $rightType->getValue($this->operator);
84+
85+
if($rightType == 'identifier') {
86+
$right = $this->reflection->getProperty('right');
87+
$right->setAccessible(true);
88+
$rightValue = $right->getValue($this->operator);
89+
90+
if ($rightValue == $from) {
91+
$right->setValue($this->operator, $to);
92+
}
93+
}
94+
95+
if($leftType == 'identifier') {
96+
$left = $this->reflection->getProperty('left');
97+
$left->setAccessible(true);
98+
$leftValue = $left->getValue($this->operator);
99+
100+
if ($leftValue == $from) {
101+
$left->setValue($this->operator, $to);
102+
}
103+
}
104+
105+
return $this->operator;
106+
}
107+
}

0 commit comments

Comments
 (0)