Completed
Branch releases/v0.2 (d913c4)
by Luke
02:17
created

Row   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.15%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 1
cbo 4
dl 0
loc 87
ccs 25
cts 26
cp 0.9615
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 4 2
A offsetExists() 0 10 2
A offsetGet() 0 9 2
A setHeaderRow() 0 19 4
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
 */
27
class Row extends AbstractRow
28
{
29
    /**
30
     * @var array Same as fields only indexed using headers rather than numbers
31
     */
32
    protected $assocCols = [];
33
34
    /**
35
     * @var array The headers for this row
36
     */
37
    protected $headers = [];
38
39
    /**
40
     * Get the current key (column number or header, if available)
41
     *
42
     * @return string The "current" key
43
     * @access public
44
     * @todo Figure out if this can return a CSVelte\Table\HeaderData object so long as it
45
     *     has a __toString() method that generated the right key...
46
     */
47 14
    public function key()
48
    {
49 14
        return isset($this->headers[$this->position]) ? $this->headers[$this->position] : $this->position;
50
    }
51
52
    /**
53
     * Set the header row (so that it can be used to index the row)
54
     *
55
     * @param \CSVelte\Table\HeaderRow $headers Header row to set
56
     * @return void (return $this?)
57
     * @access public
58
     */
59 19
    public function setHeaderRow(AbstractRow $headers)
60
    {
61 19
        if (!($headers instanceof HeaderRow)) {
62 1
            $headers = new HeaderRow($headers->toArray());
63 1
        }
64 19
        $headerArray = $headers->toArray();
65 19
        if (($hcount = $headers->count()) !== ($rcount = $this->count())) {
66 2
            if ($hcount > $rcount) {
67
                // header count is long, could be an error, but lets just fill in the short row with null values...
68 1
                $this->fields = array_pad($this->fields, $hcount, null);
69 1
            } else {
70
                // @todo This is too strict. I need a way to recover from this a little better...
71
                // header count is short, this is likely an error...
72 1
                throw new HeaderException("Header count ({$hcount}) does not match column count ({$rcount}).", HeaderException::ERR_HEADER_COUNT);
73
            }
74 1
        }
75 18
        $this->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers of type object<CSVelte\Table\HeaderRow> is incompatible with the declared type array of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76 18
        $this->assocCols = array_combine($headerArray, $this->fields);
77 18
    }
78
79
    /**
80
     * Is there an offset at specified position?
81
     *
82
     * @param integer Offset
83
     * @return boolean
84
     * @access public
85
     */
86 5
    public function offsetExists($offset)
87
    {
88
        try {
89 5
            Utils::array_get($this->assocCols, $offset, null, true);
90 5
        } catch (\OutOfBoundsException $e) {
91
            // now check $this->properties?
92 5
            return parent::offsetExists($offset);
93
        }
94
        return true;
95
    }
96
97
    /**
98
     * Retrieve data at specified column offset
99
     *
100
     * @param integer Offset
101
     * @return CSVelte\Table\Data
102
     * @access public
103
     */
104 8
    public function offsetGet($offset)
105
    {
106
        try {
107 8
            $val = Utils::array_get($this->assocCols, $offset, null, true);
108 8
        } catch (\OutOfBoundsException $e) {
109 4
            return parent::offsetGet($offset);
110
        }
111 4
        return $val;
112
    }
113
}
114