|
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\Table; |
|
15
|
|
|
|
|
16
|
|
|
use CSVelte\Exception\HeaderException; |
|
17
|
|
|
use OutOfBoundsException; |
|
18
|
|
|
|
|
19
|
|
|
use function CSVelte\collect; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Table Row Class |
|
23
|
|
|
* Represents a row of tabular data (CSVelte\Table\Cell objects). |
|
24
|
|
|
* |
|
25
|
|
|
* @package CSVelte |
|
26
|
|
|
* @subpackage CSVelte\Table |
|
27
|
|
|
* |
|
28
|
|
|
* @copyright (c) 2016, Luke Visinoni <[email protected]> |
|
29
|
|
|
* @author Luke Visinoni <[email protected]> |
|
30
|
|
|
* |
|
31
|
|
|
* @todo May need to put toArray() method in here so that it uses headers |
|
32
|
|
|
* as keys here |
|
33
|
|
|
*/ |
|
34
|
|
|
class Row extends AbstractRow |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Get the current key (column number or header, if available). |
|
38
|
|
|
* |
|
39
|
|
|
* @return string The "current" key |
|
40
|
|
|
* |
|
41
|
|
|
* @todo Figure out if this can return a CSVelte\Table\HeaderData object so long as it |
|
42
|
|
|
* has a __toString() method that generated the right key... |
|
43
|
|
|
*/ |
|
44
|
|
|
public function key() |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
return $this->fields->getKeyAtPosition($this->position); |
|
48
|
|
|
} catch (OutOfBoundsException $e) { |
|
49
|
|
|
return parent::key(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set the header row (so that it can be used to index the row). |
|
55
|
|
|
* |
|
56
|
|
|
* @param AbstractRow|HeaderRow $headers Header row to set |
|
57
|
|
|
* |
|
58
|
|
|
* @throws HeaderException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setHeaderRow(AbstractRow $headers) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!($headers instanceof HeaderRow)) { |
|
63
|
|
|
$headers = new HeaderRow($headers->toArray()); |
|
64
|
|
|
} |
|
65
|
|
|
$headerArray = $headers->toArray(); |
|
66
|
|
|
if (($hcount = $headers->count()) !== ($rcount = $this->count())) { |
|
67
|
|
|
if ($hcount > $rcount) { |
|
68
|
|
|
// header count is long, could be an error, but lets just fill in the short row with null values... |
|
69
|
|
|
$this->fields = $this->fields->pad($hcount); |
|
70
|
|
|
} else { |
|
71
|
|
|
// @todo This is too strict. I need a way to recover from this a little better... |
|
72
|
|
|
// header count is short, this is likely an error... |
|
73
|
|
|
throw new HeaderException("Header count ({$hcount}) does not match column count ({$rcount}).", HeaderException::ERR_HEADER_COUNT); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
$this->fields = collect(array_combine( |
|
77
|
|
|
$headerArray, |
|
78
|
|
|
$this->fields->toArray() |
|
79
|
|
|
)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Is there an offset at specified position? |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $offset |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
* |
|
89
|
|
|
* @internal param Offset $integer |
|
90
|
|
|
*/ |
|
91
|
|
|
public function offsetExists($offset) |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
|
|
$this->fields->get($offset, null, true); |
|
95
|
|
|
} catch (\OutOfBoundsException $e) { |
|
96
|
|
|
return parent::offsetExists($offset); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieve data at specified column offset. |
|
104
|
|
|
* |
|
105
|
|
|
* @param mixed $offset The offset to get |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed The value at $offset |
|
108
|
|
|
*/ |
|
109
|
|
|
public function offsetGet($offset) |
|
110
|
|
|
{ |
|
111
|
|
|
try { |
|
112
|
|
|
$val = $this->fields->get($offset, null, true); |
|
113
|
|
|
} catch (\OutOfBoundsException $e) { |
|
114
|
|
|
return parent::offsetGet($offset); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $val; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|