Completed
Push — master ( 5c1aea...9ef1c4 )
by Luke
03:03
created

Row::key()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

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