Completed
Push — master ( a0dcf3...deea3c )
by Nekrasov
02:03
created

Collector::scanCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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 (!$this->ids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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
            $ids = Arr::get($item, $field, []);
127
            if (is_object($ids) && method_exists($ids, 'toArray')) {
128
                $ids = $ids->toArray();
129
            }
130
131
            foreach ( (array) $ids as $id) {
132
                if ((int) $id) {
133
                    $this->ids[] = (int) $id;
134
                }
135
            }
136
        }
137
    }
138
139
    /**
140
     * A faster alternative to array_unique.
141
     *
142
     * @return array
143
     */
144
    protected function getIdsWithoutDuplications()
145
    {
146
        return array_flip(array_flip($this->ids));
147
    }
148
}
149