1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence\Searchable; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
|
9
|
|
|
class ColumnCollection implements ArrayAccess, IteratorAggregate |
10
|
|
|
{ |
11
|
|
|
/** @var array */ |
12
|
|
|
protected $columns = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create new searchable columns collection. |
16
|
|
|
* |
17
|
|
|
* @param array $columns |
18
|
|
|
*/ |
19
|
|
|
public function __construct(array $columns = []) |
20
|
|
|
{ |
21
|
|
|
foreach ($columns as $column) { |
22
|
|
|
$this->add($column); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get columns as plain array. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getColumns() |
32
|
|
|
{ |
33
|
|
|
return $this->columns; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add column to the collection. |
38
|
|
|
* |
39
|
|
|
* @param Column $column |
40
|
|
|
*/ |
41
|
|
|
public function add(Column $column) |
42
|
|
|
{ |
43
|
|
|
$this->columns[$column->getMapping()] = $column; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get array of qualified columns names. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getQualifiedNames() |
52
|
|
|
{ |
53
|
|
|
return array_map(function ($column) { |
54
|
|
|
return $column->getQualifiedName(); |
55
|
|
|
}, $this->columns); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get array of tables names. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getTables() |
64
|
|
|
{ |
65
|
|
|
return array_unique(array_map(function ($column) { |
66
|
|
|
return $column->getTable(); |
67
|
|
|
}, $this->columns)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get array of columns mappings and weights. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function getWeights() |
76
|
|
|
{ |
77
|
|
|
$weights = []; |
78
|
|
|
|
79
|
|
|
foreach ($this->columns as $column) { |
80
|
|
|
$weights[$column->getMapping()] = $column->getWeight(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $weights; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get array of columns mappings. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getMappings() |
92
|
|
|
{ |
93
|
|
|
return array_map(function ($column) { |
94
|
|
|
return $column->getMapping(); |
95
|
|
|
}, $this->columns); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if element exists at given offset. |
100
|
|
|
* |
101
|
|
|
* @param string $key |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function offsetExists($key) |
105
|
|
|
{ |
106
|
|
|
return array_key_exists($key, $this->columns); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get element at given offset. |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* @return Column |
114
|
|
|
*/ |
115
|
|
|
public function offsetGet($key) |
116
|
|
|
{ |
117
|
|
|
return $this->columns[$key]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set element at given offset. |
122
|
|
|
* |
123
|
|
|
* @param string $key [description] |
124
|
|
|
* @param Column $column |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function offsetSet($key, $column) |
128
|
|
|
{ |
129
|
|
|
$this->add($column); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Unset element at given offset. |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* @return Column |
137
|
|
|
*/ |
138
|
|
|
public function offsetUnset($key) |
139
|
|
|
{ |
140
|
|
|
unset($this->columns[$key]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get an iterator for the columns. |
145
|
|
|
* |
146
|
|
|
* @return ArrayIterator |
147
|
|
|
*/ |
148
|
|
|
public function getIterator() |
149
|
|
|
{ |
150
|
|
|
return new ArrayIterator($this->columns); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|