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) |
||
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 | * Setter for select. |
||
79 | * |
||
80 | * @param mixed $select |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function select($select) |
||
89 | |||
90 | /** |
||
91 | * Setter for where. |
||
92 | * |
||
93 | * @param mixed $where |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function where($where) |
||
102 | |||
103 | /** |
||
104 | * Perform main query. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function performQuery() |
||
116 | |||
117 | /** |
||
118 | * Collect ids from source and add them to $this->ids |
||
119 | * |
||
120 | * @param $item |
||
121 | * @param array $fields |
||
122 | */ |
||
123 | protected function collectIdsFromItem($item, array $fields) |
||
133 | |||
134 | /** |
||
135 | * Collect ids from field of item |
||
136 | * |
||
137 | * @param $item |
||
138 | * @param string $field |
||
139 | * @return array |
||
140 | */ |
||
141 | protected function collectIdsFromField($item, $field) |
||
147 | |||
148 | /** |
||
149 | * A faster alternative to array_unique. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function getIdsWithoutDuplications() |
||
157 | } |
||
158 |