Passed
Branch refactor/164-removeoldcollecti... (a8e6c3)
by Luke
02:29
created

TabularCollection::hasRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
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   v${CSVELTE_DEV_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
use function CSVelte\is_traversable;
19
use function CSVelte\collect;
20
21
class TabularCollection extends MultiCollection
22
{
23
    /**
24
     * Is input data structure valid?
25
     *
26
     * In order to determine whether a given data structure is valid for a
27
     * particular collection type (tabular, numeric, etc.), we have this method.
28
     *
29
     * @param mixed $data The data structure to check
30
     * @return boolean True if data structure is tabular
31
     */
32 43
    protected function isConsistentDataStructure($data)
33
    {
34 43
        return static::isTabular($data);
35
    }
36
37
    /**
38
     * Does this collection have specified column?
39
     *
40
     * @param mixed $column The column index
41
     * @return bool
42
     */
43 8
    public function hasColumn($column)
44
    {
45
        try {
46 8
            $this->getColumn($column);
47 8
            return true;
48 1
        } catch (OutOfBoundsException $e) {
49 1
            return false;
50
        }
51
    }
52
53
    /**
54
     * Get column as collection
55
     *
56
     * @param mixed $column The column index
57
     * @param bool $throw Throw an exception on failure
58
     * @return AbstractCollection|false
59
     */
60 9
    public function getColumn($column, $throw = true)
61
    {
62 9
        $values = array_column($this->data, $column);
63 9
        if (count($values)) {
64 9
            return static::factory($values);
65
        }
66 1
        if ($throw) {
67 1
            throw new OutOfBoundsException(__CLASS__ . " could not find column: " . $column);
68
        }
69
        return false;
70
    }
71
72
    /**
73
     * Does this collection have a row at specified index?
74
     *
75
     * @param int $offset The column index
76
     * @return bool
77
     */
78 1
    public function hasRow($offset)
79
    {
80
        try {
81 1
            $this->getRow($offset);
82 1
            return true;
83 1
        } catch (OutOfBoundsException $e) {
84 1
            return false;
85
        }
86
    }
87
88
    /**
89
     * Get row at specified index.
90
     *
91
     * @param int $offset The row offset (starts from 0)
92
     *
93
     * @return AbstractCollection|false
94
     */
95 3
    public function getRow($offset)
96
    {
97 3
        return $this->getValueAtPosition($offset);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 1
    public function map(callable $callback)
104
    {
105 1
        $ret = [];
106 1
        foreach ($this->data as $key => $row) {
107 1
            $ret[$key] = $callback(static::factory($row));
108 1
        }
109 1
        return static::factory($ret);
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 21
    public function walk(callable $callback, $extraContext = null)
116
    {
117 21
        foreach ($this as $offset => $row) {
118 21
            $callback(static::factory($row), $offset, $extraContext);
119 21
        }
120 21
        return $this;
121
    }
122
123
    /**
124
     * Magic method call
125
     *
126
     * @param string $method The name of the method
127
     * @param array $args The argument list
128
     *
129
     * @throws BadMethodCallException If no method exists
130
     *
131
     * @return mixed
132
     *
133
     * @todo Add phpdoc comments for dynamic methods
134
     * @todo throw BadMethodCallException
135
     */
136 8
    public function __call($method, $args)
137
    {
138 8
        $argc = count($args);
139 8
        if ($argc == 1 && $this->hasColumn($index = array_pop($args))) {
140 7
            $column = $this->getColumn($index);
141 7
            if (method_exists($column, $method)) {
142 7
                return call_user_func_array([$column, $method], $args);
143
            }
144
        }
145 1
        throw new BadMethodCallException("Method does not exist: " . __CLASS__ . "::{$method}()");
146
    }
147
}