-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessAttributes.php
90 lines (80 loc) · 3.3 KB
/
ProcessAttributes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Eav;
use Eav\Database\Query\Builder;
use Illuminate\Support\Collection;
class ProcessAttributes
{
/**
* Adds the requested attribute to the query.
*
* Until now we have not added the attribute to the query.
* Here we have the list of query and bindings, now we will
* inner or left join the attribute and add the querying
* conditions.
*
* @param Builder $query
* @param Collection $loadedAttributes
* @param Entity $baseEntity
* @param boolean $noJoin
* @return void
*/
public static function process(
Builder $query,
Collection $loadedAttributes,
Entity $baseEntity,
$noJoin = false
) {
$filterAttr = array_flip($query->attributeColumns);
$orginalColumns = array_flip($query->orginalColumns);
$usedAttributes = $loadedAttributes
->filter(function ($attribute) use ($filterAttr) {
return isset($filterAttr[$attribute->code()]);
});
foreach ((array) $query->attributeOrderBy as $bindings) {
foreach ($bindings as $binding) {
$attribute = $usedAttributes->get($binding['column']);
if ($attribute) {
$joinType = isset($orginalColumns[$attribute->code()])?'left':'inner';
$attribute->setEntity($baseEntity);
$attribute->addAttributeJoin($query, $joinType);
$attribute->addAttributeOrderBy($query, $binding);
}
}
}
if (!count($query->attributeWheresRef)) {
return;
}
foreach ((array) $query->attributeWheres['binding'] as $type => $bindings) {
switch ($type) {
case 'Nested':
foreach ($bindings as $binding) {
$binding['query']->processAttributes(true);
$query->addNestedWhereQuery($binding['query'], $binding['boolean']);
}
$loadedAttributes->each(function ($attribute, $key) use ($binding, $baseEntity, $query, $orginalColumns) {
$joinType = isset($orginalColumns[$attribute->code()]) || ($binding['boolean'] == 'or')?'left':'inner';
$attribute->setEntity($baseEntity);
$attribute->addAttributeJoin($query, $joinType);
});
break;
case 'Basic':
default:
foreach ($bindings as $binding) {
$attribute = $usedAttributes->get($binding['column'], false);
if ($attribute) {
$attribute->setEntity($baseEntity);
if (!$noJoin) {
$joinType = isset($orginalColumns[$attribute->code()]) || ($binding['boolean'] == 'or')?'left':'inner';
$attribute->addAttributeJoin($query, $joinType);
}
$attribute->addAttributeWhere(
$query,
$binding
);
}
}
break;
}
}
}
}