1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Nozavroni/Collections |
5
|
|
|
* Just another collections library for PHP5.6+. |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
8
|
|
|
* @author Luke Visinoni <[email protected]> |
9
|
|
|
* @license https://github.com/nozavroni/collections/blob/master/LICENSE The MIT License (MIT) |
10
|
|
|
*/ |
11
|
|
|
namespace Noz\Collection; |
12
|
|
|
|
13
|
|
|
use BadMethodCallException; |
14
|
|
|
use OutOfBoundsException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class TabularCollection. |
18
|
|
|
* |
19
|
|
|
* A collection that works as a table, meaning each item is itself an traversable item, all rows having the same set of |
20
|
|
|
* keys. This allows you to work with columns as well as rows. |
21
|
|
|
* |
22
|
|
|
* @package Noz\Collection |
23
|
|
|
*/ |
24
|
|
|
class TabularCollection extends MultiCollection |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Magic method call. |
28
|
|
|
* |
29
|
|
|
* @param string $method The name of the method |
30
|
|
|
* @param array $args The argument list |
31
|
|
|
* |
32
|
|
|
* @throws BadMethodCallException If no method exists |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
* |
36
|
|
|
* @todo Add phpdoc comments for dynamic methods |
37
|
|
|
* @todo throw BadMethodCallException |
38
|
|
|
*/ |
39
|
9 |
|
public function __call($method, $args) |
40
|
|
|
{ |
41
|
9 |
|
$argc = count($args); |
42
|
9 |
|
if ($argc == 1 && $this->hasColumn($index = array_pop($args))) { |
43
|
7 |
|
$column = $this->getColumn($index); |
44
|
7 |
|
if (method_exists($column, $method)) { |
45
|
7 |
|
return call_user_func_array([$column, $method], $args); |
46
|
|
|
} |
47
|
|
|
} |
48
|
2 |
|
throw new BadMethodCallException('Method does not exist: ' . __CLASS__ . "::{$method}()"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Does this collection have specified column? |
53
|
|
|
* |
54
|
|
|
* @param mixed $column The column index |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
9 |
|
public function hasColumn($column) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
9 |
|
$this->getColumn($column); |
62
|
|
|
|
63
|
8 |
|
return true; |
64
|
2 |
|
} catch (OutOfBoundsException $e) { |
65
|
2 |
|
return false; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get column as collection. |
71
|
|
|
* |
72
|
|
|
* @param mixed $column The column index |
73
|
|
|
* @param bool $throw Throw an exception on failure |
74
|
|
|
* |
75
|
|
|
* @return AbstractCollection|false |
76
|
|
|
*/ |
77
|
12 |
|
public function getColumn($column, $throw = true) |
78
|
|
|
{ |
79
|
12 |
|
$values = array_column($this->data, $column); |
80
|
12 |
|
if (count($values)) { |
81
|
9 |
|
return static::factory($values); |
82
|
|
|
} |
83
|
4 |
|
if ($throw) { |
84
|
3 |
|
throw new OutOfBoundsException(__CLASS__ . ' could not find column: ' . $column); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Does this collection have a row at specified index? |
92
|
|
|
* |
93
|
|
|
* @param int $offset The column index |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
1 |
|
public function hasRow($offset) |
98
|
|
|
{ |
99
|
|
|
try { |
100
|
1 |
|
$this->getRow($offset); |
101
|
|
|
|
102
|
1 |
|
return true; |
103
|
1 |
|
} catch (OutOfBoundsException $e) { |
104
|
1 |
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get row at specified index. |
110
|
|
|
* |
111
|
|
|
* @param int $offset The row offset (starts from 0) |
112
|
|
|
* |
113
|
|
|
* @return AbstractCollection|false |
114
|
|
|
*/ |
115
|
3 |
|
public function getRow($offset) |
116
|
|
|
{ |
117
|
3 |
|
return static::factory($this->getValueAtPosition($offset)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
1 |
|
public function map(callable $callback) |
124
|
|
|
{ |
125
|
1 |
|
$ret = []; |
126
|
1 |
|
foreach ($this->data as $key => $row) { |
127
|
1 |
|
$ret[$key] = $callback(static::factory($row)); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
return static::factory($ret); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function walk(callable $callback, $extraContext = null) |
137
|
|
|
{ |
138
|
1 |
|
foreach ($this as $offset => $row) { |
139
|
1 |
|
$callback(static::factory($row), $offset, $extraContext); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
24 |
|
protected function prepareData($data) |
149
|
|
|
{ |
150
|
24 |
|
return $data; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Is input data structure valid? |
155
|
|
|
* |
156
|
|
|
* In order to determine whether a given data structure is valid for a |
157
|
|
|
* particular collection type (tabular, numeric, etc.), we have this method. |
158
|
|
|
* |
159
|
|
|
* @param mixed $data The data structure to check |
160
|
|
|
* |
161
|
|
|
* @return bool True if data structure is tabular |
162
|
|
|
*/ |
163
|
24 |
|
protected function isConsistentDataStructure($data) |
164
|
|
|
{ |
165
|
24 |
|
return static::isTabular($data); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|