1 | <?php |
||
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) |
||
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) |
||
76 | |||
77 | /** |
||
78 | * Add existeing ids array source. |
||
79 | * |
||
80 | * @param array $ids |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function addIds($ids) |
||
93 | |||
94 | /** |
||
95 | * Setter for select. |
||
96 | * |
||
97 | * @param mixed $select |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function select($select) |
||
106 | |||
107 | /** |
||
108 | * Setter for where. |
||
109 | * |
||
110 | * @param mixed $where |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function where($where) |
||
119 | |||
120 | /** |
||
121 | * Perform main query. |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function performQuery() |
||
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) |
||
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) |
||
164 | |||
165 | /** |
||
166 | * A faster alternative to array_unique. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | protected function getIdsWithoutDuplications() |
||
174 | } |
||
175 |