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