1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\Collectors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
abstract class Collector |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Ids that are collected from sources. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $ids = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Fields that should be selected. |
18
|
|
|
* |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $select = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Additional filter. |
25
|
|
|
* |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
protected $where = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Data keyed by id. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $data = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get data for given ids. |
39
|
|
|
* |
40
|
|
|
* @param array $ids |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
abstract protected function getList(array $ids); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a collection source. |
47
|
|
|
* |
48
|
|
|
* @param $collection |
49
|
|
|
* @param mixed $fields |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function scanCollection($collection, $fields) |
53
|
|
|
{ |
54
|
|
|
$fields = (array) $fields; |
55
|
|
|
foreach ($collection as $item) { |
56
|
|
|
$this->collectIdsFromItem($item, $fields); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add an item source. |
64
|
|
|
* |
65
|
|
|
* @param $item |
66
|
|
|
* @param mixed $fields |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function scanItem($item, $fields) |
70
|
|
|
{ |
71
|
|
|
$fields = (array) $fields; |
72
|
|
|
$this->collectIdsFromItem($item, $fields); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add existeing ids array source. |
79
|
|
|
* |
80
|
|
|
* @param array $ids |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function addIds($ids) |
84
|
|
|
{ |
85
|
|
|
foreach ($ids as $id) { |
86
|
|
|
if ((int) $id) { |
87
|
|
|
$this->ids[] = (int) $id; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Setter for select. |
96
|
|
|
* |
97
|
|
|
* @param mixed $select |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function select($select) |
101
|
|
|
{ |
102
|
|
|
$this->select = $select; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Setter for where. |
109
|
|
|
* |
110
|
|
|
* @param mixed $where |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function where($where) |
114
|
|
|
{ |
115
|
|
|
$this->where = $where; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Perform main query. |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function performQuery() |
126
|
|
|
{ |
127
|
|
|
if (empty($this->ids)) { |
128
|
|
|
return []; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->getList($this->getIdsWithoutDuplications()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Collect ids from source and add them to $this->ids |
136
|
|
|
* |
137
|
|
|
* @param $item |
138
|
|
|
* @param array $fields |
139
|
|
|
*/ |
140
|
|
|
protected function collectIdsFromItem($item, array $fields) |
141
|
|
|
{ |
142
|
|
|
foreach ($fields as $field) { |
143
|
|
|
foreach ($this->collectIdsFromField($item, $field) as $id) { |
144
|
|
|
if ((int) $id) { |
145
|
|
|
$this->ids[] = (int) $id; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Collect ids from field of item |
153
|
|
|
* |
154
|
|
|
* @param $item |
155
|
|
|
* @param string $field |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
protected function collectIdsFromField($item, $field) |
159
|
|
|
{ |
160
|
|
|
$ids = Arr::get($item, $field, []); |
161
|
|
|
|
162
|
|
|
return is_object($ids) && method_exists($ids, 'toArray') ? $ids->toArray() : (array) $ids; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* A faster alternative to array_unique. |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
protected function getIdsWithoutDuplications() |
171
|
|
|
{ |
172
|
|
|
return array_flip(array_flip($this->ids)); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|