Completed
Push — master ( deea3c...8c82c5 )
by Nekrasov
01:38
created

Collector::collectIdsFromField()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 2
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
     * Setter for select.
79
     *
80
     * @param mixed $select
81
     * @return $this
82
     */
83
    public function select($select)
84
    {
85
        $this->select = $select;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Setter for where.
92
     *
93
     * @param mixed $where
94
     * @return $this
95
     */
96
    public function where($where)
97
    {
98
        $this->where = $where;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Perform main query.
105
     *
106
     * @return array
107
     */
108
    public function performQuery()
109
    {
110
        if (empty($this->ids)) {
111
            return [];
112
        }
113
114
        return $this->getList($this->getIdsWithoutDuplications());
115
    }
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)
124
    {
125
        foreach ($fields as $field) {
126
            foreach ($this->collectIdsFromField($item, $field) as $id) {
127
                if ((int) $id) {
128
                    $this->ids[] = (int) $id;
129
                }
130
            }
131
        }
132
    }
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)
142
    {
143
        $ids = Arr::get($item, $field, []);
144
145
        return is_object($ids) && method_exists($ids, 'toArray') ? $ids->toArray() : (array) $ids;
146
    }
147
148
    /**
149
     * A faster alternative to array_unique.
150
     *
151
     * @return array
152
     */
153
    protected function getIdsWithoutDuplications()
154
    {
155
        return array_flip(array_flip($this->ids));
156
    }
157
}
158