Completed
Pull Request — master (#178)
by ignace nyamagana
02:27
created

Reader::__call()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 2
cts 2
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 8
nop 2
crap 4
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
namespace League\Csv;
14
15
use BadMethodCallException;
16
use Countable;
17
use JsonSerializable;
18
use ReflectionClass;
19
use ReflectionMethod;
20
21
/**
22
 * Class to access records from a Csv document
23
 *
24
 * @package League.csv
25
 * @since  3.0.0
26
 *
27
 * @method array fetchAll() returns a sequential array of all records
28
 * @method array fetchOne(int $offset = 0) returns a specific record according to its offset
29
 * @method array fetchColumn(string|int $column_index = 0) returns a single specific column
30
 * @method array fetchPairs(string|int $offset_index = 0, string|int $value_index = 1) returns two CSV column as data pairs
31
 * @method string toHTML(string $class_attr = 'table-csv-data') returns a HTML table representation of the CSV document
32
 * @method DOMDocument toXML(string $root_name = 'csv', string $row_name = 'row', string $cell_name = 'cell') returns a XML representation of the CSV
33
 * @method array jsonSerialize() returns the records that should be serialized to JSON
34
 * @method int count() returns the records count from the CSV document
35
 */
36
class Reader extends AbstractCsv implements JsonSerializable, Countable
37
{
38
    /**
39
     * Stream filtering mode
40
     *
41
     * @var int
42
     */
43 159
    protected $stream_filter_mode = STREAM_FILTER_READ;
44
45 159
    /**
46
     * Returns a collection of selected records
47
     *
48
     * @param Statement|null $stmt
49
     *
50
     * @return RecordSet
51
     */
52
    public function select(Statement $stmt = null)
53
    {
54
        $stmt = $stmt ?: new Statement();
55
56 159
        return new RecordSet($this, $stmt);
57
    }
58 159
59 75
    /**
60
     * Triggered when invoking inaccessible methods in the object
61
     *
62 144
     * @param string $method
63
     * @param array  $args
64
     */
65
    public function __call($method, array $args)
66
    {
67
        static $stmt;
68
        if (null === $stmt) {
69
            $stmt = new Statement();
70
        }
71
72
        static $method_list;
73
        if (null === $method_list) {
74 63
            $method_list = array_flip(array_map(function (ReflectionMethod $method) {
75
                return $method->name;
76 63
            }, (new ReflectionClass(RecordSet::class))->getMethods(ReflectionMethod::IS_PUBLIC)));
77
        }
78
79
        if (isset($method_list[$method])) {
80
            return call_user_func_array([new RecordSet($this, $stmt), $method], $args);
81
        }
82
83
        throw new BadMethodCallException(sprintf('Unknown method %s::%s', get_class($this), $method));
84
    }
85
86
    /**
87
     * Returns the records count
88
     *
89 9
     * @var int
90
     */
91 9
    public function count()
92 9
    {
93 9
        return $this->__call('count', []);
94 9
    }
95 6
96 9
    /**
97 9
     * records representation to be serialized to JSON
98
     *
99 6
     * @return array
100 9
     */
101 9
    public function jsonSerialize()
102 6
    {
103
        return $this->__call('jsonSerialize', []);
104 9
    }
105
}
106