1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
4
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
5
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
6
|
|
|
* suck out of working with CSV. |
7
|
|
|
* |
8
|
|
|
* @version v0.2 |
9
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
10
|
|
|
* @author Luke Visinoni <[email protected]> |
11
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
namespace CSVelte\Table; |
14
|
|
|
|
15
|
|
|
use CSVelte\Utils; |
16
|
|
|
use CSVelte\Exception\HeaderException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Table Row Class |
20
|
|
|
* Represents a row of tabular data (CSVelte\Table\Cell objects) |
21
|
|
|
* |
22
|
|
|
* @package CSVelte |
23
|
|
|
* @subpackage CSVelte\Table |
24
|
|
|
* @copyright (c) 2016, Luke Visinoni <[email protected]> |
25
|
|
|
* @author Luke Visinoni <[email protected]> |
26
|
|
|
* @todo May need to put toArray() method in here so that it uses headers |
27
|
|
|
* as keys here |
28
|
|
|
*/ |
29
|
|
|
class Row extends AbstractRow |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array Same as fields only indexed using headers rather than numbers |
33
|
|
|
*/ |
34
|
|
|
protected $assocCols = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array The headers for this row |
38
|
|
|
*/ |
39
|
|
|
protected $headers = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the current key (column number or header, if available) |
43
|
|
|
* |
44
|
|
|
* @return string The "current" key |
45
|
|
|
* @access public |
46
|
|
|
* @todo Figure out if this can return a CSVelte\Table\HeaderData object so long as it |
47
|
|
|
* has a __toString() method that generated the right key... |
48
|
|
|
*/ |
49
|
14 |
|
public function key() |
50
|
|
|
{ |
51
|
14 |
|
return isset($this->headers[$this->position]) ? $this->headers[$this->position] : $this->position; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the header row (so that it can be used to index the row) |
56
|
|
|
* |
57
|
|
|
* @param \CSVelte\Table\HeaderRow $headers Header row to set |
58
|
|
|
* @return void (return $this?) |
59
|
|
|
* @access public |
60
|
|
|
*/ |
61
|
20 |
|
public function setHeaderRow(AbstractRow $headers) |
62
|
|
|
{ |
63
|
20 |
|
if (!($headers instanceof HeaderRow)) { |
64
|
1 |
|
$headers = new HeaderRow($headers->toArray()); |
65
|
1 |
|
} |
66
|
20 |
|
$headerArray = $headers->toArray(); |
67
|
20 |
|
if (($hcount = $headers->count()) !== ($rcount = $this->count())) { |
68
|
2 |
|
if ($hcount > $rcount) { |
69
|
|
|
// header count is long, could be an error, but lets just fill in the short row with null values... |
70
|
1 |
|
$this->fields = array_pad($this->fields, $hcount, null); |
71
|
1 |
|
} else { |
72
|
|
|
// @todo This is too strict. I need a way to recover from this a little better... |
73
|
|
|
// header count is short, this is likely an error... |
74
|
1 |
|
throw new HeaderException("Header count ({$hcount}) does not match column count ({$rcount}).", HeaderException::ERR_HEADER_COUNT); |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
19 |
|
$this->headers = $headerArray; |
78
|
19 |
|
$this->assocCols = array_combine($headerArray, $this->fields); |
79
|
19 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Is there an offset at specified position? |
83
|
|
|
* |
84
|
|
|
* @param integer Offset |
85
|
|
|
* @return boolean |
86
|
|
|
* @access public |
87
|
|
|
*/ |
88
|
5 |
|
public function offsetExists($offset) |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
5 |
|
Utils::array_get($this->assocCols, $offset, null, true); |
92
|
5 |
|
} catch (\OutOfBoundsException $e) { |
93
|
|
|
// now check $this->properties? |
94
|
5 |
|
return parent::offsetExists($offset); |
95
|
|
|
} |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve data at specified column offset |
101
|
|
|
* |
102
|
|
|
* @param integer Offset |
103
|
|
|
* @return CSVelte\Table\Data |
104
|
|
|
* @access public |
105
|
|
|
*/ |
106
|
9 |
|
public function offsetGet($offset) |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
9 |
|
$val = Utils::array_get($this->assocCols, $offset, null, true); |
110
|
9 |
|
} catch (\OutOfBoundsException $e) { |
111
|
4 |
|
return parent::offsetGet($offset); |
112
|
|
|
} |
113
|
5 |
|
return $val; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|