Completed
Push — master ( 7c9b2f...038cbf )
by Luke
32s
created

TabularCollection::prepareData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nop 1
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/*
4
 * Nozavroni/Collections
5
 * Just another collections library for PHP5.6+.
6
 *
7
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
8
 * @author    Luke Visinoni <[email protected]>
9
 * @license   https://github.com/nozavroni/collections/blob/master/LICENSE The MIT License (MIT)
10
 */
11
namespace Noz\Collection;
12
13
use BadMethodCallException;
14
use OutOfBoundsException;
15
16
/**
17
 * Class TabularCollection
18
 *
19
 * A collection that works as a table, meaning each item is itself an traversable item, all rows having the same set of
20
 * keys. This allows you to work with columns as well as rows.
21
 *
22
 * @package Noz\Collection
23
 */
24
class TabularCollection extends MultiCollection
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 22
    protected function prepareData($data)
30
    {
31 22
        return $data;
32
    }
33
34
    /**
35
     * Magic method call.
36
     *
37
     * @param string $method The name of the method
38
     * @param array  $args   The argument list
39
     *
40
     * @throws BadMethodCallException If no method exists
41
     *
42
     * @return mixed
43
     *
44
     * @todo Add phpdoc comments for dynamic methods
45
     * @todo throw BadMethodCallException
46
     */
47 8
    public function __call($method, $args)
48
    {
49 8
        $argc = count($args);
50 8
        if ($argc == 1 && $this->hasColumn($index = array_pop($args))) {
51 7
            $column = $this->getColumn($index);
52 7
            if (method_exists($column, $method)) {
53 7
                return call_user_func_array([$column, $method], $args);
54
            }
55
        }
56 1
        throw new BadMethodCallException('Method does not exist: ' . __CLASS__ . "::{$method}()");
57
    }
58
59
    /**
60
     * Does this collection have specified column?
61
     *
62
     * @param mixed $column The column index
63
     *
64
     * @return bool
65
     */
66 8
    public function hasColumn($column)
67
    {
68
        try {
69 8
            $this->getColumn($column);
70
71 8
            return true;
72 1
        } catch (OutOfBoundsException $e) {
73 1
            return false;
74
        }
75
    }
76
77
    /**
78
     * Get column as collection.
79
     *
80
     * @param mixed $column The column index
81
     * @param bool  $throw  Throw an exception on failure
82
     *
83
     * @return AbstractCollection|false
84
     */
85 9
    public function getColumn($column, $throw = true)
86
    {
87 9
        $values = array_column($this->data, $column);
88 9
        if (count($values)) {
89 9
            return static::factory($values);
90
        }
91 1
        if ($throw) {
92 1
            throw new OutOfBoundsException(__CLASS__ . ' could not find column: ' . $column);
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Does this collection have a row at specified index?
100
     *
101
     * @param int $offset The column index
102
     *
103
     * @return bool
104
     */
105 1
    public function hasRow($offset)
106
    {
107
        try {
108 1
            $this->getRow($offset);
109
110 1
            return true;
111 1
        } catch (OutOfBoundsException $e) {
112 1
            return false;
113
        }
114
    }
115
116
    /**
117
     * Get row at specified index.
118
     *
119
     * @param int $offset The row offset (starts from 0)
120
     *
121
     * @return AbstractCollection|false
122
     */
123 3
    public function getRow($offset)
124
    {
125 3
        return static::factory($this->getValueAtPosition($offset));
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 1
    public function map(callable $callback)
132
    {
133 1
        $ret = [];
134 1
        foreach ($this->data as $key => $row) {
135 1
            $ret[$key] = $callback(static::factory($row));
136 1
        }
137
138 1
        return static::factory($ret);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function walk(callable $callback, $extraContext = null)
145
    {
146
        foreach ($this as $offset => $row) {
147
            $callback(static::factory($row), $offset, $extraContext);
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * Is input data structure valid?
155
     *
156
     * In order to determine whether a given data structure is valid for a
157
     * particular collection type (tabular, numeric, etc.), we have this method.
158
     *
159
     * @param mixed $data The data structure to check
160
     *
161
     * @return bool True if data structure is tabular
162
     */
163 22
    protected function isConsistentDataStructure($data)
164
    {
165 22
        return static::isTabular($data);
166
    }
167
}
168